不能共享活动和远程服务之间共享preferences - Android的错误或意见? [英] Cannot share SharedPreferences between activity and remote service - Android bug or feature?

查看:149
本文介绍了不能共享活动和远程服务之间共享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.com/a/8723589/1127492

The answer is here, thx to x90: http://stackoverflow.com/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 - Android的错误或意见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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