AlarmManager 是否基于设备的当前时间?

Is AlarmManager based on the device's current time?

我有一个应用程序有一个独立的(可以这么说).. Clock 来自设备的系统时钟。如果该时钟到达特定时间,我需要从该时钟创建一个警报。现在我正在使用 AlarmManagerPendingIntent 创建一个警报,但它只需要 long triggerAtMillis 作为参数。这是基于设备的系统时钟吗?

根据 AlarmManager.setRepeating()javadocs

 * @param triggerAtMillis time in milliseconds that the alarm should first
 * go off, using the appropriate clock (depending on the alarm type).

那么appropriate clock是什么意思?

虽然AlarmManager对设备时间起作用,但触发警报的时间由您提供。

因此您可以计算自定义时钟与设备时钟之间的差异,然后将相应的时间传递给 AlarmManager

你可以试试这样的

第 1 步获取设备时钟和您的时钟的当前时间并将它们相减得到差值

第2步 不同的是,您现在可以根据您的时钟时间计算设备时间

第 3 步 现在根据该时间设置 AlarmManager,该时间相当于您的自定义时钟时间的设备时间。

虽然这是一种解决方法,但这是我能想到的。

希望这对您有所帮助:)

根据docs

There are two general clock types for alarms: "elapsed real time" and "real time clock" (RTC). Elapsed real time uses the "time since system boot" as a reference, and real time clock uses UTC (wall clock) time. This means that elapsed real time is suited to setting an alarm based on the passage of time (for example, an alarm that fires every 30 seconds) since it isn't affected by time zone/locale. The real time clock type is better suited for alarms that are dependent on current locale.

时间取决于当前本地,当用户更改本地设置时,它也会影响您的闹钟。 您也可以根据自己的意愿使用 RTC 方法