Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Alarm Manager setExactAndAllowWhileIdle() not working in Android 7.0 Nougat in Doze mode

Tags:

I am trying to make an alarm fire in my app every 30 minutes using Alarm Manager's setExactAndAllowWhileIdle but it is not working!

I test the functionality by issuing a push notification whenever I receive an alarm signal.

The problem is: when the device enters doze mode after being idle for sometime, I no longer receive alarms. However, as soon as I turn On the screen, I receive a notification. My app needs accurate alarms that need to be delivered exactly on-time every 30 minutes! It can not afford to receive delayed alarms or dropped ones because the device is in Doze mode!

I used the following in my code:

  1. I set the alarm when I open my app.
  2. I receive the alarm signal using a WakefulBroadcastReceiver. In its onReceive() method I set the next alarm. I also, start a startWakefulService that only issues a push notification, then stops itself.
  3. I call completeWakefulIntent at the end of the onReceive().
  4. I tried testing both: RTC_WAKEUP & ELAPSED_REALTIME_WAKEUP

Notes:

  • The wakefulbroadcastReceiver class is registered in the Manifest.
  • I added permissions for : android.permission.WAKE_LOCK
  • I tried White-Listing my app, but the results are the same
  • I tried using setAlarmClock() which worked all the time even during doze mode, with one dropped/delayed alarm every 50 alarms. So, it is also not perfect. And I don't want the user to see an alarm icon all the time up there.
  • Not only does setExactAndAllowWhileIdle() not work during doze, but also it has terrible accuracy when it is working. I usually get lots of alarm signals
    either 1-3 minutes later or 1-3 minutes earlier.
  • I am testing using Huawei Mate 8, and android 7.0 Nougat.

P.S: Before answering please make sure you are aware of the restrictions imposed starting Android 6.0 M and Doze mode.

Link1: https://developer.android.com/training/monitoring-device-state/doze-standby.html

In summary it says this:

  • If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
  • Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.

Now, why don't I get accurate alarm signals every 30 minutes using setExactAndAllowWhileIdle()?! And, why isn't setAlarmClock() 100% reliable?!

like image 929
Mena Avatar asked Jul 13 '17 11:07

Mena


People also ask

How do I set alarm manager on android?

This example demonstrates how do I implement alarm manager in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you turn off alarm on alarm Manager android?

In order to delete : AlarmManager alarmManager = (AlarmManager) getSystemService(Context. ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver. class); PendingIntent pendingIntent = PendingIntent.

How do I turn off alarm manager?

You can cancel the alarm like this: Intent intent = new Intent(this, Mote. class); PendingIntent pendingIntent = PendingIntent. getBroadcast(getApplicationContext(), 1253, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.

What are alarms in android?

Alarms in Android have the following characteristics: Alarms let you send intents at set times or intervals. You can use alarms with broadcast receivers to start services and perform other operations.


1 Answers

You might get accurate alarms, but in Doze mode

The system ignores wake locks.

So it seems as though AlarmManager.setAlarmClock is the only acceptable solution if you really need to trigger every 30 minutes. This will probably negate all doze mode energy savings...

BTW: it seems like you can see alarms with adb shell dumpsys alarm.


Possibility: use Firebase JobDispatcher

The Firebase JobDispatcher is a library for scheduling background jobs in your Android app. It provides a JobScheduler-compatible API that works on all recent versions of Android (API level 14+) that have Google Play services installed.

like image 185
serv-inc Avatar answered Sep 21 '22 04:09

serv-inc