setAlarmClock在android棉花糖中不完全是

setAlarmClock在android棉花糖中不完全是

本文介绍了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;
}

好,我从这个项目中实现。

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
我使用

Current Time in millisecond: 1473318858628I 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:



Ref



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

08-06 02:10