title: Keep running Multiple countdown timers and multiple stopwatches in background using Android
body: <p>I am new to Android and Java and I have been trying to solve an issue on timers.Please help
  I have an App where the user can create multiple countdown timers and multiple stopwatches.
  Example code of countdown timer is given below .(Stop watch is also implemented in a similar fashion using handler). I want the timers and stopwatches to run in the background even when the user leaves the app.And the current state of timers should be shown when the user comes back to the app again,rather than showing  the initial state.
  The problem is my timers and stopwatches are running when I leave the app,but the results  are not published into the UI when the user comes back to the app. A new instance is created and the fresh timers with 00:00:00 are shown again,while the old timers keep running in the background and goes on without being able to stop.Also in the following code I have a listener which publishes the result into the UI in its callback.This interface is implemented in my activity. So can anyone please suggest a solution for this ?</p>
  
  <pre><code>public class CountDownTimerService {
  
      public interface CounterTimeChangeListener {
          public void onCounterTimeChange(String time,Boolean counterFinished);
      }
  
      CounterTimeChangeListener listener;
  
      MyCountDownTimer myCountDownTimer;
      String output;
      int length;
      long startTime = 0;
      long currentTime = 0;
      long timeElapsed = 0;
      long timeRemaining = 0;
      long prevTimeRemaining = 0;
  
  
      public CountDownTimerService(int countDownFutureTime)
      {
          myCountDownTimer= new MyCountDownTimer(countDownFutureTime,1000);
          length=countDownFutureTime;
      }
  
  
  
      // code for countdown timer
  
      // this function format the time to hours seconds and minutes format
      public String formatTime(long mills)
      {
          output = "";
          long seconds = mills / 1000;
          long minutes = seconds / 60;
          long hours = minutes / 60;
  
          seconds = seconds % 60;
          minutes = minutes % 60;
          hours = hours % 60;
  
          String secondsD = String.valueOf(seconds);
          String minutesD = String.valueOf(minutes);
          String hoursD = String.valueOf(hours);
  
  
          if (seconds &lt; 10)
              secondsD = "0" + seconds;
          if (minutes &lt; 10)
              minutesD = "0" + minutes;
  
          if (hours &lt; 10)
              hoursD = "0" + hours;
  
          output = hoursD +":" +minutesD +":"+ secondsD;
  
          return output;
      }
  
      // this function is called on start button click
  
      public void CountDownControl(int state) {
  
          if(state==0)
          {
              startTime = System.currentTimeMillis();
              myCountDownTimer.start();
          }
  
          else if(state==1)
          {
              // when the pause is clicked
              myCountDownTimer.cancel();
              currentTime = System.currentTimeMillis();
              timeElapsed = currentTime - startTime;
              if (prevTimeRemaining == 0)
                  timeRemaining = length - timeElapsed;
              else
                  timeRemaining = prevTimeRemaining - timeElapsed;
              prevTimeRemaining = timeRemaining;
              myCountDownTimer= new MyCountDownTimer(timeRemaining,1000);
  
              if (listener!=null) {
                  listener.onCounterTimeChange(formatTime(timeRemaining), false);
              }
  
          }
  
      }
  
      // this function is called on reset button click
      public void resetInitial()
      {
          prevTimeRemaining = 0;
          myCountDownTimer.cancel();
          if (listener!=null) {
              listener.onCounterTimeChange(formatTime(length),false);
          }
          myCountDownTimer= new MyCountDownTimer(length,1000);
      }
  
  
      public class MyCountDownTimer extends CountDownTimer {
  
          public MyCountDownTimer(long millisInFuture, long countDownInterval) {
              super(millisInFuture, countDownInterval);
          }
  
          public void onTick (long millisUntilFinished) {
  
              // calls the fragment's callback function passing the time value
              Log.e("in counter","1");
              if (listener!=null) {
                  Log.e("in counter","2");
                  listener.onCounterTimeChange(formatTime(millisUntilFinished), false);
              }
          }
  
          public void onFinish() {
             /*
             * write code for an alarm when timer times out
             *
             * */
              prevTimeRemaining = 0;
  
              if (listener!=null) {
                  listener.onCounterTimeChange(formatTime(length),true);
              }
  
              myCountDownTimer= new MyCountDownTimer(length,1000);
          }
      }
  
  
      public void setCounterTimeChangeListener(CounterTimeChangeListener listener) {
          this.listener = listener;
      }
  }
  </code></pre>