[英]Notification at specific time
我在特定時間收到通知,請參閱我的代碼:
//Create alarm manager
AlarmManager alarmMgr0 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//Create pending intent & register it to your alarm notifier class
Intent intent0 = new Intent(this, AlarmReceiver_maandag_1e.class);
intent0.putExtra("uur", "1e");
PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0, intent0, 0);
//set timer you want alarm to work (here I have set it to 8.30)
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 8);
timeOff9.set(Calendar.MINUTE, 30);
timeOff9.set(Calendar.SECOND, 0);
//set that timer as a RTC Wakeup to alarm manager object
alarmMgr0.set(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), pendingIntent0);
這很完美,除了一件事。
使用警報管理器將通知設置為8:30。如果我在8:30(例如9:00)之后啟動應用,則通知仍會顯示。
警報是否有可能在8:30響起,但不會遲到?
編輯
對於有相同問題的任何人,以下是解決方案:
將其放在您的廣播接收器中:
long current_time = System.currentTimeMillis();
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 8);
timeOff9.set(Calendar.MINUTE, 35);
timeOff9.set(Calendar.SECOND, 0);
long limit_time = timeOff9.getTimeInMillis();
if (current_time > limit_time) {
//nothing
} else {
//show notification
}
您也可以嘗試以下警報管理器方法:
public void setExact (int type, long triggerAtMillis, PendingIntent operation)
但它需要API級別19。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.