title: Android timer that counts when app is closed
body: <p>i have an activity in which i want to track the time elapsed between two actions(for ex startAction and endAction).I've used the following code to implement a timer that increments ever 500 ms after the user press the button for startAction:</p>
  
  <pre><code>TextView dayTimer = (TextView)findViewById(R.id.tvDayTimer);
  long startTime;
  class DayTimer extends TimerTask {
  
               @Override
               public void run() {
                   BBCAndroid.this.runOnUiThread(new Runnable() {
  
  
                       public void run() {
                          long millis = SystemClock.elapsedRealtime() - startTime;
                          int seconds = (int) (millis / 1000);
                          int minutes = seconds / 60;
                          seconds     = seconds % 60;
  
                          dayTimer.setText(String.format("%d:%02d", minutes, seconds));
                       }
                   });
               }
          };
  
  
        startDayButton.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
                              startTime = SystemClock.elapsedRealtime();
                          timer.schedule(new DayTimer(),  0,500);
                          LoginResult.DayState = 1;
                          startDayButton.setEnabled(false);
                          endDayButton.setEnabled(true);
              }
          });
  </code></pre>
  
  <p>Now my problem is that,if i exit the application the timer stops.How can i make it run even when the user is out of the app?
  To sum up ,i need a timer that counts even if the app is closed.</p>