0

I'm having trouble accessing my preferences from a BroadcastReceiver. I'm using the following code:

public class MyReceiver extends BroadcastReceiver
{
    private static final String TAG = MyReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent)
    {
        SharedPreferences sharedPreferences = 
            PreferenceManager.getDefaultSharedPreferences(
                context.getApplicationContext());
        Log.d(TAG, sharedPreferences.getAll().toString());
    }
}

This code always prints stale preferences. When I go to my PreferenceActivity, change settings and then fire an event that triggers the above BroadcastReceiver it always prints old values for the preferences.

I also added the same code to my main activity and it always prints the correct settings.

What is going on here? Are the settings being cached? Is there a way to force a reload from my BroadcastReceiver ?

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88

1 Answers1

0

It seems my problem is described in SharedPreferences in BroadcastReceiver seems to not update?

My BroadcastReceiver is started from a Service that looks like:

<service android:name=".MyService" android:process=":remote">
  <intent-filter>
    <action android:name="com.example.MyService"/>
  </intent-filter>
</service>

Removing the android:process=":remote" made my problem go away. Though I am not sure if this is the right thing to do for my application. I kind of depend on this service being detached from the main app as it needs to be started at boot time.

Community
  • 1
  • 1
Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88