'setExact'和'setAlarmClock'之间的区别

Zel*_*g63 7 java android alarm

在我的应用程序中应该在指定的时间触发警报,警报的目的是通过a通知用户Notification,我对获得非精确结果感到绝望.警报振铃,但不是在指定的时间.当警报设置和应该关闭的时间之间的时间很长时,这是系统的.为此,我正在使用setExact(RTC_WAKEUP,time,intent).经过多次尝试才能使它工作,我终于看到并尝试了这个setAlarmClock功能,一切都很顺利!

根据javadoc,除了暗示之外,它setAlarmClock是相同的.据我所知,至少存在另外一个差异.有谁知道吗?setExactRTC_WAKEUP

Sni*_*kow 9

setExact()并且setAlarmClock()两者都进行非常相似的调用setImpl().如果你使用RTC_WAKEUP你的setExact()电话,唯一的区别是类型的最终ARG AlarmClockInfo.

在服务端,在set()中接收呼叫.在该方法结束时,我们可以看到额外参数的区别:

@Override
public void set(int type, long triggerAtTime, long windowLength, long interval, int flags,
        PendingIntent operation, WorkSource workSource,
        AlarmManager.AlarmClockInfo alarmClock) {
    final int callingUid = Binder.getCallingUid();
    if (workSource != null) {
        getContext().enforcePermission(
                android.Manifest.permission.UPDATE_DEVICE_STATS,
                Binder.getCallingPid(), callingUid, "AlarmManager.set");
    }

    // No incoming callers can request either WAKE_FROM_IDLE or
    // ALLOW_WHILE_IDLE_UNRESTRICTED -- we will apply those later as appropriate.
    flags &= ~(AlarmManager.FLAG_WAKE_FROM_IDLE
            | AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED);

    // Only the system can use FLAG_IDLE_UNTIL -- this is used to tell the alarm
    // manager when to come out of idle mode, which is only for DeviceIdleController.
    if (callingUid != Process.SYSTEM_UID) {
        flags &= ~AlarmManager.FLAG_IDLE_UNTIL;
    }

    // If the caller is a core system component, and not calling to do work on behalf
    // of someone else, then always set ALLOW_WHILE_IDLE_UNRESTRICTED.  This means we
    // will allow these alarms to go off as normal even while idle, with no timing
    // restrictions.
    if (callingUid < Process.FIRST_APPLICATION_UID && workSource == null) {
        flags |= AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED;
    }

    // If this is an exact time alarm, then it can't be batched with other alarms.
    if (windowLength == AlarmManager.WINDOW_EXACT) {
        flags |= AlarmManager.FLAG_STANDALONE;
    }

    // If this alarm is for an alarm clock, then it must be standalone and we will
    // use it to wake early from idle if needed.
    if (alarmClock != null) {
        flags |= AlarmManager.FLAG_WAKE_FROM_IDLE | AlarmManager.FLAG_STANDALONE;
    }

    setImpl(type, triggerAtTime, windowLength, interval, operation,
            flags, workSource, alarmClock, callingUid);
}
Run Code Online (Sandbox Code Playgroud)

如果存在非null alarmClock参数,则请求将获得FLAG_WAKE_FROM_IDLE标记,FLAG_STANDALONE在两种情况下都会获得该标记.评论FLAG_WAKE_FROM_IDLE说:

报警标志:即使报警处于空闲状态,该报警也会唤醒设备.例如,这是闹钟的警报.

如果你愿意,你可以深入了解FLAG_WAKE_FROM_IDLE - 但据我所知,这就是差异.您的"正常"确切警报不会将设备从空闲状态唤醒,但使用setAlarmClock()设置的警报会启动.


ser*_*inc 7

打盹模式,在 Android 6.0(API 级别 23)中引入,增加了对上述限制的进一步限制:(TL;DR:仅setAlarmClock在打盹模式下触发,并添加系统图标

打瞌睡限制

以下限制适用于您在 Doze 中的应用程序:[...]

  • 系统忽略唤醒锁。
  • 标准AlarmManager警报(包括setExact()setWindow())将推迟到下一个维护时段。
    • 如果您需要设置在打瞌睡时触发的闹钟,请使用setAndAllowWhileIdle()setExactAndAllowWhileIdle()
    • 设置为 的警报setAlarmClock()继续正常触发 - 系统在这些警报触发前不久退出 Doze。[...]

设备进入低功耗打瞌睡状态,定期唤醒以执行其他警报等。