没有得到在服务的共享preference更新值 [英] Not getting the updated value of the shared preference in the service
问题描述
我是从存储从一个小部件发起了一个活动有一定价值的共享preference。如果我检索值从服务来自相同插件启动时,它是不更新之一。我得到了previous值曾在共享preference。即使我检查的共享preference XML的价值,我看到更新的一个人也没有。 为什么发生这种情况。我知道,小工具和活动的是两个过程,是什么原因呢?
I am storing some value to a shared preference from an activity which launched from a widget. If I retrieve that value from the service started from the same widget, it is not the updated one. I am getting the previous value had in the shared preference. Even i check that value in the shared preference xml, i sees the updated one there. Why this is happening. I know that widget and activity are two process, is that the reason ?
共享preferences preferences = getShared preferences(preferences_target_value,Context.MODE_PRIVATE);
串targetValue = preferences.getString(preferences_target_value,0);
的System.out.println(targetValue+ targetValue);
SharedPreferences preferences = getSharedPreferences("preferences_target_value", Context.MODE_PRIVATE);
String targetValue = preferences.getString("preferences_target_value", "0");
System.out.println("targetValue "+targetValue);
推荐答案
这些值每个进程的高速缓存。
These values are cached per process.
如果您在运行Android> 2.3,您必须指定 MODE_MULTI_PROCESS
当你调用 getShared preferences()
。如果您在Android&LT运行; 2.3那么它应该只是正常工作。如果您在Android 2.3上运行再有就是在共享preferences的东西的一个错误,它不正确在多个流程的工作,无论你做什么。
If you are running on Android > 2.3 you must specify MODE_MULTI_PROCESS
when you call getSharedPreferences ()
. If you are running on Android < 2.3 then it should just work correctly. If you are running on Android 2.3 then there is a bug in the shared preferences stuff and it doesn't work correctly across multiple processes no matter what you do.
这篇关于没有得到在服务的共享preference更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!