0

I was just guessing how to access the DefaultSharedPreferences (set in a PreferenceActivity) from an application in a BroadcastReceiver, knowing that onReceive method comes with Context and Intent?

Best regards,

2 Answers2

1

Looking at the source of it, we can see getDefaultSharedPreferences actually uses getSharedPreferences. So we can use getSharedPreferences the same we getDefaultSharedPreferences uses it.

In this specific case (onReceive of a BroadcastReceiver) :

SharedPreferences settings = context.getSharedPreferences(context.getPackageName() + "_preferences", Context.MODE_PRIVATE);
1

In the BroadcastReceiver:

PreferenceManager.getDefaultSharedPreferences(context)
Mohamed Hafez
  • 8,621
  • 7
  • 41
  • 49