Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On date change listener

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?

like image 906
Erick Avatar asked Feb 10 '14 10:02

Erick


1 Answers

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().

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");

    }
}

Register it in your onResume method:

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

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

unregisterReceiver(mDateReceiver);
like image 142
Saket Avatar answered Oct 26 '22 00:10

Saket