Android ACTION_DATE_CHANGED 广播 [英] Android ACTION_DATE_CHANGED broadcast

查看:70
本文介绍了Android ACTION_DATE_CHANGED 广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台 Nexus S,当我在手机上手动更改日期时,并不总是广播 ACTION_DATE_CHANGED.如果我将日期从 2014 年 2 月 13 日更改为 2014 年 2 月 14 日,我还没有得到 ACTION_DATE_CHANGED 的工作,但如果我将其设置为未来几年,我有时会触发它.

I have a Nexus S, and when I change the date manually on the phone, ACTION_DATE_CHANGED is not always broadcasted. If I change the date from Feb 13, 2014 to Feb 14, 2014, I have not gotten an ACTION_DATE_CHANGED to work, but if I set it to several years in the future, I sometimes get it to fire.

我可以 (99%) 向您保证我没有滥用 IntentFilters、BroadcastReceivers 等.我只是很好奇为什么这个广播的记录如此糟糕.通过 SO & 快速扫描谷歌显示,人们不确定它是在用户手动更改时发生,还是在每天凌晨 12:00 日期滚动时发生,或两者兼而有之.我的经验表明,它在用户更改方面非常不一致,我还没有尝试过系统更改.

I can (99%) assure you I'm not misusing IntentFilters, BroadcastReceivers, etc. I'm just curious as to why this broadcast is so poorly documented. A quick scan through SO & Google shows that people aren't sure if it happens when the user manually changes it, or when the date rolls over at 12:00am every day, or both. My experience shows that it's pretty inconsistent regarding user changes and I haven't tried system changes.

我将通过 AOSP 代码进行 grep 并隔离所有触发点并返回报告.

I'll grep through the AOSP code and isolate all of the points where this is fired and report back.

问题:有人知道这里发生了什么吗?:-)

the question: Anyone have any idea what's going on here? :-)

推荐答案

这里是 frameworks/base/services/java/android/server/AlarmManagerService.java 中 4.0.3_r1 的代码.

Here is code from 4.0.3_r1 in frameworks/base/services/java/android/server/AlarmManagerService.java.

首先,我们创建一个 PendingIntent mDateChangeSender;

First, we create a PendingIntent mDateChangeSender;

private final PendingIntent mDateChangeSender;

然后,在AlarmManagerService.java的构造函数中,我们设置PendingIntent:

Then, in the constructor of AlarmManagerService.java, we setup the PendingIntent:

Intent intent = new Intent(Intent.ACTION_DATE_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
mDateChangeSender = PendingIntent.getBroadcast(context, 0, intent, 0);

然后在构造函数中:

mClockReceiver.scheduleDateChangedEvent();

那么什么是 mClockReceiver?只是一个 BroadcastReceiver 监听 Intent.ACTION_TIME_TICK 和 Intent.ACTION_DATE_CHANGED.在它的 onReceive():

So what is mClockReceiver? Just a BroadcastReceiver listening for Intent.ACTION_TIME_TICK and Intent.ACTION_DATE_CHANGED. In it's onReceive():

...
else if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED)) {
...
    scheduleDateChangedEvent();
}

然后,后来我们找到了 scheduleDateChangedEvent() 方法:

Then, later we find the method scheduleDateChangedEvent():

public void scheduleDateChangedEvent() {
     Calendar calendar = Calendar.getInstance();
     calendar.setTimeInMillis(System.currentTimeMillis());
     calendar.set(Calendar.HOUR, 0);
     calendar.set(Calendar.MINUTE, 0);
     calendar.set(Calendar.SECOND, 0);
     calendar.set(Calendar.MILLISECOND, 0);
     calendar.add(Calendar.DAY_OF_MONTH, 1);
     set(AlarmManager.RTC, calendar.getTimeInMillis(), mDateChangeSender);
}

所以它设置了一次性闹钟,从当前时间开始,然后将小时/分钟/秒/毫秒设置为零,然后添加一天,所以如果今天是下午1:30,那么下次它会得到将在 10 小时 30 分钟内发射.

So it sets a one-shot alarm, starting with the current time, then setting hour/min/sec/milli to zero, then adding a day, so if it was 1:30pm today, the next time it will get fired would be in 10 hours and 30 minutes.

这并不是说这里没有错误或任何东西,但看起来 ACTION_DATE_CHANGED 应该在每天午夜触发.

This isn't to say there aren't bugs or anything here, but it LOOKS like ACTION_DATE_CHANGED should fire at midnight every day.

现在 - 如果我要更改电话上的日期,可以说是未来 10 年.处理时间变化的代码将触发第一个 ACTION_DATE_CHANGED 事件,然后安排一个新的 ACTION_DATE_CHANGED 被触发,在 10 年 + 一天的一小部分.然后,如果我们将日期改回 10 年,到正确的日期,警报仍计划在 10 年后触发,因此 ACTION_DATE_CHANGED 将不再被触发(除非您将日期设置为 10 年后 - 试试看!).

NOW - if I were to change the date on the phone lets say 10 years into the future. The code to handle the change in time will fire the first ACTION_DATE_CHANGED event then schedule a new ACTION_DATE_CHANGED to get fired, at 10 years + some fraction of a day. Then if we change the date back 10 years, to the correct date, the alarm is still scheduled to be fired in 10 years, thuse ACTION_DATE_CHANGED will no longer get fired (unless you set the date further than 10 years from now - try it!).

tl;dr:这是 Android 中的一个错误.

tl;dr: This is a bug in Android.

这篇关于Android ACTION_DATE_CHANGED 广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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