不能共享活动和远程服务之间共享preferences

编程入门 行业动态 更新时间:2024-10-21 19:29:38
本文介绍了不能共享活动和远程服务之间共享preferences - Android的错误或意见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望在共享preferences改变来更新远程服务。下面使用API​​级别8(Android 2.2的)工作。

I want to update a remote service when the SharedPreferences change. The following used to work with API level 8 (Android 2.2).

我的活动有一个在preferencesChangedListener 这就要求通过服务粘结剂对象的远程服务。接口远程服务提供了一个方法 prefsChanged(无效)这就是所谓的监听器。随后接口方法除其他外计算为警报触发一些动作新时间。 这一次也被保存到共享preferences 和广播被发送。收到我的活动现在可以显示新的警报时间播出。 活动和远程服务,通过调用一个静态方法获得共享preferences 对象获取preFS(上下文),回答 preferenceManager.getDefaultShared preferences(上下文)。

My activity has an OnPreferencesChangedListener which calls the remote service via the service binder object. The interface to the remote service offers a methods prefsChanged(void) which is called by the listener. Afterwards the interface methods computes among other things the new time for an alert to trigger some action. This time is also saved to the SharedPreferences and a broadcast is send. The broadcast is received by my activity which can now display the new alert time. Activity and remote service get the SharedPreferences object through a call to a static method GetPrefs(Context) which answers PreferenceManager.getDefaultSharedPreferences(Context).

使用API​​ 15级(安卓4.0.3)这个停止工作。调试表明,远程服务认为持不同(较老的?)值的其他一些版本的共享preferences的。从活动和从远程服务登录时登录的共享preferencesImpl对象显示不同的地址。该文件系统揭示了只有一个共享preferences文件。

With API level 15 (Android 4.0.3) this stopped working. Debugging shows that the remote service sees some other version of the SharedPreferences holding different (older?) values. Logging the SharedPreferencesImpl object displays different addresses when logging from the activity and from the remote service. The file system reveals only one shared preferences file.

这是一个错误,或做了我的code与API级别8巧合的工作? 任何建议将AP preciated。

Is this a bug or did my code work with API level 8 by coincidence? Any suggestions will be appreciated.

推荐答案

答案就在这里,THX到X90:的http:// stackoverflow/a/8723589/1127492

The answer is here, thx to x90: stackoverflow/a/8723589/1127492

使用MODE_MULTI_PROCESS这是旧的(但无证)的和姜饼(Android 2.3的)前的行为和目标定位等版本时,这个标志是隐含的。对于应用程序的目标定位SDK版本高于Android 2.3的,该标志必须显式设置如果需要的话。

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

更多推荐

不能共享活动和远程服务之间共享preferences

本文发布于:2023-08-01 12:30:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:preferences

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!