【发布时间】:2016-11-11 17:50:22
【问题描述】:
在我的应用中,我需要在每天下午 2:00 启动一项服务。现在我写了一次触发警报的代码,每次打开应用程序都会运行此代码:
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, DownloadReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.cancel(pIntent);
Calendar cal= Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY,refreshhour);
cal.set(Calendar.MINUTE,refreshmin);
cal.set(Calendar.SECOND, 0);
// if the scheduler date is passed, move scheduler time to tomorrow
if (System.currentTimeMillis() > cal.getTimeInMillis()) {
cal.add(Calendar.DAY_OF_YEAR, 1);
}
if(android.os.Build.VERSION.SDK_INT>=23) {
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), pIntent);
}
else{
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pIntent);
}
Q1.如果设备处于Doze 模式,我将setAndAllowWhileIdle() 用于23 以上的sdk。我在此功能中找不到任何可以将闹钟设置为每天重复的选项。
Q2.我也有关于setInexactRepeating()的问题,一般是通过设置参数INTERVAL_DAY设置为每天重复,但在docs中,它说
从 API 19 开始,所有重复警报都将不准确,并受制于 与其他警报进行批处理,无论其规定的重复间隔如何。
这是否意味着INTERVAL_DAY 不再起作用了,那么如何在不重新运行此功能并重置alarmManager 的情况下每天设置闹钟?
【问题讨论】:
标签: android alarmmanager