Question

I'm trying to create notification when specified date is up. Where should I put method which checks date if you want that this method was invoked all the time during the program? Thanks from advice.

Was it helpful?

Solution

I'd suggest a Service so you won't miss the date even if your app wasn't running or just create a timer in your activity

create a service and add it in your application manifest.xml

public class DateCheckService extends Service {
   @Override
   public IBinder onBind(Intent arg0) {
      return null;
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {

      // add a timer here to check date, check below for code

      Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      return START_STICKY;
   }
   @Override
   public void onDestroy() {
      super.onDestroy();
      Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
   }
}

create timer in activity or service.

Timer timer = new Timer();

class DateCheckTask extends TimerTask {

   public void run() {
       //calculate date and take action
   }
}

finally add the task to the Timer with update interval

TimerTask dateCheckTask = new DateCheckTask ();
timer.scheduleAtFixedRate(dateCheckTask , 0, 10);//interval of 10unit
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top