[英]Preferences not up-to-date when read from service
I have the a very simple preferences implementation in my app.我的应用程序中有一个非常简单的首选项实现。 This is my Preferences activity:这是我的首选项活动:
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Every activity or service that reads preferences does this:每个读取首选项的活动或服务都会这样做:
preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Now, one of the settings is a server the app connects to.现在,其中一项设置是应用程序连接到的服务器。 This is implemented as an unbound service because it's supposed to remain online even when the user left the app.这是作为未绑定服务实现的,因为即使用户离开应用程序,它也应该保持在线状态。 When the service starts (onStartCommand), the socket information is read from the preferences.当服务启动(onStartCommand)时,从首选项中读取套接字信息。
The problem: After changing a preference and restarting the service it still has the old value.问题:更改首选项并重新启动服务后,它仍然具有旧值。
I don't get it!我不明白! Please help :)请帮忙 :)
Here's a little more detail since its still not working.这里有更多细节,因为它仍然无法正常工作。 My guess my error lies somewhere else ...我猜我的错误在于其他地方......
My service looks as follows.我的服务如下所示。
public class XMPPService2 extends Service {
SharedPreferences preferences;
public void onCreate() { ... }
public int onStartCommand(Intent intent, int flags, int startId) {
preferences = getSharedPreferences("com.company.appname_preferences.xml", MODE_MULTI_PROCESS);
Log.d(TAG, preferences.getString("server", "DEFAULT")); // this always prints the default value
}
public void onDestroy() { ... }
}
As far as I understand, there is not much more I have to do about it.据我所知,我没有什么可做的了。 I've also tried to我也试过
getPreferenceManager().setSharedPreferencesMode(MODE_MULTI_PROCESS);
before前
addPreferencesFromResource(R.xml.preferences);
but this didn't help.但这没有帮助。
Now, I know that these methods are deprecated.现在,我知道这些方法已被弃用。 However, I have to make sure the app runs on tablets as well as 2.3 devices.但是,我必须确保该应用程序可以在平板电脑和 2.3 设备上运行。
I just wanted to mention that I'm still new to android and maybe I've misunderstood some basic concept.我只是想提一下,我对 android 还是个新手,也许我误解了一些基本概念。 The answers I've gotten coincide with my web research, so i expect them to be correct.我得到的答案与我的网络研究一致,所以我希望它们是正确的。
If you are trying to access a shared preference from a different process (even though they are in same package), you need to use MODE_MULTIPROCESS.如果您尝试从不同的进程访问共享首选项(即使它们在同一个包中),您需要使用 MODE_MULTIPROCESS。
Please use the following code:请使用以下代码:
SharedPreferences pref = getSharedPreferences("com.mypackage.myapp_preferences", MODE_MULTI_PROCESS);
The important thing is to use MODE_MULTI_PROCESS and name of your preference file.重要的是使用 MODE_MULTI_PROCESS 和您的首选项文件的名称。
To share the preferences between processes, you need to have *MODE_MULTI_PROCESS* set on the preferences file.要在进程之间共享首选项,您需要在首选项文件上设置 *MODE_MULTI_PROCESS*。 Problem is that you can't control flags of the default preferences file, right?问题是您无法控制默认首选项文件的标志,对吗? Well, There's a way.嗯,有办法。 In your PreferenceActivity class, add these lines to onCreate() :在您的PreferenceActivity类中,将这些行添加到onCreate() :
PreferenceManager prefMgr = getPreferenceManager();
prefMgr.setSharedPreferencesName("preferences");
prefMgr.setSharedPreferencesMode(MODE_MULTI_PROCESS);
Then instead of using PreferenceManager.getDefaultSharedPreferences , get the preferences from the file using:然后,而不是使用PreferenceManager.getDefaultSharedPreferences ,使用以下方法从文件中获取首选项:
getSharedPreferences("preferences", MODE_MULTI_PROCESS);
You have to read your preferences from the same package.您必须从同一个包中读取您的首选项。 Move your service to the same package as the preference activity and you are done.将您的服务移动到与偏好活动相同的包中,您就完成了。
这似乎是 android 中的一个错误: https : //code.google.com/p/android/issues/detail? id =66625
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.