Android服务getSharedPreferences问题

时间:2012-09-22 09:29:44

标签: android service sharedpreferences

基本上,我的服务会显示一个通知,如果用户愿意,可以关闭该通知。

该设置由应用程序内部控制,并通过PreferenceActivity设置。

退出活动时:

public void onDestroy(){
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    debug.postLog(Log.DEBUG, "Settings", "("+this.toString()+") showNotification: " + prefs.getBoolean("showNotification", true));

显示正确的值,如果用户取消选中该复选框,则显示false,反之亦然。

与此同时,我在服务中随时致电:

prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Boolean show = prefs.getBoolean("showNotification", true);
    debug.postLog(Log.DEBUG, "Passive Service", "("+this.toString()+") showNotification: " + prefs.getBoolean("showNotification", true));       
    if(prefs.getBoolean("showNotification", true)){

我得到相同的结果,直到应用程序完全停止然后重新启动,然后保留当前值。这几乎就像服务获取首选项的快照。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

解决了,该服务以远程方式运行。