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

后端 未结 1 1987
太阳男子
太阳男子 2020-12-06 03:43

I had a massive reading and still I think there is not a clear/complete Answer to this question.

First some stuff to clarify : this question is not concern with batt

相关标签:
1条回答
  • 2020-12-06 04:14

    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);
        }}
      
    0 讨论(0)
提交回复
热议问题