2017-06-22 45 views
0

我想調度報警,應該在給定的時間每天觸發報警。他們應該100%一致。目前我使用Android報警調度高效

AlamManager.setInexactRepeating 

,它工作了一天,但不是第二天本身開始新的一天,我的意思是它並不一致。

那麼我應該在Android AlarmManager中使用哪種觸發器必須每天觸發並且應該高效?

+0

這裏是我的一個應用程序,它允許用戶選擇像迴文時間(10:01)和這種奇怪的事情一些「特殊時期」的通知。我使用了AlarmManager,請查看:https://github.com/magicleon94/SpasmoTime/tree/master/app/src/main/java/com/tomorrow/magicleon/spasmotime 在'updateAlarmService'中有一個'addAlarmFor '註冊每日重複報警的指定時間 – magicleon

+0

好吧,我會檢查它 – Danish

+0

請注意,代碼是非常類似於下面發佈的答案 – magicleon

回答

1

此代碼將在每天下午1點或2點運行Intent。希望能幫助你。

Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, 13); // For 1 PM or 2 PM 
calendar.set(Calendar.MINUTE, 0); 
calendar.set(Calendar.SECOND, 0); 
PendingIntent pi = PendingIntent.getService(context, 0, 
     new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT); 
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
          AlarmManager.INTERVAL_DAY, pi);