I have application with an activity and a service, I need to save some value in the activity and retrieve in the service.
i could save the value using SharedPreferences in the activity, however, when I try to retrieve the value in the BroadcastReceiver, it says getPreferences is undefined for service.
how could I retrieve my value in BroadcastReceiver?
EDITED to reflect change of the original question from Service
to BroadcastReceiver
.
Instead of using getPreferences(int mode)
in the Activity
use...
getSharedPreferences(String name, int mode).
The getPreferences(int mode)
method is a convenience method for the above and simply passes the Activity
class name as the name
parameter. This implies it should really only be used for a given Activity
to store its own internal preferences and not preferences that need to be global to other app components.
In the case of a BroadcastReceiver
the onReceive(...)
method is passed a Context
parameter so you can use context.getSharePreferences(<some_name>, <mode>)
to get the SharedPreferences
saved by the Activity
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With