I want to call a function when System timestamp reaches to specific time. is there anyway better than a CountDownTimer ? It should be called on a service because i want it still run when app closes.
thanks a lot.
I want to call a function when System timestamp reaches to specific time. is there anyway better than a CountDownTimer ? It should be called on a service because i want it still run when app closes.
thanks a lot.
You have to use BroadcastReceiver along with AlarmManager Like this.
//Create alarm manager
AlarmManager malarmMngr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//Create pending intent & register it to your alarm notifier class
Intent intent = new Intent(this, yourBroadcastReceiver.class);
PendingIntent mPendInt = PendingIntent.getBroadcast(this, 0, intent, 0);
//set your time stamp (for once in future)
malarmMngr .set(AlarmManager.RTC_WAKEUP, yourtimestamp, mPendInt);
Now create yourBroadcastReceiver class to call function.
public class yourBroadcastReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// This method is called when this BroadcastReceiver receives an Intent broadcast.
// Call your function here
}
}