Search code examples
androidemulationandroid-6.0-marshmallow

Android M (preview) Doze mode and AlarmManager


I'm trying to test the behavior of our Android app when the OS goes in Doze mode. I'm using an emulator running Android API 23 (Revision 1). The application launches a Service through the AlarmManager using the method setInexactRepeating with type ELAPSED_REALTIME_WAKEUP. I set the alarm to fire approximately every three minutes (only for testing purposes). After several attempts (the official guide is very unclear) I succeed to put the emulator in IDLE state by locking the screen of the emulator and running dumpsys suggested commands. When the device is IDLE I'm still able to see the Service being launched by the alarm. This should not be the expected behavior. I was expecting the Alarm be stopped. Is this a bug? Or am I missing something?


Solution

  • For test use code below.

    adb shell dumpsys deviceidle enable 
    adb shell dumpsys battery unplug
    adb shell dumpsys deviceidle step
    adb shell dumpsys deviceidle force-idle
    

    Use setAndAllowWhileIdle for force smartphone wakeup.

    In my case I use this:

    if (android.os.Build.VERSION.SDK_INT > 22) {
       am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
    } else {
       am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sender);
    }
    

    I believe that the expected is the Alarm be stopped.