-2

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.

Sasan Farrokh
  • 184
  • 2
  • 12
  • You could try using an alarmmanager, with this you can set a specific timestamp single or repeat occurence to run a service. – kabuto178 Aug 06 '16 at 22:42

1 Answers1

0

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
    }
}
Hardik Kubavat
  • 251
  • 3
  • 23