1

The docs state that :

Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.

My question is a context related question really - I am not quite clear if the context received by a BroadcastReceiver (in myapp.receivers package), the context I get in some activity or service (in myapp.activities and myapp.services respectively) will delegate to the same SharedPreferences instance.

Note that behind the scenes getDefaultSharedPreferences(context) calls getSharedPreferences(context.getPackageName(), MODE_PRIVATE).

Btw it is also reported that starting an app from shortcut as opposed to menu gives different Default Preferences

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361

2 Answers2

1

Contexts that a normal app has access to are assigned to Applications, Activities, Services, and BroadcastReceivers (did I miss any?). From any Context, you can go up to the Application Context by calling getApplicationContext() (safe to call on the Application Context as well).

The packages within a single app don't have any impact on the Context. I've no idea what that linked question is about but getPackageName() essentially gives you the package name defined in the manifest (in fact it returns the name in the app's PackageInfo object).

It is plausible that the BroadcastReceiver Context has a different package name (though I strongly doubt it). More importantly, you can still get the application context, so just do that from everywhere.

Delyan
  • 8,881
  • 4
  • 37
  • 42
0

Manifest Broadcast Receiver: The context in which the Manifest BroadcastReceiver runs is android.app.ReceiverRestrictedContext. This context has limited features enabled, though it does have packageName (that registered the receiver). Using getDefaultSharedPreferences in BroadcastReceiver should not be a problem as the information needed to retrieve the Default Shared Preferences (ie., package name) is available in this restricted context.

Dynamically Registered Broadcast Receivers: Dynamically registered Broadcast Receivers (registered using resgisterReceiver) run in the context of the Activity or the service that registered the receiver.

I could not find any direct documentation mentioning details of context in Broadcast Receivers, this is outcome of my experience and trials.

Also, regarding the observation on difference in behavior of getDefaultSharedPreferences if launched from Menu vs shortcut, I never faced such an issue, and neither could find references to such issues on the net. So, it is likely that this issue is limited to a particular combination of SDK, device, Mod.

Sudhee
  • 704
  • 6
  • 12
  • I know about the `ReceiverRestrictedContext` - so in all cases the name I get back is the one in ``. Any links to the source, google groups etc ? Also what about the discrepancy reported [here](http://stackoverflow.com/questions/10786172/android-getdefaultsharedpreferences) ? – Mr_and_Mrs_D May 02 '13 at 18:27
  • I still miss some more formal links but closing for now – Mr_and_Mrs_D May 03 '13 at 11:51