Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Find everything you need for the Galaxy S5 and discuss it in our S5 forum!
Have you seen that OnePlus One's awesome camera?? The forum is over here!

test: Reply
 
LinkBack Thread Tools
Old September 17th, 2010, 10:45 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Sep 2010
Location: FL
Posts: 14
 
Device(s): G1
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default getString() question

Let's say I have a piece of code that looks like this:

Code:
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.telephony.gsm.SmsMessage;


public class SMSReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive (Context context, Intent intent)
    {
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = ""; 
        
        if (bundle != null)
        {
            // Now retrieve the SMS message
             Object[] pdus = (Object[]) bundle.get("pdus");
             msgs = new SmsMessage[pdus.length]; 
             for (int i=0; i<msgs.length; i++)
             {
                 msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                     
                 str += msgs[i].getMessageBody().toString();
                 if (str.equals("one-two-three"))
                 {
                      // Do something
                 }

             }

        }
        return;
    }
}
I have the "one-two-three" defined in strings.xml. How can I use it here?

raubvogel is offline  
Reply With Quote
sponsored links
Old September 17th, 2010, 01:39 PM   #2 (permalink)
Member
 
Join Date: Aug 2010
Location: Norway
Posts: 313
 
Device(s): Samsung Galaxy Nexus
Carrier: Not Provided

Thanks: 0
Thanked 53 Times in 51 Posts
Default

To reference strings you use: "@string/one-two-three"
miXer is offline  
Reply With Quote
Old September 21st, 2010, 09:59 AM   #3 (permalink)
New Member
 
Join Date: May 2010
Posts: 12
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 1 Time in 1 Post
Default

Does that syntax work in code or just the layout XML?

I typically use:

Code:
context.getString(R.string.OneTwoThreeStringName);
Quote:
Originally Posted by miXer View Post
To reference strings you use: "@string/one-two-three"
RNekic is offline  
Reply With Quote
Old September 21st, 2010, 12:43 PM   #4 (permalink)
Member
 
Join Date: Aug 2010
Location: Norway
Posts: 313
 
Device(s): Samsung Galaxy Nexus
Carrier: Not Provided

Thanks: 0
Thanked 53 Times in 51 Posts
Default

"@string/one-two-three" is just for xml layout.

If you want to reference a string in code, you use R.string.one-two-three
miXer is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:39 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.