getString() question


Last Updated: 2010-09-21 17:43:52
  1. raubvogel

    raubvogel Member

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

    Code (Text):
    1.  
    2. import android.content.Context;
    3. import android.content.Intent;
    4. import android.os.Bundle;
    5. import android.content.BroadcastReceiver;
    6. import android.telephony.gsm.SmsMessage;
    7.  
    8.  
    9. public class SMSReceiver extends BroadcastReceiver
    10. {
    11.     @Override
    12.     public void onReceive (Context context, Intent intent)
    13.     {
    14.         Bundle bundle = intent.getExtras();        
    15.         SmsMessage[] msgs = null;
    16.         String str = "";
    17.        
    18.         if (bundle != null)
    19.         {
    20.             // Now retrieve the SMS message
    21.              Object[] pdus = (Object[]) bundle.get("pdus");
    22.              msgs = new SmsMessage[pdus.length];
    23.              for (int i=0; i<msgs.length; i++)
    24.              {
    25.                  msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                    
    26.                  str += msgs[i].getMessageBody().toString();
    27.                  if (str.equals("one-two-three"))
    28.                  {
    29.                       // Do something
    30.                  }
    31.  
    32.              }
    33.  
    34.         }
    35.         return;
    36.     }
    37. }
    38.  
    I have the "one-two-three" defined in strings.xml. How can I use it here?

    Advertisement
  2. miXer

    miXer Well-Known Member

    To reference strings you use: "@string/one-two-three"
  3. RNekic

    RNekic Member

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

    I typically use:

    Code (Text):
    1. context.getString(R.string.OneTwoThreeStringName);
  4. miXer

    miXer Well-Known Member

    "@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

Share This Page