Repeat Alarm Manager At Exact Interval in API=>19?

こ雲淡風輕ζ 提交于 2019-11-27 22:34:16

You can use setExact similarly to setRepeating.

void scheduleAlarm(Context context) {
    AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent yourIntent = new Intent();
    //TODO configure your intent
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, MyApplication.ALARM_REQUEST_CODE, yourIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmMgr.setExact(AlarmManager.RTC_WAKEUP, timeToWakeUp, alarmIntent);
}

The two differences are:

  1. The timing will be exact (or as nearly as possible)
  2. You will have to schedule the next occurrence in the onReceive of your BroadcastReceiver.

      public class AlarmReceiver extends BroadcastReceiver  {
      @Override
      public void onReceive(Context context, Intent intent) {
        //TODO process alarm
        scheduleAlarm(context);
      }}
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!