Alarm Manager does not work in background on Android 6.0

霸气de小男生 提交于 2019-11-27 20:45:31
Y.S

There are a couple of things you can try which, when used in concert, should be able to cut through all of the idle/standby/doze modes (irrespective of OS version).

1. Use a WakefulBroadcastReceiver instead of an ordinary BroadcastReceiver. Make sure you include the WAKE_LOCK permission to use it correctly.

2. Use the setExactAndAllowWhileIdle() method (on API 23 & above) to schedule the Intent for the receiver:

if(Build.VERSION.SDK_INT < 23){
    if(Build.VERSION.SDK_INT >= 19){
        setExact(...);
    }
    else{
        set(...);
    }
}
else{
    setExactAndAllowWhileIdle(...);
}

References:

1. Alarm manager for background services.

2. A flowchart for background work, alarms, and your Android app.

Shahzeb

The reason can be Doze mode introduced in Android 6.0 Marshmallow. Use setAlarmclock() instead of set(). It's designed specifically for alarm clocks and may be able to cut through doze. There are a few adb commands you can use to manually put a phone into doze or app standby mode:

https://developer.android.com/preview/features/power-mgmt.html

If that isn't able to wake your app up, there's the surefire method setExactAndAllowWhileIdle() is designed to wake the phone from doze no matter what. Worst case scenario, you can wake your app up with this method and use the wakeup to schedule the next alarm.

Reference: How to make Alarm Manager work when Android 6.0 in Doze mode?

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