Shared Preferences used in OnReceive does not update
<p>I have a service that tracks battery information and stores it in Shared Preferences. Then from my main activity I have <code>BroadcastReceiver</code>(that has <code>OnReceive</code> for listening <code>ACTION_BATTERY_CHANGED</code>)</p>

<p>Every time OnReceive occurs battery level and temperature changes, but my Shared Preferences leaves the same until I change my screen orientation(I think because of recreation of activity).
Please, help me to somehow make onReceive method also update shared preferences.</p>

<pre><code>My Main Activity:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
        appPrefs = new AppPreferences(getApplicationContext());
......
/* Broadcast Receiver for battery stats                                               */
        BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
.....
private TextView text;
int level = -1;
                @Override
            public void onReceive(Context context, Intent intent) {

                level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                text=(TextView) findViewById(R.id.TV_text);
                text.setText("level"+level+"something from Shared Pref"+appPrefs.getAnything());
}
}
}
</code></pre>

<p>My Shared Preferences:</p>

<pre><code>public class AppPreferences extends PreferenceActivity {

    private static final String APP_SHARED_PREFS = "com.aydabtu.BroadcastSMS_preferences"; //  Name of the file -.xml
         private SharedPreferences appSharedPrefs;
         private Editor prefsEditor;

         public AppPreferences(Context context)
         {
             this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
             this.prefsEditor = appSharedPrefs.edit();
         }

         public long getAnything() {
             return appSharedPrefs.getLong("Anything", 9);

         }

         public void setAnything(long a) {
             prefsEditor.putLong("Anything", a);
             prefsEditor.commit();
         }
</code></pre>

<p>My service:</p>

<pre><code>BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent){
......
appPrefs.setAnything(a);
</code></pre>

<p>So, why Shared Preferences are not updated as level or temperature in OnReceive method?
And what to do? Any help will be appreciated. :)</p>
