Gro*_*oid 8 notifications android alarmmanager
我在特定时间收到通知,请参阅我的代码:
//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);
Run Code Online (Sandbox Code Playgroud)
除了一件事,这完美无缺.
通知设置为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
}
Run Code Online (Sandbox Code Playgroud)
小智 2
您也可以尝试警报管理器的这种方法:
public void setExact (int type, long triggerAtMillis, PendingIntent operation)
Run Code Online (Sandbox Code Playgroud)
但它需要 API 级别 19。
归档时间: |
|
查看次数: |
5394 次 |
最近记录: |