从服务访问共享首选项 [英] Access shared preference from a service
本文介绍了从服务访问共享首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对android很陌生.
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?
非常感谢您的关注.
推荐答案
看到您已经在扩展Context的应用程序组件中,则可以简单地使用:
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);
确保在onCreate()
或之后调用此电话.
Make sure you call this in or after onCreate()
.
这篇关于从服务访问共享首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文