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

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

问题描述

我有一个活动而更新一个字符串中的共享preferences。

 共享preferences设置= preferenceManager.getDefaultShared preferences(本);
共享preferences.Editor编辑器= settings.edit();
editor.putString(用户名,用户名);
editor.commit();
 

我再启动服务:

  startService(新意图(这一点,MyService.class));
 

服务营造一个参考报警延伸BroadcastReceiver的:

 报警报警= NULL;
公共无效的onCreate(){
    报警=新的警报();
}
@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
    alarm.SetAlarm(本);
}
 

在SetAlarm我做的所有基本建立的东西(在这一点上,用户名仍然是正确的。我选中):

 公共无效SetAlarm(上下文的背景下){
    AlarmManager AM =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent圆周率= PendingIntent.getBroadcast(上下文,0,I,0);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(),1000 * 60 *间隔,PI);
}
 

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

 公共无效CancelAlarm(上下文的背景下){
   意向意图=新的意图(背景下,Alarm.class);
   PendingIntent发件人= PendingIntent.getBroadcast(上下文,0,意图,0);
   AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
   alarmManager.cancel(寄件人);
}
 

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

 公共无效的onReceive(上下文的背景下,意图意图){
    共享preferences设置= preferenceManager.getDefaultShared preferences(上下文);
    Log.e(喜,settings.getString(用户名,));
}
 

解决方案

我有同样的问题,并争取时间去解决它之后,我终于发现了问题,导致它

。在您的Andr​​oidManifest你可能有这样的事情:

 <接收器的Andr​​oid版本:NAME =AlarmReceiver安卓程序=:远程/>
 

最后attribue(过程:远程)造成reciver到一个不同的/新的进程中运行,当它被调用。但是,共享preferences是不是不同进程之间的支持。

所以,我所做的就是从清单中删除这最后一个属性。言下之意是,code现在在主线程中运行 - 但如果你只有几行,以显示一个通知,然后不应该不成问题。另一种方法是调用服务来运行长时间操作。

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();

I then start a service:

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);
}

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);
}

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);
}

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", ""));
}

解决方案

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" />

The last attribue (process:remote) cause the reciver 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 this 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 not be a problem. Another alternative is to call a service to run the long operation.

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

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