我目前正在尝试编写警报管理器,它会在每天的指定时间段内发出警报。首先,我检查用户是否为那天设置了闹钟:
if ((User.getReminderTime(Home.this) > 0)
&& (dt.getDate() != today.getDate() || dt.getDay() != today
.getDay())) {
AppointmentManager.setFutureAppointmentCheck(this
.getApplicationContext());
User.setLongSetting(this, "futureappts", today.getTime());
}
然后我去设置实际的闹钟在第二天的 12 点到 12:10 之间响起:
public static void setFutureAppointmentCheck(Context con) {
AlarmManager am = (AlarmManager) con
.getSystemService(Context.ALARM_SERVICE);
Date futureDate = new Date(new Date().getTime() + 86400000);
Random generator = new Random();
futureDate.setHours(0);
futureDate.setMinutes(generator.nextInt(10));
futureDate.setSeconds(0);
Intent intent = new Intent(con, FutureAppointmentReciever.class);
PendingIntent sender = PendingIntent.getBroadcast(con, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP, futureDate.getTime(), sender);
}
现在我为此设置了一个测试环境,它每两分钟就会关闭一次,它似乎工作正常,但是当我部署到实际设备时,接收器似乎没有收到警报。我认为这可能是设备处于 sleep 状态的问题,所以我添加了电源管理器。但是还是不行:
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "keepAlive");
wl.acquire();
setFutureAppointments(context.getApplicationContext());
AppointmentManager.setFutureAppointmentCheck(context
.getApplicationContext());
User.setLongSetting(context.getApplicationContext(), "futureappts",
new Date().getTime());
wl.release();
有人看到我做错了什么还是我做错了?感谢您的帮助。
最佳答案
我通常会做更多类似的事情:
Intent i = new Intent(this, MyService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi); // cancel any existing alarms
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
AlarmManager.INTERVAL_DAY, pi);
这样,您就不必担心在 Service
中重新设置 AlarmManager
。
我通常在我的应用程序启动时运行这段代码(onResume
在我的主要 Activity 中)并在设置为接收 BOOT_COMPLETED< 的
。BroadcastReceiver
中运行
我已经编写了关于创建 Service
和使用 AlarmManager
的指南,该指南基于我自己的经验以及我从观看中挑选的一些提示和技巧Google I/O 演讲。有兴趣的可以阅读here .
要在下面回答您的问题,我所能做的就是引用 the docs :
public void setInexactRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour. These alarms are more power-efficient than the strict recurrences supplied by setRepeating(int, long, long, PendingIntent), since the system can adjust alarms' phase to cause them to fire simultaneously, avoiding waking the device from sleep more than necessary.
Your alarm's first trigger will not be before the requested time, but it might not occur for almost a full interval after that time. In addition, while the overall period of the repeating alarm will be as requested, the time between any two successive firings of the alarm may vary. If your application demands very low jitter, use setRepeating(int, long, long, PendingIntent) instead.
总之,还不是很清楚。文档只说警报“可能会有所不同”。但是,您应该知道,第一个触发可能不会在该时间之后的几乎整个间隔内发生。
关于android - 如何设置 Recurring AlarmManager 以每天执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4430849/