我制作了一个一直可以运行到Android 6.0的应用程序。我认为这是打ze功能,不允许我的闹铃触发。

我使用sharedpreferences处理选项:

//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);

这里有什么不对的线索吗?

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

编辑:
我有3个文件(更多但...),例如:
MainActivity (All code)
CallReceiver (Broadcast that triggers the alarm again (reboot etc))
CallReceiverService (Handles the call / phone state)

最佳答案

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

如果要测试打ze模式,可以使用ADB command:



编辑:添加setAlarmClock示例

不要忘记检查SDK级别( 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);

10-07 22:29