2015-01-17 114 views
4

我設置了每天重複的報警。 但它會有幾秒或幾分鐘的錯誤。 我怎樣才能讓它更準確?每天準確地重複報警(報警管理器)

PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), notificationId, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 12); 
    calendar.set(Calendar.MINUTE, 30); 
    long startUpTime = calendar.getTimeInMillis(); 
    if (System.currentTimeMillis() > startUpTime) { 
     startUpTime = startUpTime + 24*60*60*1000; 
    } 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24*60*60*1000 , pendingIntent); 
+0

是否'myIntent'指向一個'WakefulBroadc astReceiver'? – CommonsWare

回答

2

嘗試添加

calendar.set(Calendar.SECOND,00); 

和改變

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24*60*60*1000 , pendingIntent); 

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
      startUpTime, AlarmManager.INTERVAL_DAY, pendingIntent); 
+0

它的作品!謝謝你回答=) –

+0

它應該重複每一天? –

0
Intent myIntent = new Intent(ThisActivity.this , NotifyService.class);  
AlarmManager alarmManager = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE); 
PendingIntent pendingIntent = PendingIntent.getService(ThisActivity.this, 0, myIntent, 0); 
Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, 12); 
calendar.set(Calendar.MINUTE, 00); 
calendar.set(Calendar.SECOND, 00); 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);