2017-04-05 44 views
-1

我想做一個重複的警報通知,在一天的特定時間內觸發。我發出的重複報警很好地發出了警報,但有時在設定的時間內關閉。例如,我在上午6點設置了鬧鐘,但在第二天第一次觸發後,它開始於上午6點15分,所以我想知道它們是否是更準確的鬧鐘設置方式?如何使重複報警更準確?

這是我的用於我的重複報警

 Calendar calendar_dog= Calendar.getInstance(); 
         calendar_dog.set(Calendar.HOUR_OF_DAY,7); 
         calendar_dog.set(Calendar.MINUTE,0); 
         calendar_dog.set(Calendar.SECOND,0); 
         Intent intent= new Intent(getApplicationContext(), notify_dog.class); 
         PendingIntent dog_intent=PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT); 
         AlarmManager dog_alarm =(AlarmManager)getApplicationContext().getSystemService(ALARM_SERVICE); 
         dog_alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar_dog.getTimeInMillis(),AlarmManager.INTERVAL_DAY,dog_intent); 

和我reciever

NotificationManager notify_dog=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent dog_intent= new Intent(context, HomeActivity.class); 
    dog_intent.setFlags(dog_intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent dog_pending=PendingIntent.getActivity(context,100,dog_intent,PendingIntent.FLAG_UPDATE_CURRENT); 
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder dog_build= new NotificationCompat.Builder(context).setContentIntent(dog_pending). 
      setSmallIcon(R.drawable.pet) 
      .setContentTitle("Pet Guide 101") 
      .setContentText("have you feed your Dog?") 
      .setSound(alarmSound) 
      .setAutoCancel(true); 
    notify_dog.notify(100,dog_build.build()); 

回答

1

Android Developers代碼:

如上所述,選擇報警類型往往是創建鬧鐘的第一步。另一個區別是你需要你的鬧鐘有多精確。對於大多數應用程序,setInexactRepeating()是正確的選擇。當您使用此方法時,Android會同步多個不精確的重複警報並同時觸發它們。這可以減少電池的消耗。

對於具有嚴格時間要求的罕見應用程序 - 例如,警報需要在上午8:30準確着火,然後每小時使用setRepeating()。但是如果可能的話,你應該避免使用精確的警報

使用setInexactRepeating(),您無法像使用setRepeating()一樣指定自定義間隔。您必須使用其中一個間隔常量,例如INTERVAL_FIFTEEN_MINUTES,INTERVAL_DAY等。請參閱AlarmManager以獲取完整列表。

所以,來完成你想要什麼,你需要使用它這樣的:

dog_alarm.setRepeating(AlarmManager.RTC_WAKEUP,calendar_dog.getTimeInMillis(),AlarmManager.INTERVAL_DAY,dog_intent); 
+0

哦確定生病試試吧謝謝你.. –

+0

@ tyreklGana,一個沒有工作的呢?如果是,請檢查我的解決方案是否已接受。 –