在安卓6.0系统中,setAlarmClock方法并不精确。

4

我使用AlarmManagersetAlarmClock方法来制作闹钟应用程序。当我将闹钟设置为早上6点,闹钟在早上6点01分响起时,它会延迟并不准确。

以下是setAlarmClock的代码。

Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.putExtra(AppConstant.REMINDER, note.convertToString());
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
        note.getId(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(getTargetTime().getTimeInMillis(),pendingIntent),pendingIntent);
}

为闹钟设置日历时的代码

  private Calendar getTargetTime() {
    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();
    calSet.set(Calendar.DAY_OF_MONTH, sDay);
    calSet.set(Calendar.HOUR_OF_DAY, sHour);
    calSet.set(Calendar.MINUTE, sMinute);
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);

    return calSet;
}

好的,我从这个项目中实现:https://github.com/googlesamples/android-DirectBoot

该项目在时间到达时使用setAlarmClock进行闹钟设置。

更新:我将setAlarmClock更改为setExact。

这是代码:

 alarmManager.setExact(AlarmManager.RTC_WAKEUP, getTargetTime().getTimeInMillis(), pendingIntent);

对我来说它不起作用,但它延迟了同样的设置闹钟。

当前时间(毫秒):1473318858628,我使用http://www.ruddwire.com/handy-code/date-to-millisecond-calculators/#.V9EPXfmLRD8将其更改为默认格式。

Thu Sep 08 2016 14:14:18 GMT+0700

设置闹钟的时间(毫秒):1473318960000

Thu Sep 08 2016 14:16:00 GMT+0700

现在是14:16:00,但它没有响铃。

它将在14:20:00响铃,所以延迟了约4分钟。(毫秒时间=1473319200207)

谢谢


1
这是在设备上还是模拟器上?如果是设备,请在模拟器上尝试相同的代码。可能是设备的问题。某些设备实际上会对闹钟造成问题。另外,您可以尝试示例项目,并查看计划是否正确(如果适用)。 - JoxTraex
好的,谢谢!我使用Wiko Ufeel(Android Marshmallow)设备,我会尝试使用模拟器。非常感谢。 - Wuttipong Khemphetjetsada
2个回答

2
你应该阅读文档。根据摘录的说明,这是有意设计的:
引用:

注意:从API 19(KITKAT)开始,闹钟传递是不精确的:操作系统将移动闹钟以最小化唤醒和电池使用。有新的API支持需要严格的交付保证的应用程序;请参见setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。目标SdkVersion早于API 19的应用程序将继续看到以前的行为,在其中所有闹钟在请求时都会准确地传递。

参考资料

https://developer.android.com/reference/android/app/AlarmManager.html


好的,谢谢。如何显示setalarmclock是不准确的?使用setWindow代替setalarmclock,但我看到其他闹钟应用程序使用setAlarmclock,因为当我设置闹钟时,在移动设备的状态栏右侧看到闹钟图像。 - Wuttipong Khemphetjetsada
我使用了 setWindow 和 setExact,但它对我不起作用。 - Wuttipong Khemphetjetsada
1
你不应该同时使用两者,只能选其一。此外,“不起作用”并没有告诉我们任何信息。你已经验证过它是否正确地被安排了吗? - JoxTraex
1
我通过打印 getTimeInMilli 并转换为实际时间进行了验证。它与我设置的时间有所延迟。你有其他的方法吗?非常感谢。 - Wuttipong Khemphetjetsada
1
好的,当我使用setExact时,我会更新我的帖子。抱歉给您带来不便。非常感谢您的帮助。 - Wuttipong Khemphetjetsada

-1

如果你在使用闹钟,请使用setAlarmClock而不是setExact。

在你的代码中:

alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(getTargetTime().getTimeInMillis(),pendingIntent),pendingIntent);

也许您的 getTargetTime() 函数出了问题。尝试手动将时间设置为比当前时间早 1 分钟,方法是添加:
alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(getTargetTime().getTimeInMillis()-60*1000,pendingIntent),pendingIntent);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接