1. Check out our companion app, Forums for Android! Download from Google Play

getString() question

Discussion in 'Application Development' started by raubvogel, Sep 17, 2010.

  1. raubvogel

    raubvogel Member
    Thread Starter
    15

    Sep 17, 2010
    14
    0
    15
    FL
    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
    53

    Aug 12, 2010
    313
    51
    53
    Norway
    To reference strings you use: "@string/one-two-three"
     
  3. RNekic

    RNekic Member
    16

    May 25, 2010
    12
    1
    16
    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
    53

    Aug 12, 2010
    313
    51
    53
    Norway
    "@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

Loading...