我有一个项目使用SQLite数据库在特定时间从数据库触发警报我在我的项目中使用android报警管理器并使用广播接收器在启动时管理报警这里是一个AlarmCode示例:
alarmIntent = PendingIntent.getBroadcast(context, ALARM_ID, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
//lollipop_mr1 is 22, this is only 23 and above
// alarmMgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, then.getMillis()-G.AzanDelayTime, alarmIntent);
alarmMgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, then.getMillis(), alarmIntent);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
//JB_MR2 is 18, this is only 19 and above.
//alarmMgr.setExact(AlarmManager.RTC_WAKEUP, then.getMillis()-G.AzanDelayTime, alarmIntent);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, then.getMillis(), alarmIntent);
}
else if (Build.VERSION.SDK_INT >= 21) {
alarmMgr.setAlarmClock(new AlarmManager.AlarmClockInfo(then.getMillis(), alarmIntent), alarmIntent);
Toast.makeText(G.context,"This is sdk>21",Toast.LENGTH_LONG).show();
}
else {
//available since api1
//alarmMgr.set(AlarmManager.RTC_WAKEUP, then.getMillis()-G.AzanDelayTime, alarmIntent);
alarmMgr.set(AlarmManager.RTC_WAKEUP, then.getMillis(), alarmIntent);
但是我希望在每个设备的确切时间发出警报,在某些情况下警报会在15分钟或更短的时间内关闭,这个问题的最佳解决方案是什么?
你可以使用AlarmManagerCompat:
AlarmManagerCompat.setExactAndAllowWhileIdle(...)
要么
AlarmManagerCompat.setAlarmClock(...)