Alarm Manager won't work when the app is killed in the background and device is locked

我怕爱的太早我们不能终老 提交于 2019-12-04 11:22:00

I just found the solution, set flag named FLAG_INCLUDE_STOPPED_PACKAGES to intent of the alarm, things will go right. Here are the illustration in Android Developers

Sebastian Breit

I had a similar issue one day and found a solution online, which was wakelocking the phone when the broadcastreceiver was called. So try creating this class, and calling MyWakeLock.acquire() when the broadcastreceiver gets called. You should also call MyWakeLock.release() when you are done executing whatever your alarm does.

package your.package.name;

import android.content.Context;
import android.os.PowerManager;

public abstract class MyWakeLock {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context c) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) c.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, MainActivity.APP_TAG);
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null){
            wakeLock.release();
        }
        wakeLock = null;
    }
}

i had this problem and finally found wrong targetSdkVersion is above 22 and no handle permission. set targetSdkVersion 22 and worked ^_^

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