简体   繁体   English

从服务中读取时首选项不是最新的

[英]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);

It could be because the method addPreferencesFromResource() is depreciated in API 11:这可能是因为方法addPreferencesFromResource()在 API 11 中被贬低了:

Read more here 在这里阅读更多

More likely there is a problem with how the preference is being committed.更有可能的是,偏好的提交方式存在问题。 Could you post your XML?你能发布你的XML吗?

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.

相关问题 依赖关系是最新的,但Gradle认为不是 - Dependencies are up-to-date but Gradle thinks not 获取最新的GPS位置 - Get up-to-date GPS Location 在 Android 中使用两个 Fragment 时,如何确保数据保持一致和最新? - How to ensure that data remains consistent and up-to-date when working with two Fragments in an Android? 尝试从Android中的首选项读取字符串时出现XML错误 - XML error when trying to read string from preferences in android 尝试从首选项数据存储中读取数据时返回 null - Return null when try to read data from preferences datastore 当我想生成 apk 文件时:UncheckedIOException:在最新检查期间无法捕获任务属性的输入文件的快照 - When I want to generate apk file: UncheckedIOException: Failed to capture snapshot of input files for task property during up-to-date check 检测到我的应用程序小部件的数据不是最新的 - Detecting that my app widget's data is not up-to-date 如何始终保持对活动的最新引用 - How to keep always up-to-date reference to Activity Android Studio停留在对话框中,提示类最新检查 - Android studio stuck at dialog saying Classes up-to-date check Android Studio 在我的应用程序上发出警告:app:stripDebugDebugSymbols UP-TO-DATE - Android Studio Warning on my app: app:stripDebugDebugSymbols UP-TO-DATE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM