检查 AlarmManager 任务是否正在进行

Check if AlarmManager task is in progress

我正在 运行使用 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,但我担心如果我再次 运行 这段代码,它会重新编写我的日程安排。问题:

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.