我想每天早上6点开始闹钟。我写了下面的代码,但是在致电服务部门后立即拨打了我的警报电话,没有保留并设置为上午6点。我怎么了我在广播接收器中将此方法称为“方法”,并每秒重复调用一次!如果我将代码更改为以下代码,则警报会按时正常运行。
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, new Date().getTime()+2*60*1000, monitoringSchedulePI);
private void setSingleExactMonitoringSchedule() {
final AlarmManager alarmManager = (AlarmManager) KITILApplication.getappContext().getSystemService(Context.ALARM_SERVICE);
Date date = new Date();
Log.e("MonitoringScheduleDate:", "Broadcast:" + date.toString());
Intent monitoringScheduleIntent = new Intent(KITILApplication.getappContext(), AlarmManagerBroadcastReciever.class);
monitoringScheduleIntent.putExtra(AppConstant.REQUEST_CODE_RECEIVER, AppConstant.REPEAT_GET_SCHEDULE);
PendingIntent monitoringSchedulePI = PendingIntent.getBroadcast(KITILApplication.getappContext(), OfflineData.getUniqueIdMonitoringSchedule(KITILApplication.getappContext()), monitoringScheduleIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,6);
calendar.set(Calendar.MINUTE,0);
long trigger=calendar.getTimeInMillis();
if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, trigger, monitoringSchedulePI);
} else if (Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,trigger, monitoringSchedulePI);
}else{
alarmManager.set(AlarmManager.RTC_WAKEUP, trigger, monitoringSchedulePI);
}
}
答案 0 :(得分:0)
使用以下代码。它将起作用。
private void setSingleExactMonitoringSchedule() {
final AlarmManager alarmManager = (AlarmManager) KITILApplication.getappContext().getSystemService(Context.ALARM_SERVICE);
Date date = new Date();
Log.e("MonitoringScheduleDate:", "Broadcast:" + date.toString());
Intent monitoringScheduleIntent = new Intent(KITILApplication.getappContext(), AlarmManagerBroadcastReciever.class);
monitoringScheduleIntent.putExtra(AppConstant.REQUEST_CODE_RECEIVER, AppConstant.REPEAT_GET_SCHEDULE);
PendingIntent monitoringSchedulePI = PendingIntent.getBroadcast(KITILApplication.getappContext(), OfflineData.getUniqueIdMonitoringSchedule(KITILApplication.getappContext()), monitoringScheduleIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,6);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.DATE, 1);/*call from next day*/
long trigger=calendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()
, AlarmManager.INTERVAL_DAY, monitoringSchedulePI);
}