Access shared preference from a service

[亡魂溺海] 提交于 2019-12-24 12:44:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!