title: Shared Preferences are not being read correctly in a BroadcastReceiver
body: <p>I am new to android development. I have a problem with <code>SharedPreferences</code> not being read in a <code>BroadcastReceiver</code>  </p>
  
  <p>Background:<br>
  I am trying to write an Android App that has no GUI. the App will do some work every time the 
  application icon is clicked, write settings to Shared preferences, set up an Alarm for 3 seconds in the future and then exit(finish()). The Application icon will be clicked 3 or 4 times in quick succession. The BroadcastReceiver and the Shared Preferences will allow me to keep track of how many times it was clicked, when it was last clicked and some other settings.</p>
  
  <p>The setting that I am using in the manifest to suppress the GUI is </p>
  
  <pre><code>android:theme="@android:style/Theme.NoDisplay"
  </code></pre>
  
  <p>There are 3 files </p>
  
  <ol>
  <li>A manifest file</li>
  <li>My main activity file MyFirstTestActivity.java</li>
  <li>My Broadcast Receiver class MyReceiver.java</li>
  </ol>
  
  <p>When I click the Application icon multiple times in quick succession. The class MyFirstTestActivity reads the Shared Preferences "Count" setting correctly and increments it correctly.
  It sets the Alarm and passes a an extra piece of data called "CurrentCount"
  In my Broadcast Receiver when I read the Shared Preferences and the extra data passed with the intent it is stuck at 1 it doesn't increment correctly.  </p>
  
  <p>What I am trying to achieve is in MyReceiver.onReceive by comparing the value of<br>
  The "Count" Shared Preference and the "CurrentCount" Intent extra setting passed in are the same value I will know that the user has stopped clicking quickly in succession and then I can display a message to the user, rather than displaying the message every time it is called.</p>
  
  <p>Below is a section of the Eclipse LogCat log</p>
  
  <blockquote>
    <p><strong>11-288 09:43:10.652: I/UpdateSettings(782): Read Value of Count from Preferences -7<br>
    11-28 09:43:10.712: I/UpdateSettings(782): Read Back Count from Preferences - 8</strong><br>
    11-28 09:43:10.892: W/InputManagerService(112): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@415ef6a8<br>
    11-28 09:43:11.012: W/NetworkManagementSocketTagger(112): setKernelCountSet(10040, 0) failed with errno -2<br>
    <strong>11-28 09:43:13.772: I/MyReceiver(797): Test<br>
    11-28 09:43:13.782: I/MyReceiver(797): Preferences Contains the field Count<br>
    11-28 09:43:13.792: I/MyReceiver(797): count=1<br>
    11-28 09:43:13.792: I/MyReceiver(797): currentCount=1</strong><br>
    11-28 09:43:36.162: I/ActivityManager(112): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000   cmp=com.mywebsite.myfirsttest/.MyFirstTestActivity} from pid 212<br>
    11-28 09:43:36.162: W/WindowManager(112): Failure taking screenshot for (180x300) to layer 21005
    11-28 09:43:36.212: W/NetworkManagementSocketTagger(112): setKernelCountSet(10040, 1) failed with errno -2<br>
    <strong>11-28 09:43:36.272: I/UpdateSettings(782): Read Value of Count from Preferences -8<br>
    11-28 09:43:36.362: I/UpdateSettings(782): Read Back Count from Preferences - 9</strong><br>
    11-28 09:43:36.583: W/InputManagerService(112): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@41640fa8<br>
    11-28 09:43:36.653: W/NetworkManagementSocketTagger(112): setKernelCountSet(10040, 0) failed with errno -2<br>
    <strong>11-28 09:43:39.402: I/MyReceiver(797): Test<br>
    11-28 09:43:39.412: I/MyReceiver(797): Preferences Contains the field Count<br>
    11-28 09:43:39.412: I/MyReceiver(797): count=1<br>
    11-28 09:43:39.412: I/MyReceiver(797): currentCount=1</strong><br>
    11-28 09:44:27.072: D/dalvikvm(196): GC_CONCURRENT freed 398K, 6% free 10399K/10951K, paused 5ms+4ms<br>
    11-28 09:47:46.312: W/ThrottleService(112): unable to find stats for iface rmnet0  </p>
  </blockquote>
  
  <p>The logs with the tag UpdateSettings show as updating correctly
  The logs with the tag of MyReceiver are not reading the latest Shared Preference values (or intent extra Data) correctly</p>
  
  <p>Here are my 2 source files
  MyFirstTestActivity.java</p>
  
  <pre><code>public class MyFirstTestActivity extends Activity {
  
      public static final String MY_PREFS = "TestPrefs";
  
      /** Called when the activity is first created. */
          @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // Note: This application has no display 
          // See the manifest @android:style/Theme.NoDisplay"
  
          //Toast.makeText(this,"in onCreate", Toast.LENGTH_SHORT).show();
  
          // Increment the count shared setting 
          int count = updateSharedPreferenceSettings();
          // Send the alarm
          setMessageAlarm(count);
          // Exit the application
          finish();
  
      }
  
  
      private int updateSharedPreferenceSettings() {
          SharedPreferences prefs = this.getSharedPreferences(MyFirstTestActivity.MY_PREFS, Context.MODE_WORLD_READABLE);
  
  
          int count = 1;
          if(prefs.contains("Count")) {
              count = prefs.getInt("Count", 1);
              Log.i("UpdateSettings", "Read Value of Count from Preferences -" +     String.valueOf(count));
              count = count+1;
          }
  
          Editor editor = prefs.edit();
          editor.putInt("Count", count);
          editor.commit();
          Log.i("UpdateSettings", "Read Back Count from Preferences - " +             prefs.getInt("Count", 0));
  
          return count;
      }
  
     private void setMessageAlarm(int currentCount) {
         Intent intent = new Intent(MyFirstTestActivity.this, MyReceiver.class);
         intent.putExtra("CurrentCount", currentCount);
  
         PendingIntent appIntent = 
              PendingIntent.getBroadcast(MyFirstTestActivity.this, 0, intent, 0);
  
  
         Calendar c = Calendar.getInstance();
         c.setTimeInMillis(System.currentTimeMillis());
         c.add(Calendar.SECOND, 3);
  
         // Set the alarm for 3 seconds in the future
         AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
         am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), appIntent); 
     }
  }
  </code></pre>
  
  <p>My Receiver Source File MyReceiver.java  </p>
  
  <pre><code>    public class MyReceiver extends BroadcastReceiver {
  
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Log.i("MyReceiver", "Test");
  
          SharedPreferences prefs =     context.getSharedPreferences(MyFirstTestActivity.MY_PREFS, Context.MODE_WORLD_READABLE);
          if(prefs.contains("Count")) {
          Log.i("MyReceiver", "Preferences Contains the field Count");
  
          int count = prefs.getInt("Count", -2);
              Log.i("MyReceiver", "count=" + String.valueOf(count));
  
              int currentCount = intent.getIntExtra("CurrentCount", -3);
              Log.i("MyReceiver", "currentCount=" + String.valueOf(currentCount));    
      }
          else {
              Log.i("MyReceiver", "Preferences Does NOT Contains the field Count");
              }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My Manifest File   </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mywebsite.myfirsttest"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk android:minSdkVersion="14" /&gt;
  
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
          &lt;activity
              android:label="@string/app_name"
              android:name="com.mywebsite.myfirsttest.MyFirstTestActivity"
              android:theme="@android:style/Theme.NoDisplay"
               &gt;
              &lt;intent-filter &gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;receiver  android:name="com.mywebsite.myfirsttest.MyReceiver" android:process=":remote"&gt;&lt;/receiver&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Why does MyReceiver.java not pick up the last SharedPreferences, but instead get stuck with the value the first time the alarm was set?
  Is it appropriate to use the Context passed in to MyReceiver?</p>