我在特定时间收到通知,请参阅我的代码:
//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 响,但不晚点响吗?
Edit
对于遇到同样问题的人,解决方案如下:
将其放入您的广播接收器中:
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。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)