2017-03-19 37 views
0

爲什麼AlarmManager有時無法正好5分鐘後執行,因爲它應該是?例如,一次它可能不起作用,但在下一個時期它工作2(或3)次。甚至滯後1分鐘。爲什麼AlarmManager在設備上沒有完全重複?

API 17(Android 4.2.1) - setRepeating應該是確切的。

public void scheduleAlarm() 
    { 
     Intent intent = new Intent(getApplicationContext(), TestAlarmReceiver.class); 
     final PendingIntent pIntent = PendingIntent.getBroadcast(this, TestAlarmReceiver.REQUEST_CODE, 
       intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     long firstMillis = System.currentTimeMillis(); 
     AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
     alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 5 * 60 * 1000, pIntent); // every 5 minutes 

     Log.i(TAG_LOG, "The alarm started"); 
    } 

日誌:

<...> 
17.03.2017 07:20:30 
17.03.2017 07:25:30 
17.03.2017 07:30:30 
17.03.2017 07:40:25 <- this 
17.03.2017 07:40:30 <- this 
17.03.2017 07:45:30 
17.03.2017 07:50:30 
17.03.2017 07:55:30 
17.03.2017 08:00:30 
17.03.2017 08:10:25 <- this 
17.03.2017 08:10:30 <- this 
17.03.2017 08:15:30 
17.03.2017 08:20:30 
<...> 
17.03.2017 16:50:30 
17.03.2017 16:55:30 
17.03.2017 17:01:10 <- this 
17.03.2017 17:05:30 
17.03.2017 17:10:30 
17.03.2017 17:15:30 
17.03.2017 17:21:08 <- this 
17.03.2017 17:25:30 
17.03.2017 17:40:29 <- this 
17.03.2017 17:40:29 <- this 
17.03.2017 17:40:30 <- this 
17.03.2017 17:45:30 
17.03.2017 17:51:09 <- this 
17.03.2017 17:55:30 
17.03.2017 18:00:50 
<...> 

回答

0

setRepeating()不應該工作,你想要的方式。

setExact()觸發在準確的時間報警,但因爲沒有setExactRepeating()方法,你需要使用setExact(),並在BroadcastReceiver,再次調用setExact()功能。

有沒有簡單的方法來做到這一點,因爲谷歌正試圖指出,這是對電池不好。如上所述使用setExact()是您確切重複的唯一選項。

+0

是的,但我提到我有** API 17 **。沒有'setExact()'。 –

相關問題