Date and Time Change Listener in Android

Date and time change listener in Android?

Create an intent filter :

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

and a broadcast receiver:

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

register the receiver:

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

EDIT:

and unregister it:

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

On date change listener

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

How do i programatically change the Current date and time of an event in android?

You want to set the calender event based on the date selected by user, hence you need to user the myCalender instance to fetch the startTime.

long startTime = myCalendar.getTime()+24*60*60*1000;
long endTime = startTime+ 60 * 30 * 1000;

Below line create a calender instance of the curent date time. Hence remove it.

 Calendar cal = Calendar.getInstance();

Android check for date changed

You can use brodcast receiver like ACTION_DATE_CHANGED

More details can be found at https://developer.android.com/reference/android/content/Intent.html#ACTION_DATE_CHANGED

Answer to more questions:

1) For specific date you can have check within onReceive method.
2) To schedule it at a different time or do other stuffs after particular days, you can also use Alarm Service like in the below link:

Android - How to set a notification to a specific date in the future?

Android: listener that detects minute changing in the clock

Thanks to the hint of Manpreet Singh I was able to come up with the following:

BroadcastReceiver tickReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0) {
Log.v("Karl", "tick tock tick tock...");
}
}
};
registerReceiver(tickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK)); // register the broadcast receiver to receive TIME_TICK

Then call the following onStop():

// unregister broadcast receiver, will get an error otherwise
if(tickReceiver!=null)
unregisterReceiver(tickReceiver);


Related Topics



Leave a reply



Submit