Android 警报管理器无法使用特定的日期和时间
Android Alarm Manager not working with specific date and time
所以我一直在开发药物摄取应用程序,我需要在本地提醒用户(不需要 internet/push 通知)关于服用他们的药物。
为此,我正在使用 Android 警报管理器。下面是代码
请注意,我正在尝试为特定日期安排闹钟:“2018 年 7 月 13 日下午 3 点”。我安排并等待但提醒没有触发(所以没有广播)但是如果我使用 AlarmManager.ELAPSED_REALTIME_WAKEUP 并定义了毫秒数它会触发(但是 AlarmManager.RTC_WAKEUP 就是行不通)
`
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent myIntent;
PendingIntent pendingIntent;
long reminderDateTimeInMilliseconds = 000;
myIntent = new Intent(this,MedicationScheduleBroadCastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//TODO : Reminder the user to take medication on the 13th July 2018 at 15:30
Calendar calendarToSchedule = Calendar.getInstance();
calendarToSchedule.set(Calendar.YEAR, 2018);
calendarToSchedule.set(Calendar.MONTH, 07);
calendarToSchedule.set(Calendar.DAY_OF_MONTH, 13);
calendarToSchedule.set(Calendar.HOUR_OF_DAY, 15);
calendarToSchedule.set(Calendar.MINUTE, 30);
reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
}
else{
manager.set(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
}
`
如果设备处于 Doze mode,则不会触发警报。使用 setAndAllowWhileIdle
或 setExactAndAllowWhileIdle
也可以在打瞌睡模式下触发警报。
来自 Android Doc.
- 网络访问被暂停。
- 系统忽略唤醒锁。
- 标准 AlarmManager 警报(包括
setExact()
和 setWindow()
)是
推迟到下一次维护 window。如果您需要设置在打瞌睡时触发的警报,请使用 setAndAllowWhileIdle()
或 setExactAndAllowWhileIdle()
.
- 使用 setAlarmClock() 设置的警报会继续正常触发 — 系统会在这些警报触发前不久退出打盹模式。
- 系统不执行 Wi-Fi 扫描。
- 系统不允许同步适配器 运行。系统不允许 JobScheduler
运行.
所以我弄清楚了问题,主要问题是 Java 中的日历月实际上是基于 0 索引的,所以(Jan:0 - Dec:11)下面是更新后的代码.
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent myIntent;
PendingIntent pendingIntent;
long reminderDateTimeInMilliseconds = 000;
myIntent = new Intent(this,MedicationScheduleBroadCastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//TODO : Reminder the user to take medication on the 13th July 2018 at 15:30
// Note: For the month of July the int value will actuall be 6 instead of 7
Calendar calendarToSchedule = Calendar.getInstance();
calendarToSchedule.setTimeInMillis(System.currentTimeMillis());
calendarToSchedule.clear();
//.Set(Year, Month, Day, Hour, Minutes, Seconds);
calendarToSchedule.set(2018, 06, 13, 15, 30, 0);
reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
}
else{
manager.set(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
}
所以我一直在开发药物摄取应用程序,我需要在本地提醒用户(不需要 internet/push 通知)关于服用他们的药物。 为此,我正在使用 Android 警报管理器。下面是代码 请注意,我正在尝试为特定日期安排闹钟:“2018 年 7 月 13 日下午 3 点”。我安排并等待但提醒没有触发(所以没有广播)但是如果我使用 AlarmManager.ELAPSED_REALTIME_WAKEUP 并定义了毫秒数它会触发(但是 AlarmManager.RTC_WAKEUP 就是行不通) `
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent myIntent;
PendingIntent pendingIntent;
long reminderDateTimeInMilliseconds = 000;
myIntent = new Intent(this,MedicationScheduleBroadCastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//TODO : Reminder the user to take medication on the 13th July 2018 at 15:30
Calendar calendarToSchedule = Calendar.getInstance();
calendarToSchedule.set(Calendar.YEAR, 2018);
calendarToSchedule.set(Calendar.MONTH, 07);
calendarToSchedule.set(Calendar.DAY_OF_MONTH, 13);
calendarToSchedule.set(Calendar.HOUR_OF_DAY, 15);
calendarToSchedule.set(Calendar.MINUTE, 30);
reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
}
else{
manager.set(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
}
`
如果设备处于 Doze mode,则不会触发警报。使用 setAndAllowWhileIdle
或 setExactAndAllowWhileIdle
也可以在打瞌睡模式下触发警报。
来自 Android Doc.
- 网络访问被暂停。
- 系统忽略唤醒锁。
- 标准 AlarmManager 警报(包括
setExact()
和setWindow()
)是 推迟到下一次维护 window。如果您需要设置在打瞌睡时触发的警报,请使用setAndAllowWhileIdle()
或setExactAndAllowWhileIdle()
. - 使用 setAlarmClock() 设置的警报会继续正常触发 — 系统会在这些警报触发前不久退出打盹模式。
- 系统不执行 Wi-Fi 扫描。
- 系统不允许同步适配器 运行。系统不允许 JobScheduler 运行.
所以我弄清楚了问题,主要问题是 Java 中的日历月实际上是基于 0 索引的,所以(Jan:0 - Dec:11)下面是更新后的代码.
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent myIntent;
PendingIntent pendingIntent;
long reminderDateTimeInMilliseconds = 000;
myIntent = new Intent(this,MedicationScheduleBroadCastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//TODO : Reminder the user to take medication on the 13th July 2018 at 15:30
// Note: For the month of July the int value will actuall be 6 instead of 7
Calendar calendarToSchedule = Calendar.getInstance();
calendarToSchedule.setTimeInMillis(System.currentTimeMillis());
calendarToSchedule.clear();
//.Set(Year, Month, Day, Hour, Minutes, Seconds);
calendarToSchedule.set(2018, 06, 13, 15, 30, 0);
reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
}
else{
manager.set(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
}