I want to use one shared counter (int) for an android service. It's called from two different receivers, incremented and put back. At first call, both counters have the same value. However, after that, each one is incemented by itself. I couldn't find the problem.
FirstReceiver:
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();
}
}
Second Receiver:
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();
}
}
Output log:
hatirlatici I/Unlock: Counter: 234
hatirlatici:remote I/Alarm: Counter: 234
hatirlatici:remote I/Alarm: Counter: 235
hatirlatici:remote I/Alarm: Counter: 236
hatirlatici:remote I/Alarm: Counter: 237
hatirlatici I/Unlock: Counter: 235
hatirlatici:remote I/Alarm: Counter: 238
hatirlatici:remote I/Alarm: Counter: 239
hatirlatici:remote I/Alarm: Counter: 240
hatirlatici I/Unlock: Counter: 236
Normally, it should be like this:
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
Note: New to Android