当设备在 Android 中处于睡眠状态时,AlarmManager 警报不起作用

AlarmManager alarm not working when the device is asleep in Android

我需要每个时间段都有一段代码运行,即使应用程序被杀死或设备休眠。

我正在使用 AlarmManager 来实现这个并且它有点工作,但是当设备休眠大约五分钟时 Service 不再被调用......有谁知道我是什么做错了??

这是我的代码:

public class Profiler extends IntentService {

    public static final int ALARM_MANAGER_ID = 21436587;

    public Profiler() {
        super("Profiler");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        JodaTimeAndroid.init(getApplicationContext());
        System.out.println("Testing profiles");
        List<Profile> profiles = MainActivity.getStoredProfiles(getApplicationContext());

        if (profiles != null) {
            for (Profile profile : profiles) {
                if(profile.check()) {
                    profile.set(getApplicationContext());
                }
            }
        }

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent intent2 = new Intent(getApplicationContext(), Profiler.class);
        PendingIntent pIntent = PendingIntent.getService(getApplicationContext(), ALARM_MANAGER_ID, intent2, PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, DateTime.now().plusSeconds(10).getMillis(), pIntent);
    }
}

还有一个问题。这种调用服务的方式靠谱吗??

这是日志

07-24 11:31:24.654 17689-19078/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}
07-24 11:31:34.671 17689-20364/cz.fjerabek.soundprofiler I/System.out: Testing profiles
07-24 11:31:34.673 17689-20364/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":11,"minute":15}
07-24 11:31:34.674 17689-20364/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}
07-24 11:31:44.692 17689-21571/cz.fjerabek.soundprofiler I/System.out: Testing profiles
07-24 11:31:44.693 17689-21571/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":11,"minute":15}
07-24 11:31:44.694 17689-21571/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}
07-24 11:31:54.704 17689-22753/cz.fjerabek.soundprofiler I/System.out: Testing profiles
07-24 11:31:54.705 17689-22753/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":11,"minute":15}
07-24 11:31:54.706 17689-22753/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}
07-24 11:32:04.717 17689-24015/cz.fjerabek.soundprofiler I/System.out: Testing profiles
07-24 11:32:04.718 17689-24015/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":11,"minute":15}
07-24 11:32:04.718 17689-24015/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}
07-24 11:35:29.032 17689-17475/cz.fjerabek.soundprofiler I/System.out: Testing profiles
07-24 11:35:29.040 17689-17475/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":11,"minute":15}
07-24 11:35:29.050 17689-17475/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}

                                                                       [ 07-24 11:35:29.056 17476:17476 I/         ]
                                                                       power log dlsym ok

应用程序在 11:31 启动,现在是 11:40,您可以看到代码的最后一次执行是在 11:35,之前是在 11:32它是 3 分钟,然后它似乎每 5 分钟被调用一次。

我正在启动服务:

Intent bgServiceIntent = new Intent(getApplicationContext(), Profiler.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        if(checkNotificationPolicy(getApplicationContext())) {
            startService(bgServiceIntent);
        }
    } else {
        startService(bgServiceIntent);
    }

我使用的设备:HUAWEI P9 lite (VNS-L21)

问题的原因很可能是 Doze

来自其关于 AlarmManager 的文档:

To help with scheduling alarms, Android 6.0 (API level 23) introduces two new AlarmManager methods: setAndAllowWhileIdle() and setExactAndAllowWhileIdle(). With these methods, you can set alarms that will fire even if the device is in Doze.

您应该根据 API 级别调用适当的 AlarmManager 方法,以确保即使在设备处于睡眠状态时闹钟也会响起:

if (Build.VERSION.SDK_INT >= 23) {
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
            alarmMillis, alarmIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
    alarmManager.setExact(AlarmManager.RTC_WAKEUP,
            alarmMillis, alarmIntent);
} else {
    alarmManager.set(AlarmManager.RTC_WAKEUP,
            alarmMillis, alarmIntent);
}

考虑以下因素:

Neither setAndAllowWhileIdle() nor setExactAndAllowWhileIdle() can fire alarms more than once per 9 minutes, per app.

正如您所说,您正在华为设备上进行测试。

华为(以及其他制造商)已经实施了某些节电功能,可以防止警报响起。您必须将您的应用程序添加到 "protected" 个应用程序。

检查以下问题:1 &

不建议以编程方式执行此操作,正确的解决方案可能是在启动时警告您的用户(例如 AlertDialog)在电池管理器中手动制作您的应用程序 "protected" .