Android SharedPreferences为两个不同的类

时间:2016-05-26 10:55:18

标签: android sharedpreferences

我想为android服务使用一个共享计数器(int)。它从两个不同的接收器调用,递增并放回。在第一次调用时,两个计数器具有相同的值。然而,在那之后,每个人都被自己攻击。我找不到问题。

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

第二接收者:

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

通常,它应该是这样的:

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 个答案:

没有答案