Manually set a new alarm with setExactAndAllowWhileIdle() each time the alarm goes off: Android

569 views Asked by At

I know that this has been asked several times before, but I have not been able to find something that could work for me till now. I am using setRepeating() to schedule the alarm manager to run every minute. However, this has stopped working after the Android 6.0 Doze mode. I have also tried using the setExactAndAllowWhileIdle() but it doesn't not seem to be repeating as it is not setexactRepeating.

The below is my code,

PendingIntent service; // A pending intent object
Intent intentForService = new Intent(
    context.getApplicationContext(),
    ServiceToUpdateWidget.class);
final AlarmManager alarmManager = (AlarmManager) context
    .getSystemService(Context.ALARM_SERVICE);
final AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(System.currentTimeMillis(), service);
final Calendar time = Calendar.getInstance();
time.set(Calendar.MINUTE, 0);
time.set(Calendar.SECOND, 0);
time.set(Calendar.MILLISECOND, 0);

if (service == null) {
    service = PendingIntent.getService(context, 0,
        intentForService, PendingIntent.FLAG_CANCEL_CURRENT);
}

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),60000, service);

I need to call the ServiceToUpdateWidget every minute. Is there a way to do it even when the phone goes into a deep sleep or doze mode. Can I use a timer or count down for this purpose? Will thry work in the doze mode?

I have gone through many links like below,

However, I am still not able to call my service every one minute.

I have also tried using alarmManager.setAlarmClock(), still no luck.

I found a link that says : https://www.reddit.com/r/androiddev/comments/40ci7v/how_to_set_the_repeating_alarm_which_will_work/

"You manually have to set a new alarm with setExactAndAllowWhileIdle() each time the alarm goes off. However, there is a reason why no setInexactRepeatingAndAllowWhileIdle() exists: This will reduce battery life for your users. Only use this workaround if you really need it."

How can I manually set a new alarm when the previous alarm foes off? Please, can anyone help?

0

There are 0 answers