Android 6.0打ze模式下的警报管理器问题 [英] Alarm Manager issue in Android 6.0 Doze mode

查看:106
本文介绍了Android 6.0打ze模式下的警报管理器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个在Android 6.0之前一直可用的应用程序。我认为这是打ze功能,不允许我的警报发出。

I've made an app that always worked until Android 6.0. I think it's the Doze feature that it's not allowing my Alarm to fire.

我使用sharedpreferences处理选项:

I use sharedpreferences to handle the options:

//ENABLE NIGHT MODE TIMER
    int sHour = blockerTimerPreferences.getInt("sHour", 00);
    int sMinute = blockerTimerPreferences.getInt("sMinute", 00);

    Calendar sTime = Calendar.getInstance();
    sTime.set(Calendar.HOUR_OF_DAY, sHour);
    sTime.set(Calendar.MINUTE, sMinute);

    Intent enableTimer = new Intent(context, CallReceiver.class);
    enableTimer.putExtra("activate", true);
    PendingIntent startingTimer = PendingIntent.getBroadcast(context, 11002233, enableTimer, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager sAlarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    sAlarm.setRepeating(AlarmManager.RTC_WAKEUP,
            sTime.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, startingTimer);

这里有什么错误的线索吗?

Any clue of whats wrong here?

这是一个阻止通话的应用程序。谢谢!

This is an app to block calls. Thank you!

编辑:
我有3个文件(更多但...),例如:

I have 3 files (more but...) like:

MainActivity (All code)
CallReceiver (Broadcast that triggers the alarm again (reboot etc))
CallReceiverService (Handles the call / phone state)


推荐答案

打ze模式会将警报延迟到下一个维护窗口。为避免打ze模式阻止警报,可以使用 code> setAndAllowWhileIdle() setExactAndAllowWhileIdle() setAlarmClock() 。您将有大约10秒钟的时间来执行代码,并设置下一个警报(对于使用 _AndAllowWhileIdle 的方法,每15分钟不超过一次)

The Doze mode will delay your alarm until the next maintenance window. To avoid the Doze mode to block your alarm, you can use setAndAllowWhileIdle(), setExactAndAllowWhileIdle() or setAlarmClock(). You will have about 10s to execute your code, and set your next alarm (not more than once per 15min for methods with _AndAllowWhileIdle though)

如果要测试打ze模式,则可以使用 ADB命令

If you want to test the Doze mode, you can use ADB command:



  1. 配置硬件设备或虚拟设备具有Android 6.0(API级别23)或更高系统映像的设备。

  1. Configure a hardware device or virtual device with an Android 6.0 (API level 23) or higher system image.

将设备连接到开发计算机并安装应用。

Connect the device to your development machine and install your app.

关闭设备屏幕。 (该应用程序仍处于活动状态。)
通过运行以下命令来强制系统在打ze模式下循环:

Shut off the device screen. (The app remains active.) Force the system to cycle through Doze modes by running the following commands:

adb shell dumpsys battery拔下

adb shell dumpsys deviceidle步骤

您可能需要多次运行第二个命令。重复此操作,直到设备状态变为空闲。

You may need to run the second command more than once. Repeat it until the device state changes to idle.


编辑:添加setAlarmClock示例

别忘了检查SDK级别( Build.VERSION.SDK_INT> = Build.VERSION_CODES.LOLLIPOP

Don't forget to check the SDK level (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarmReceiver.class); //or just new Intent() for implicit intent 
//set action to know this come from the alarm clock
intent.setAction("from.alarm.clock");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//Alarm fire in 5s.
am.setAlarmClock(new AlarmManager.AlarmClockInfo(System.currentTimeMillis() + 5000, pi), pi);

这篇关于Android 6.0打ze模式下的警报管理器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆