BroadcastReceiver 中的 SharedPreferences 似乎没有更新? [英] SharedPreferences in BroadcastReceiver seems to not update?

查看:20
本文介绍了BroadcastReceiver 中的 SharedPreferences 似乎没有更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,它更新 SharedPreferences 中的一个字符串.

I have a Activity which updates a string in the SharedPreferences.

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", username);
editor.commit();

然后我启动一个服务:

startService(new Intent(this, MyService.class));

服务创建对扩展广播接收器的警报的引用:

The service creates a reference to Alarm which extends BroadcastReceiver:

Alarm alarm = null;
public void onCreate() {
    alarm = new Alarm();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    alarm.SetAlarm(this);
}

在 SetAlarm 中,我进行了所有基本设置(此时,用户名"仍然正确.我检查过):

In SetAlarm I do all the basic setting up stuff (At this point, "username" is still correct.. i checked):

public void SetAlarm(Context context) {
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1000 * 60 * interval, pi);
}

然后我停止服务,然后再次启动它(使用 SetAlarm).

I then stop the service and then start it again (using SetAlarm).

public void CancelAlarm(Context context) {
   Intent intent = new Intent(context, Alarm.class);
   PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
   AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
   alarmManager.cancel(sender);
}

问题出在 onReceive.. 第一次用户名"字段是正确的.第二次,如果用户名在服务停止和启动之间更新,但是,它返回第一个值.该值似乎没有更新...

The issue is in onReceive.. the first time the "username" field is correct. The second time, if username is updated between the service stopping and starting, however, it returns the first value. The value does not seem to get updated...

public void onReceive(Context context, Intent intent) {   
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    Log.e("hi", settings.getString("username", ""));
}

推荐答案

我遇到了同样的问题,经过几个小时的努力解决后,我终于找到了导致它的问题.在您的 AndroidManifest 中,您可能有类似的内容:

I had the same problem and after struggling for hours to solve it, I finally found the issue causing it. In your AndroidManifest you probably have something like that:

<receiver android:name="AlarmReceiver" android:process=":remote" />

最后一个属性 (process:remote) 导致接收器在被调用时在不同的/新进程上运行.但不同进程之间不支持 SharedPreferences.

The last attribute (process:remote) cause the receiver to run on a different/new process when it is called. But SharedPreferences is NOT supported between different processes.

所以我所做的是从清单中删除最后一个属性.这意味着代码现在将在主线程上运行 - 但如果您只有几行来显示通知,那么这应该不是问题.另一种方法是调用一个服务来运行长时间的操作.

So what I did is to remove that last attribute from the manifest. The implication is that the code will now run on the main thread - but if you only have a few lines to show a notification then that shouldn't be a problem. Another way is to call a service to run the long operation.

这篇关于BroadcastReceiver 中的 SharedPreferences 似乎没有更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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