2014-04-04 39 views
0

我設置了一個重複報警,我只想在設備醒來時關閉。當它睡着時,我希望它停止(並在設備醒來時再回來)。但是,無論如何,它現在都會關閉。以下是我如何註冊我的鬧鐘:設備睡着時防止報警發生

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上發現的每一個其他問題都會在設備處於睡眠狀態時警報不起作用。我希望我有他們的問題!

+0

什麼是'UPDATE_INTERVAL'的價值? – codeMagic

+0

出於測試目的,我在10秒鐘(10000)。但是在製作過程中會有幾個小時。 – Adam

+0

你有沒有試過用2或3分鐘測試它?值得嘗試排除。 – codeMagic

回答

1

但是,無論如何,它現在都會關閉。

想必別的持有的部分WakeLock,設備實際上不是睡着了,儘管屏幕可能會關閉。使用adb shell dumpsys power嘗試追蹤它(查找「Wake Locks」部分)。

+0

這裏的激活鎖定部分: 喚醒鎖定:大小= 0 暫停阻滯劑:大小= 2 PowerManagerService:引用計數= 0 PowerManagerService.Broadcasts:引用計數= 0 – Adam

+0

@Adam:嗯......據我所知,在「REF計數= 0「意味着這些暫停阻止者不會讓設備保持清醒狀態。你能否在另一個環境中嘗試你的代碼,比如模擬器? – CommonsWare