通知在特定时间(Notification at specific time)

2019-08-18 15:00发布

我有在特定时间的通知,请参阅我的代码:

//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
}

Answer 1:

您也可以尝试报警管理器的这个方法:

public void setExact (int type, long triggerAtMillis, PendingIntent operation)

但requiers API等级19。



文章来源: Notification at specific time