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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With