SharedPreferences in BroadcastReceiver seems to not update?

I have a Activity which updates a string in the SharedPreferences.

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", username);

I then start a service:

startService(new Intent(this, MyService.class));

The service creates a reference to Alarm which extends BroadcastReceiver:

Alarm alarm = null;
public void onCreate() {
    alarm = new Alarm();
public int onStartCommand(Intent intent, int flags, int startId) {

In SetAlarm I do all the basic setting up stuff (At this point, "username" is still correct.. i checked):

public void SetAlarm(Context context) {
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1000 * 60 * interval, pi);

I then stop the service and then start it again (using SetAlarm).

public void CancelAlarm(Context context) {
   Intent intent = new Intent(context, Alarm.class);
   PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
   AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

The issue is in onReceive.. the first time the "username" field is correct. The second time, if username is updated between the service stopping and starting, however, it returns the first value. The value does not seem to get updated...

public void onReceive(Context context, Intent intent) {   
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    Log.e("hi", settings.getString("username", ""));


I had the same problem and after struggling for hours to solve it, i finally found the issue causing it. In your AndroidManifest you probably have something like that:

<receiver android:name="AlarmReceiver" android:process=":remote" />

The last attribue (process:remote) cause the reciver to run on a different/new process when it is called. But SharedPreferences is NOT supported between different processes.

So what I did is to remove this last attribute from the manifest. The implication is that the code will now run on the main thread - but if you only have a few lines to show a notification then that shouldn't not be a problem. Another alternative is to call a service to run the long operation.

Need Your Help

Data.MemoCombinators, where can I find examples?

haskell recursion dynamic-programming

This package has some functions to turn recursive functions into dynamic programming recursive functions, for better performance:

How can I detect if text is paste using right-click

javascript jquery html css

If I paste text using ctrl c and ctrl v on table1(For example on "Apple"), duplicate text on input on table2 still change, but If i paste using right click and paste,duplicate input on table 2 does...