Broadcast receiver not affecting shared preferences in Android 4.0 (probably 3.1+)

时间秒杀一切 提交于 2019-12-06 06:19:59

Use Context.MODE_MULTI_PROCESS as the 2nd parameter to getSharedPreferences(). The problem is that you have the broadcast receiver running in a separate process from your activities, and as of Android 3.0 the default behaviour of multi-process access to shared preferences has changed. This should fix the problem for you on 4.x devices.

However, there is a bug in Android 2.3 which causes simultaneous access to shared preferences from multiple processes not to work reliably in some cases. We came across this and were very frustrated by it because it is difficult to reproduce and explain (probably some kind of timing issue).

What I ended up doing was just having a check in the activity itself that says "if the alarm time is less than 0, delete the alarm time." It was a work around; I didn't receive any other answer that worked unfortunately.

you should delete android:process=":remote" attribute in your receiver in the manifest. SharedPreferences does not work with new processes and :remote declares "do whatever the process id is ".

I know this is an old question but maybe my answer will be helpful for others who facing this problem this days. I just found a solution and it worked for me.

make a service and implement the work you want on it, and then just call the service from your broadcast receiver, which means the only work of your receiver is to call the service which will handle the work you want to do.

The simply answer is this: don't bother doing so much in a receiver. Instead, kick of an intent call to another Activity in the same package hierarchy and you'll be able to do it just as easily.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!