Erick
Erick

Reputation: 715

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?

Upvotes: 9

Views: 5580

Answers (2)

Saket
Saket

Reputation: 3076

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

Upvotes: 4

i.n.e.f
i.n.e.f

Reputation: 1803

You have to Check System Time in Background at Every Definite Time Interval & if Your desired time (12:00 am) matches the system time then do Something what you have to do.For this Task you have to use Either Async Task Or Alarm Manager.If i am not wrong then this would be the Solution to your Question hope this help you.

Upvotes: 1

Related Questions