gpt4 book ai didi

android - setAlarmClock 在 android marshmallow 中不准确

转载 作者:行者123 更新时间:2023-11-30 01:01:15 26 4
gpt4 key购买 nike

我使用 AlarmmanagersetAlarmClock 方法来做闹钟应用。当我从 6.00 AM 设置闹钟并在 6.01 AM 设置闹钟时。它延迟且不准确。

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);

它对我不起作用,但它会延迟相同的 setAlarmClock。

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

2016 年 9 月 8 日星期四 14:14:18 GMT+0700

以毫秒为单位设置闹钟时间:1473318960000

2016 年 9 月 8 日星期四 14:16:00 GMT+0700

现在是14:16:00,不报警。

14.20:00 闹钟,请延迟约 4 分钟。 (以毫秒为单位的时间 = 1473319200207)

感谢

最佳答案

您应该阅读文档。这是根据带有摘录的文档设计的:

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

引用

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

关于android - setAlarmClock 在 android marshmallow 中不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39363500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com