java - 检查 AlarmManager 任务是否正在进行

标签 java android alarmmanager android-pendingintent android-backup-service

我正在使用 AlarmManager 运行定期后台任务:

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 60 * 1000, pendingIntent);

我放了一个标志 PendingIntent.FLAG_UPDATE_CURRENT,但我担心如果我再次运行这段代码它会重新编写我的日程安排。问题:

  • 是否会重置时间,我必须等待 10 分钟才能收到事件?
  • 有没有办法检查我是否设置了闹钟任务计划?

最佳答案

Will it reset the time and I'll have to wait for 10 mins to receive an event?

是的,会的。根据文档:

If there is already an alarm scheduled for the same IntentSender, it will first be canceled.

根据

Is there a way to check if my alarm task schedule is set?

是的,there is几个解决方案,您可以检查。

另外,请注意 setRepeating不准确的

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

关于java - 检查 AlarmManager 任务是否正在进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41793577/

相关文章:

java - JDBC 在 "same"时间打开 2 个连接

java - 无法使用谷歌地图构建应用程序

android - 使用 telnet 从一个 Android 模拟器调用电话到另一个?

android - AlarmManager,手机休眠时不调用闹钟

android - 如何获取在关机和开机之间传递的警报通知

java - 如何实现自调整二叉搜索树?

java - 如何使用 Mockito 将模拟注入(inject)抽象父类中的 @Autowired 字段

java - 逻辑错误导致输出丢失/不正确,可能与 for 循环相关

java - 日志开关实现

Android:定期(每 30 秒)在后台发送位置数据并向服务器请求新数据