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

Fra*_*bek 4 android alarmmanager

即使该应用被终止或设备进入睡眠状态,我也需要在每个时间段运行一段代码。

我正在使用它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);
    }
}
Run Code Online (Sandbox Code Playgroud)

还有一个问题。这种呼叫服务的方法可靠吗?

这是日志

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
Run Code Online (Sandbox Code Playgroud)

该应用程序从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);
    }
Run Code Online (Sandbox Code Playgroud)

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

ear*_*jim 5

问题的原因很可能是打ze睡

从其有关的文档AlarmManager

为了帮助安排警报,Android 6.0(API级别23)引入了两种新AlarmManager方法:setAndAllowWhileIdle()setExactAndAllowWhileIdle()。使用这些方法,您可以设置即使设备处于打ze状态也将触发的警报。

您应该AlarmManager基于API级别调用适当的方法,以确保即使设备处于睡眠状态,警报也会响起:

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);
}
Run Code Online (Sandbox Code Playgroud)

请考虑以下因素:

每个应用程序每9分钟发出一次警报都setAndAllowWhileIdle()不会setExactAndAllowWhileIdle()超过一次。

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

华为(及其他制造商)已实现某些节电功能,可防止警报响起。您必须将您的应用添加到“受保护的”应用中。

检查以下问题:12

不建议以编程方式执行此操作,正确的解决方案是在启动时警告您的用户(AlertDialog例如使用),以在电池管理器中手动使您的应用“受保护”。