日期和时间变化监听器在Android中? [英] Date and time change listener in Android?

查看:584
本文介绍了日期和时间变化监听器在Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,有一个报警服务,我发现,如果用户修改它的日期或时间到时间的流逝。我的闹钟不会在我预计的时间被触发。

In my application, there's a alarm service, and I find that if user change it's date or time to a passed time. My alarm will not be triggered at the time I expect.

所以,我可能会再次复位所有报警。是否有日期和时间的变化监听器在Android的?

So, I may have to reset all the alarms again. Is there an date and time change listener in android?

推荐答案

创建一个意图过滤器:

     static {
        s_intentFilter = new IntentFilter();
        s_intentFilter.addAction(Intent.ACTION_TIME_TICK);
        s_intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
    }

和广播接收器:

    private final BroadcastReceiver m_timeChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action.equals(Intent.ACTION_TIME_CHANGED) ||
                action.equals(Intent.ACTION_TIMEZONE_CHANGED))
            {
                doWorkSon();
            }
        }
    };

注册接收器:

     public void onCreate() {
        super.onCreate();
        registerReceiver(m_timeChangedReceiver, s_intentFilter);     
    }

编辑:

和注销它:

     public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(m_timeChangedReceiver);     
    }

这篇关于日期和时间变化监听器在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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