Android SharedReferences在两个不同的类中为同一个键返回两个不同的值
我想为android服务使用一个共享计数器(int)。它是从两个不同的接收器调用的,递增和放回。第一次调用时,两个计数器的值相同。然而,在那之后,每一个都是由它自己激励的。我找不到问题 第一接收人:Android SharedReferences在两个不同的类中为同一个键返回两个不同的值,android,sharedpreferences,Android,Sharedpreferences,我想为android服务使用一个共享计数器(int)。它是从两个不同的接收器调用的,递增和放回。第一次调用时,两个计数器的值相同。然而,在那之后,每一个都是由它自己激励的。我找不到问题 第一接收人: public class UserPresentBroadcastReceiver extends BroadcastReceiver { public int getCounter() { return counter; } public void se
public class UserPresentBroadcastReceiver extends BroadcastReceiver {
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
private int counter=0;
@Override
public void onReceive(Context context, Intent intent) {
DataSource dataSource = new DataSource(context);
dataSource.open();
List<Note> notes = new ArrayList<>();
notes.addAll(dataSource.findAllActiveNotes());
SharedPreferences counterP = context.getSharedPreferences(HatirlaticiService.COUNTER_LABEL, Context.MODE_PRIVATE);
counter = counterP.getInt(HatirlaticiService.COUNTER_LABEL,0);
setCounter(counter);
if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
Toast toast = Toast.makeText(context, notes.get(counter%notes.size()).getName(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
counter++;
SharedPreferences.Editor editor = counterP.edit();
editor.putInt(HatirlaticiService.COUNTER_LABEL, counter);
editor.apply();
Log.i("Unlock:", "Counter: "+counter);
}
/*Device is shutting down. This is broadcast when the device
* is being shut down (completely turned off, not sleeping)
* */
else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
}
dataSource.close();
}
}
public class Alarm extends BroadcastReceiver {
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
private int counter = 0;
@Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
SharedPreferences counterP = context.getSharedPreferences(HatirlaticiService.COUNTER_LABEL, 0);
counter = counterP.getInt(HatirlaticiService.COUNTER_LABEL,0);
setCounter(counter);
DataSource dataSource = new DataSource(context);
dataSource.open();
List<Note> notes = new ArrayList<>();
notes.addAll(dataSource.findAllActiveNotes());
Toast toast = Toast.makeText(context, notes.get(counter%notes.size()).getName(),Toast.LENGTH_LONG);
toast.show();
counter++;
SharedPreferences.Editor editor = counterP.edit();
editor.putInt(HatirlaticiService.COUNTER_LABEL, counter);
editor.apply();
Log.i("Alarm", "Counter: "+counter);
dataSource.close();
wl.release();
}
}
通常情况下,应该是这样的:
hatirlatici:remote I/Alarm: Counter: 234
hatirlatici:remote I/Alarm: Counter: 235
hatirlatici I/Unlock: Counter: 236
hatirlatici:remote I/Alarm: Counter: 237
hatirlatici:remote I/Alarm: Counter: 238
hatirlatici I/Unlock: Counter: 239
注意:Android新增功能在您收到的日志中,您可以向上滚动查看开始。它应该从0开始。和cchange editor().apply()到editor.commit()并查看计数器是否按预期打印。在多个进程中使用
SharedReferences
可能会出现问题。你可以参考。
hatirlatici:remote I/Alarm: Counter: 234
hatirlatici:remote I/Alarm: Counter: 235
hatirlatici I/Unlock: Counter: 236
hatirlatici:remote I/Alarm: Counter: 237
hatirlatici:remote I/Alarm: Counter: 238
hatirlatici I/Unlock: Counter: 239