共享prefences不更新 [英] SharedPrefences not being updated

查看:496
本文介绍了共享prefences不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有其中共享preferences不被在返回到一个应用程序更新的奇数问题。这里的情景:

I am having an odd issue in which the SharedPreferences are not being updated upon returning to an app. Here's the scenario:

我必须使用相同的共享preferences两个项目。 PROJECT1和Project2的。他们是独立的,但相关的应用程序。它们使用相同的密钥签名,并使用sharedUserId共享信息。

I have two projects that use the same shared preferences. Project1 and Project2. They are separate but related apps. They are signed with the same key and use sharedUserId to share information.

PROJECT1打开Project2的。

Project1 opens Project2.

Project2的检索共享preferences文件,并通过这种方法写的:

Project2 retrieves the SharedPreferences file and writes to it via this method:

Context prefsContext = c.createPackageContext(packageNameOfProject1, Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences prefs = prefsContext.getSharedPreferences(fileName, Context.MODE_PRIVATE);
SharedPreferences.editor editor = prefs.edit();
editor.putBool("bool1", value1);
editor.putBool("bool2", value2);
...
editor.putBool("boolN", valueN);
editor.apply();

一旦做到这一点,我返回到Project通过调用完成()

PROJECT1然后读取数据,像这样:

Project1 then reads the data like so:

SharedPreferences prefs = getSharedPreferences(getPreferencesFileName(), Context.MODE_PRIVATE);
Boolean value1 = prefs.getBoolean(fileName, false);
Boolean value2 = prefs.getBoolean(fileName, false);
...
Boolean valueN = prefs.getBoolean(fileName, false);
Map<String, ?> mappings = prefs.getAll();
Set<String> keys = mappings.keySet();
for(String key : keys) {
  log.d(TAG, "_____");
  log.d(TAG, "Key = " + key);
  log.d(TAG, "Value = " + mappings.get(key));
}

问题是值不会Project1中更新。我可以告诉基于送行月底该文件甚至没有产生映射的日志。不过,我可以验证XML正在更新。如果我强制停止应用程序,然后重新启动它,所有的映射是有Project1中。所有的值是正确的。不过,我需要在用户离开Project2的时候他们更新。我觉得像有什么东西我缺少在这里,但无法发现它。

The problem is the values are not updated in Project1. I can tell based off the logs at the end that the file isn't even generating mappings. However, I can verify that the xml is being updated. If I force stop the app then restart it, all the mappings are there in Project1. All the values are correct. However, I need them updated when the user leaves Project2. I feel like there's something I'm missing here but can not spot it.

我已经能够找到关于这个问题的唯一的事情是:

The only things I have been able to find on the subject is:

<一个href="http://stackoverflow.com/questions/9050810/shared$p$pferences-editor-not-being-updated-after-initial-commit">Shared$p$pferences.Editor没有更新后最初提交

<一个href="http://stackoverflow.com/questions/10186215/shared$p$pferences-value-is-not-updated">shared$p$pferences值的更新

这些不帮助因为我已经这样做了。

These don't help as I'm already doing that.

我有两个清单WRITE_EXTERNAL_STORAG​​E集。文件名是相同的(否则我将无法读取该文件,当我重新输入程序)。

I have WRITE_EXTERNAL_STORAGE set in both manifests. The fileName is the same (else I wouldn't be able to read the file when I reenter the app).

编辑:

我要指出,我曾尝试做 editor.commit()而不是 editor.apply()因为我以为我面临的竞争条件。这个问题仍然存在。我在想,由于某种原因,正在使用,而不是一个新的,即使我每一次延迟加载它的旧参考共享preference在PROJECT1。

I should note that I did try to do editor.commit() instead of editor.apply() as I thought I was facing a race condition. The problem still persisted. I'm thinking that for some reason, the old reference to the SharedPreference in Project1 is being used instead of a new one even though I'm lazy-loading it each time.

EDIT2:

确定,进一步的测试,看看有什么ID回事。我决定尝试相反的方向。

Ok, to further test to see what id going on. I decided to try the opposite direction.

在PROJECT1我做的:

In Project1 I do:

Float testFloat (float) Math.random();
Log.d("TEST_FLOAT", "Project1: TEST_FLOAT = " + testFloat);
prefs.edit().putFloat("TEST_FLOAT", testFloat).commit();

在Project2中我做的:

In Project2 I do:

Log.d("TEST_FLOAT", "Project2: TEST_FLOAT = " + prefs.getFloat("TEST_FLOAT", 0.0f));

我再来回走两个像这样的: Project1-&GT; Project2-&GT; Project1-&GT; Project2-&GT; Project1-&GT; Project2的和这里是logcat的结果:

I then go back and forth between the two like so: Project1->Project2->Project1->Project2->Project1->Project2 and here is the logcat result:

Project1: TEST_FLOAT = 0.30341884
Project2: TEST_FLOAT = 0.30341884
Project1: TEST_FLOAT = 0.89398974
Project2: TEST_FLOAT = 0.30341884
Project1: TEST_FLOAT = 0.81929415
Project2: TEST_FLOAT = 0.30341884

在换句话说,它的读取和写入的同一个文件。然而,它的保留,它已当它第一次打开了它在该项目的映射。即使我关闭该项目,映射保持,直到应用程序的力量停止。

In other words, it's reading and writing to the same file. However, it's keeping the mapping that it had when it was first opened it in the project. Even though I close the project, the mapping remains until the application is force stopped.

推荐答案

最后的答案:

替换

getShared preferences(文件名,Context.MODE_PRIVATE);

getShared preferences(文件名,Context.MODE_MULTI_PROCESS);

根据文档:

Context.MODE_MULTI_PROCESS

Context.MODE_MULTI_PROCESS

共享preferences装载标志:当设置,磁盘上的文件将被   检查修改即使共享preferences实例是   已经装载在此过程。此行为有时希望在   情况下,该应用程序有多个进程,所有写入   相同的共享preferences文件。通常有更好的形式的   进程间的通信,虽然。

SharedPreferences loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process. This behavior is sometimes desired in cases where the application has multiple processes, all writing to the same SharedPreferences file. Generally there are better forms of communication between processes, though.

这是旧的(但无证)在之前的行为   姜饼(Android 2.3的),这标志的目标时,暗示   这样的排放。对于面向SDK版本高于应用   的Andr​​oid 2.3(姜饼),该标志必须显式设置如果需要的话。

This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targeting such releases. For applications targeting SDK versions greater than Android 2.3(Gingerbread), this flag must be explicitly set if desired.

我知道有这个简单的疏忽。

I knew there was a simple oversight in this.

这篇关于共享prefences不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆