Alternatives for AlarmManager setRepeating in API 19 and above?

岁酱吖の 提交于 2019-12-06 05:36:14

Is there any workaround to get API 19's setExact method to work on a loop?

Sure. Have your BroadcastReceiver, or whatever is getting control from the alarm, call setExact() to schedule the next recurrence, in addition to doing its existing work.

Bear in mind, though, that the changes to background processing in Android M may cause you difficulty.

If Google provides a setExactRepeating() method, it may solve your problem. (It's a joke)

For now, you have to reschedule the alarm by yourself.

  1. use the setExact() method to schedule the alarm for the first time, for example:

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);

  2. reschedule the alarm in the onReceiver() of your BroadcastReceiver, the repeating time is a day(AlarmManager.INTERVAL_DAY in the following code):

    public void onReceive(Context context, Intent intent){

        Context danielContext = MonitorApp.getContext();
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, ReceiverName.class);//put your own ReceiverName
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, i, 0);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_DAY, pendingIntent);
    

    }

Of course, do something in the onReceiver() method to achieve your aim.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!