AlarmManager:如何安排每天的报警和处理时间的变化 [英] AlarmManager: how to schedule a daily alarm and deal with time changes

查看:1042
本文介绍了AlarmManager:如何安排每天的报警和处理时间的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要每天设置一个闹钟在给定的时间。我使用 AlarmManager.RTC_WAKEUP 设置报警,所以它使用系统的时间作为参考。我设置闹钟首先在期望的时间执行,那么每天重复:

I need to set up an alarm daily at a given hour. I'm programming the alarm using AlarmManager.RTC_WAKEUP, so it uses the system time as reference. I'm setting the alarm to first execute at the desired hour, then to repeat daily:

    alarmManager.setRepeating(
        AlarmManager.RTC_WAKEUP,
        getTimestampToday("12:00"),
        AlarmManager.INTERVAL_DAY,
        pendingIntent
    );

getTimestampToday 方法返回在所需时间较长时间戳今天。它通过获得今天本地日期,则设定所需的小时,最后将其转换回时间戳(基于UTC)这样做。

The getTimestampToday method returns a long timestamp for today at the desired hour. It does so by obtaining the local date for today, then setting the desired hour, finally converting it back to a timestamp (which is UTC based).

这里的问题是报警应根据本地时间工作。因此,如果报警调度后的时间更改(例如,夏令时间的变化,或者用户进入到一个不同的国家,或是他改变了日期,时间或手动时区),报警将触发在错误的地方时间下一次。

The problem here is the alarm should work according to the local time. So if the time changed after the alarm is scheduled (for instance, DST time change, or the user goes to a different country, or he changes the date, time or timezone manually), then the alarm will fire at the wrong local time the next time.

例如:


  1. 报警在8:00上午设为第一把火,每天重复(即间隔86400秒)。

  2. 那天晚上,一个自动DST时间更改发生在3:00 AM,移位时钟拨回凌晨2:00。

  3. 第二天早上,7:00 AM的火警,这是不对的。

该应用程序,就是要运行很长一段时间(它是唯一的企业使用信息亭的应用程序)。我重新调度警报应用程序启动的时候,但是这并没有解决我的问题,因为该应用程序可以在不重新启动运行天。我需要检测到时改变根据新的本地时间重新安排这些报警。

The app is meant to be running for long periods of time (it is a kiosk app for corporate use only). I'm re-scheduling the alarms when the app starts, but this does not solve my problem, because the app can run for days without being restarted. I need to detect when the time changes to schedule the alarms again according to the new local time.

我使用这个接收器:

    <receiver android:name="foo.bar.receivers.TimeChangeReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.ACTION_TIMEZONE_CHANGED" />
            <action android:name="android.intent.action.ACTION_TIME_CHANGED" />
            <action android:name="android.intent.action.DATE_CHANGED"></action>
        </intent-filter>
    </receiver>

它检测的日期改变,但不随时间变化,也不时区变化工作。 (测试它在平板电脑上运行OS 4.0.3)。这是非常奇怪的,因为我可以清楚地看到事件被logcat中播出。

It detects the date change, but does not work with time changes nor time zone changes. (Tested it on a tablet running OS 4.0.3). It is really odd, because I can clearly see the events being broadcasted in logcat.

所以我的问题:


  • 为什么是 ACTION_TIMEZONE_CHANGED ACTION_TIME_CHANGED 不被我的广播接收器接收,我怎么能解决这个问题的事件。

  • 是将这些意图检测DST时间更改呢?

  • 如果不可能,有没有更好的方法编写在本地时间报警和处理时间的变化?

  • Why are the ACTION_TIMEZONE_CHANGED and ACTION_TIME_CHANGED events not being received by my BroadcastReceiver, and how could I solve it.
  • Would these intents detect a DST time change as well?
  • If not possible, is there a better approach to program alarms at local times and deal with time changes?

先谢谢了。

推荐答案

您应该使用

<action android:name="android.intent.action.TIMEZONE_CHANGED" /> 
<action android:name="android.intent.action.TIME_SET" />

INSTEAD-OF

Instead-of

  <action android:name="android.intent.action.ACTION_TIMEZONE_CHANGED" />
  <action android:name="android.intent.action.ACTION_TIME_CHANGED" />

在请参阅 ACTION_TIMEZONE_CHANGED 并的 ACTION_TIME_CHANGED 。并观察这些常量定的值。

Refer at ACTION_TIMEZONE_CHANGED and ACTION_TIME_CHANGED. And observe the value given for these Constants.

而对于DST更改读<一个href=\"http://stackoverflow.com/questions/8377853/does-android-alarmmanager-handle-daylight-saving-changes\">Does Android的AlarmManager处理夏令时的变化?

这篇关于AlarmManager:如何安排每天的报警和处理时间的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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