设备处于睡眠状态时,防止警报响起

时间:2014-04-04 18:09:26

标签: android

我设置了一个重复闹钟,我只想在设备醒来时关机。当它处于睡眠状态时,我希望它停止(并在设备唤醒时重新开启)。然而,无论如何,它目前都会消失。这是我如何注册我的警报:

    Intent updateIntent = new Intent(UPDATE_INTENT);
    AlarmManager alarmService = (AlarmManager) context.getSystemService(
            Context.ALARM_SERVICE);
    PendingIntent updatePendingIntent = PendingIntent.getBroadcast(context, 0,
            updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmService.setInexactRepeating(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis(), UPDATE_INTERVAL, updatePendingIntent);

警报管理器文档说RTC不会唤醒设备。文档完全指定了我想要的行为:

Alarm time in System.currentTimeMillis() (wall clock time in UTC). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up.

当我按下设备上的锁定按钮时,我清楚地看到logcat中来自PowerManager的进入睡眠消息:

I/PowerManagerService(488): Going to sleep by user request...

然后我的闹钟响了。这里发生了什么?

具有讽刺意味的是,我在SO上发现的所有其他问题都会在设备处于睡眠状态时处理警报。我希望我有他们的问题!!

2 个答案:

答案 0 :(得分:1)

  

然而,无论如何,它目前都会消失。

大概是其他东西拿着部分WakeLock,并且设备实际上并没有睡着,即使屏幕可能已关闭。使用 adb shell dumpsys power 尝试跟踪它(查找"唤醒锁定"部分)。

答案 1 :(得分:0)

我最终决定注册广播接收器以侦听SCREEN_ON和SCREEN_OFF,并适当地切换警报。我意识到这可能不是超级优雅,但至少它总是有效,即使另一个应用程序正在进行唤醒锁定。

开启和关闭屏幕:android: broadcast receiver for screen on and screen off

关闭闹钟:How to stop an alarm in android

相关问题