8

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

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

1 回答 1

2

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

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

但它需要 API 级别 19。

于 2014-09-21T06:10:44.517 回答