setAlarmClock在android棉花糖中不完全是 [英] setAlarmClock not exact in android marshmallow

查看:189
本文介绍了setAlarmClock在android棉花糖中不完全是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Alarmmanager setAlarmClock 方法进行闹钟应用。
当我从6.00 AM设置闹钟并在6.01 AM设置闹钟时。

I use setAlarmClock method of Alarmmanager for do alarm clock application. When I set alarm clock from 6.00 AM and alarm in 6.01 AM . It delay and not exact.

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

setCalander发出警报时的代码

Code when setCalander for alarm

  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

Ok, I implement from this project https://github.com/googlesamples/android-DirectBoot.

此项目使用setAlarmClock发出警报。

This project use setAlarmClock for alarm when it's time.

更新:我将setAlarmClock更改为setExact

update: I change setAlarmClock to setExact

此代码:

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

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

It doesn't work for me but It delay same setAlarmClock.

当前时间(以毫秒为单位):1473318858628
我使用 http://www.ruddwire.com/handy-code/date-to-millisecond-calculators/#.V9EPXfmLRD8

Current Time in millisecond: 1473318858628 I change to default format by use http://www.ruddwire.com/handy-code/date-to-millisecond-calculators/#.V9EPXfmLRD8

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

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

设置闹钟时间为毫秒:1473318960000

Set Alarm Time in millisecond:1473318960000

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

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

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

And now It's 14:16:00 , It doesn't alarm.

是在14.20:00发出警报,因此延迟约4分钟。 (以毫秒为单位的时间= 1473319200207)

It's alarm on 14.20:00 , so delay about 4 minutes. ( Time in millisecond = 1473319200207)

谢谢

推荐答案

阅读文档。这是设计使然,根据文档的摘录:

You should read the documentation. This is by design as according to the documentation with the excerpt:


注意:从API 19(KITKAT)开始,警报传递是不精确的:操作系统将更改警报,以最大程度地减少唤醒和电池消耗。有一些新的API支持需要严格交付保证的应用程序。请参见setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。 targetSdkVersion早于API 19的应用程序将继续看到以前的行为,在该行为中,所有警报均在请求时准确地传递。

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.



Ref



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

这篇关于setAlarmClock在android棉花糖中不完全是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆