title: Android: using sharedPreferences in a broadcast receiver
body: <p>I'm trying to create an application that will trigger a toast message if the phone receives an SMS containing a string that is stored in the default preferences. My problem is that I'm having trouble getting the toast to appear when the SMS is received. I've tested my code for the receiver with a declared string and it works, but when I use the preference stored one, it doesn't come up. Here is my sample code:</p>
  
  <pre><code>public class Main extends Activity{
  private static final int RESULT_SETTINGS = 1;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      display();
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.action_settings:
              Intent i = new Intent(this, Settings.class);
              startActivityForResult(i, RESULT_SETTINGS);
              break;
      }
      return true;
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      switch (requestCode) {
          case RESULT_SETTINGS: display(); break;
      }
  }
  
  private void display(){
      TextView displayv = (TextView) findViewById(R.id.mysettings);
      SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
      // codes that display
  }
  </code></pre>
  
  <p>And here is the receiver</p>
  
  <pre><code>public class WakeSMS extends BroadcastReceiver{
  
  @Override
  public void onReceive(Context context, Intent intent){
      SharedPreferences sharedPrefs = context.getSharedPreferences("sharedPrefs", context.MODE_PRIVATE);
      String trigger=sharedPrefs.getString("smsstr","NULL");
  
      Bundle bundle = intent.getExtras();
      SmsMessage[] msgs = null;
      String str= "SMS from";
      if(bundle != null){
          Object[] pdus =(Object[])bundle.get("pdus");
          msgs=new SmsMessage[pdus.length];
          for (int i = 0; i&lt;msgs.length; i++){
              msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
              if(i==0){
                  str+= msgs[i].getOriginatingAddress();
                  str+=": ";
              }
              str+=msgs[i].getMessageBody().toString();
          }
          if(str.contains(trigger)){
              Toast.makeText(context, str, Toast.LENGTH_LONG).show();
          }
      }
  }}
  </code></pre>
  
  <p>In my main activity, I can get my code to display the prefs but in the receiver it fails to trigger the toast. Is there anything I'm doing wrong? (My receiver is called WakeSMS because It's supposed to trigger an alarm in the future, but right now I just want it to trigger a toast for testing)</p>
  
  <p>Edit: I have a feeling that the way I've declared my preferences in my code is perhaps off, but I'm at a loss trying to figure out what I'm doing wrong since the preferences' values can be displayed in the main activity, but cannot be used in the receiver.</p>