Alarm Manager not working at specific given time interval

寵の児 提交于 2019-12-05 08:28:15

Don't use setInexactRepeating. This, as the name suggests, doesn't schedule the alarm to go off at an exact time.

You can use something like this:

public void scheduleSingleAlarm(Context context) {
    Intent intent = new Intent(context, NotificationReceiver.class);
    PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(context,
            SINGLE_ALARM_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    Calendar futureDate = Calendar.getInstance();
    futureDate.add(Calendar.MINUTE, 3);

    setSingleExactAlarm(futureDate.getTime().getTime(), pendingUpdateIntent);
}

@SuppressLint("NewApi")
private void setSingleExactAlarm(long time, PendingIntent pIntent) {
    if (android.os.Build.VERSION.SDK_INT >= 19) {
        mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pIntent);
    } else {
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, pIntent);
    }
}

When you receive a call from the alarm, schedule another alarm to go off in another three minutes. I've blogged about this here

You can see from the official documentation for AlarmManager that starting from Android version 19 (KitKat), alarms scheduled will be inexact even if they were marked exact (although you already have inexactRepeating()).

The alarms will get bunched with system events to minimize device wake up and battery use which is what you are seeing. This is by design.

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