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
ServicetoBroadcastReceiver.Instead of using
getPreferences(int mode)in theActivityuse…The
getPreferences(int mode)method is a convenience method for the above and simply passes theActivityclass name as thenameparameter. This implies it should really only be used for a givenActivityto store its own internal preferences and not preferences that need to be global to other app components.In the case of a
BroadcastReceivertheonReceive(...)method is passed aContextparameter so you can usecontext.getSharePreferences(<some_name>, <mode>)to get theSharedPreferencessaved by theActivity.