问题
I am fairly new to android.
I have an activity in my application. I used shared preferences to store some data related to that activity. Now, how can I access those stored data from another service class? Is it possible?
Your attention is highly appreciated.
回答1:
Seeing as you're already in an application component that extends Context, you can simply use:
SharedPreferences prefs = this.getSharedPreferences("Pref name", Context.MODE_PRIVATE);
Make sure you call this in or after onCreate()
.
回答2:
Have a look here http://developer.android.com/guide/topics/data/data-storage.html#pref
You can only access SharedPreferences within the same application ID.
If you want to access it from another application's context, you could put the mode to world-readable (but stronly not recommended and depreciated in API 17).
来源:https://stackoverflow.com/questions/14423553/access-shared-preference-from-a-service