title: Android - Calculate Time in Background Even When App is Closed
body: <p>I have a <strong>textview</strong> which acts a an <strong>stepuptimer</strong>. I used a handler and updating it every second and its working perfectly. But when I close the app and open, the timer starts from "0". I want the time to be running background and when I opened the activity, the elapsed time should be displayed instead of starting from 0 again. The displaying time format is <strong>00:00:00</strong>.</p>
  
  <p>My code is</p>
  
  <pre><code>public class GoOnlinePage extends Activity { 
  
  private TextView Tv_timer;
  private Handler onlineTimeHandler = new Handler();
  private long startTime = 0L;
  private long timeInMilliseconds = 0L;
  private long timeSwapBuff = 0L;
  private long updatedTime = 0L;
  private int mins;
  private int secs;
  private int hours;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.go_online_page);
  
      Tv_timer = (TextView) findViewById(R.id.txt_timer);
  
       startTime = SystemClock.uptimeMillis();
  
      onlineTimeHandler.post(updateTimerThread);
  
  
    }
  
  
  
    private Runnable updateTimerThread = new Runnable() {
  
      public void run() {
  
          timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
  
          updatedTime = timeSwapBuff + timeInMilliseconds;
  
          secs = (int) (updatedTime / 1000);
          hours = secs / (60 * 60);
          mins = secs / 60;
          secs = secs % 60;
          if (mins &gt;= 60) {
              mins = 0;
          }
  
  
          Tv_timer.setText(String.format("%02d", hours) + ":" + 
           String.format("%02d", mins) + ":"
                  + String.format("%02d", secs));
  
  
  
          onlineTimeHandler.postDelayed(this, 1 * 1000);
  
      }
  
  };
  
  }
  </code></pre>
  
  <p>I tried using session and updating the time each second inside <strong>updateTimerThread</strong>. When I open back the app, I was getting the current time and finding difference between the two times and updating the timer back. But nothing worked. </p>
  
  <p>Please help me. Thanks in Advance.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>SOLVED</strong></p>
  
  <p>I solved it by using a <strong>Service</strong> and updated the <strong>textview</strong> by using a <strong>BroadcastReceiver</strong>. I have attached the code below for your reference.</p>
  
  <pre><code>import android.app.Service;
  import android.content.Intent;
  import android.os.Binder;
  import android.os.Handler;
  import android.os.IBinder;
  import android.os.SystemClock;
  import android.util.Log;
  
  
  
  public class TimerService extends Service {
      private static String LOG_TAG = "TimerService";
      private IBinder mBinder = new MyBinder();
  
      Handler onlineTimeHandler = new Handler();
      long startTime = 0L, timeInMilliseconds = 0L, timeSwapBuff = 0L, updatedTime = 0L;
      int mins, secs, hours;
      String time = "";
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          return super.onStartCommand(intent, flags, startId);
      }
  
      @Override
      public void onCreate() {
          super.onCreate();
          Log.v(LOG_TAG, "in onCreate");
          startTime = SystemClock.uptimeMillis();
          onlineTimeHandler.post(updateTimerThread);
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          Log.v(LOG_TAG, "in onBind");
          return mBinder;
      }
  
      @Override
      public void onRebind(Intent intent) {
          Log.v(LOG_TAG, "in onRebind");
          super.onRebind(intent);
      }
  
      @Override
      public boolean onUnbind(Intent intent) {
          Log.v(LOG_TAG, "in onUnbind");
          return true;
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          Log.v(LOG_TAG, "in onDestroy");
          if(onlineTimeHandler!=null){
              onlineTimeHandler.removeCallbacks(updateTimerThread);
          }
      }
  
  
      public class MyBinder extends Binder {
          TimerService getService() {
              return TimerService.this;
          }
      }
  
      private Runnable updateTimerThread = new Runnable() {
  
          public void run() {
  
              timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
  
              updatedTime = timeSwapBuff + timeInMilliseconds;
  
              secs = (int) (updatedTime / 1000);
              hours = secs / (60 * 60);
              mins = secs / 60;
              secs = secs % 60;
              if (mins &gt;= 60) {
                  mins = 0;
              }
  
              time = String.format("%02d", hours) + ":" + String.format("%02d", mins) + ":"
                      + String.format("%02d", secs);
  
  
              Intent intent = new Intent();
              intent.setAction("com.app.Timer.UpdateTime");
              intent.putExtra("time", time);
              sendBroadcast(intent);
  
              onlineTimeHandler.postDelayed(this, 1 * 1000);
  
          }
  
      };
  }
  </code></pre>