因此,我一直在研究药物摄入应用程序,我需要在本地提醒用户(无需互联网/推送通知)有关服用药物的信息。 我正在为此使用Android警报管理器。下面是代码 请注意,我正在尝试将警报安排在特定日期:“ 2018年7月13日下午3点30分” 。我安排并等待,但提醒未触发(因此未广播),但是如果我使用 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);
}
`
答案 0 :(得分:1)
所以我弄清楚了这个问题,主要的问题是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);
}
答案 1 :(得分:0)
如果设备位于Doze mode中,则不会触发警报。使用setAndAllowWhileIdle
或setExactAndAllowWhileIdle
也可以在打ze模式下触发警报。
来自Android Doc。
setExact()
和setWindow()
)为
推迟到下一个维护窗口。如果您需要设置在打ze睡时触发的警报,请使用setAndAllowWhileIdle()
或setExactAndAllowWhileIdle()
。