通过使用setExact()在每天的特定时间触发警报

时间:2018-11-09 08:59:57

标签: android alarmmanager

我每天尝试在特定时间触发一次警报。但是,像其他所有人一样,我使用的是setExact()而不是setRepeating()。警报在正确的时间触发。但是一旦触发,它就会每5秒重复一次。如何每天在特定时间一天触发一次?这是我的代码:

MainActivity.java:

public class MainActivity extends AppCompatActivity implements Observer {

    private AlarmManager alarmManager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BroadcastObserver.getInstance().addObserver(this);
    }

    @Override
    public void update(Observable o, Object arg) {
        Log.e("MainActivity", "Alarm set through observer");
        cancelAlarm();
        setAlarm();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!checkAlarm()) {
            setAlarm();
        }

    }

    public void setAlarm() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 14);
        calendar.set(Calendar.MINUTE, 13);
        calendar.set(Calendar.SECOND, 0);
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, MyReceiver.class);
        intent.setAction(MyReceiver.ACTION_RECEIVER);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }

    private boolean checkAlarm() {
        Intent intent = new Intent(this, MyReceiver.class);
        intent.setAction(MyReceiver.ACTION_RECEIVER);
        boolean isSet = PendingIntent.getBroadcast(this, 1001, intent, PendingIntent.FLAG_NO_CREATE) != null;
        Log.e("MainActivity", isSet + " :Alarm is set");
        return isSet;
    }

    @Override
    protected void onStop() {
        super.onStop();
        cancelAlarm();
    }

    private void cancelAlarm() {
        Intent intent = new Intent(this, MyReceiver.class);
        intent.setAction(MyReceiver.ACTION_RECEIVER);
        final PendingIntent pendingIntent =
                PendingIntent.getBroadcast(MainActivity.this, 1001, intent,
                        PendingIntent.FLAG_NO_CREATE);
        if (pendingIntent != null) {
            alarmManager.cancel(pendingIntent);
            pendingIntent.cancel();
        }
    }
}

BroadcastReceiver:

public class MyReceiver extends BroadcastReceiver {

    public static final String ACTION_RECEIVER = "Receiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("MainActivity", "triggered");
        BroadcastObserver.getInstance().updateValue(intent);
    }
}

BroadcastObserver:

public class BroadcastObserver extends Observable {
    private static BroadcastObserver instance = new BroadcastObserver();

    public static BroadcastObserver getInstance(){
        return instance;
    }

    private BroadcastObserver(){}

    public void updateValue(Object data) {
        synchronized (this) {
            setChanged();
            notifyObservers(data);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

一旦触发,每5秒就会重复一次

您正在为给定的一天设置14:13警报。警报触发时,Observer会收到通知,并会设置准确的相同警报(针对14:13)。

这时我们已经过了14:13,所以警报将立即触发,再次通知Observer,导致上述步骤无限循环。

最简单的解决方案可能是检查设置警报的时间以及过去的时间,然后再添加一天:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 13);
calendar.set(Calendar.SECOND, 0);

if (calendar.before(Calendar.getInstance())) {
    calendar.add(Calendar.DAY_OF_YEAR, 1);
}

为避免内存泄漏,您需要在适当的时候删除Observer

@Override
protected void onDestroy() {
    BroadcastObserver.getInstance().deleteObserver(this);
    super.onDestroy();
}

此外,取消update()中的警报也是多余的。这不是重复的警报,因此在触发后取消警报是没有意义的。