在日期更改监听器

在日期更改监听器

本文介绍了在日期更改监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是每次更改日期(每隔12点)触发一个方法来刷新或重置我的应用程序。我试图在网上搜索答案,但我找不到任何东西。我可以使用android中的任何方法/或监听器吗?或任何方法?
任何建议的人?

My task is to trigger a method to refresh or reset my application every time the date change (every 12am). I tried to search the web for an answer but I can't find anything. Is there any method/or listeners in android that I can use? or any approach?Any suggestion guys?

推荐答案

是的,您可以在Android上收听日期/时间更改。为此,请在您的中明确注册以下意向过滤器活动:

Yes you can listen to date / time changes on Android. For this, register a BroadcastReceiver for the following intent filters explicitly in your Activity:

android.intent.action.ACTION_TIME_TICK

此意图每分钟发送一次。您无法通过清单中声明的​​组件接收此内容,但只能通过。

This Intent is sent every minute. You can not receive this through components declared in your manifest, but only by explicitly registering for it with Context.registerReceiver().

您的Receiver(内部)类:

Your Receiver (inner) class:

private final MyDateChangeReceiver mDateReceiver = new MyDateChangeReceiver();

public class MyDateChangeReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(final Context context, Intent intent) {

        // compare current time and decide if it's 12 AM
        Log.d("MyDateChangeReceiver", "Time changed");

    }
}

在onResume方法中注册:

Register it in your onResume method:

registerReceiver(mDateReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));

并且不要忘记在onPause方法中取消注册:

and do not forget to un-register it in your onPause method:

unregisterReceiver(mDateReceiver);

这篇关于在日期更改监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:30