title: Android - implement a custom timer/clock
body: <p>I have an android app where user presses a start button and some function of collecting data is started. I have a custom EditText that shows the time that all this process has taken and is updated every second till user presses stop. I implemented this using an AsyncTask like this below: </p>
  
  <pre><code>    protected class RecTimer extends AsyncTask&lt;Context, Integer, String&gt; 
      {
                  @Override
                  protected String doInBackground( Context... params ) 
                  {
                           int sec=00;
                       int min=00;
                       int hours=00;
                       timerdata=timer.split(":");
                       hours=Integer.parseInt(timerdata[0]);
                       min=Integer.parseInt(timerdata[1]);
                       sec=Integer.parseInt(timerdata[2]);
  
                          while(Recording==true) 
                          {
                              try{
  
                                          Thread.sleep( 1000 );
                                          publishProgress( sec , min , hours );
                                          sec++;
  
                                          if(sec==60){
  
                                              min++;
                                              sec=0;
  
                                              if(min==60){
                                                  hours++;
                                                  min=0;
                                              }
                                          }
                                  } catch( Exception e ){
                                          Log.i("makemachine", e.getMessage() );
                                  }
                          }
                          return "COMPLETE!";
                  }
  
                  @Override
                  protected void onPreExecute() 
                  {
                          Log.i( "makemachine", "onPreExecute()" );
                          super.onPreExecute();                   
                  }
  
                  @Override
                  protected void onProgressUpdate(Integer... values) 
                  {
                          super.onProgressUpdate(values);
                          //Log.i( "makemachine", "onProgressUpdate(): " +  String.valueOf( values[0] ) );
  
                          String format = String.format("%%0%dd", 2);
                          String Sec = String.format(format, values[0]);
                          String Min = String.format(format, values[1]);
                          String Hours = String.format(format, values[2]);
  
                          ((EditText) findViewById(R.id.duration1)).setText(Hours + ":"+ Min + ":" + Sec);
  
                  }
  
  
                  @Override
                  protected void onPostExecute( String result ) 
                  {      
                          super.onPostExecute(result);
                          Log.i( "makemachine", "onPostExecute(): " + result );
  
                 }          
          } 
  </code></pre>
  
  <p>It seemed to work fine at first, but now I see that if the app is idle and out of focus the timer may not always count well. I mean that I leave for example the device for 15 minutes and then see that in my “clock” only 5 have passed. Since the view is back on focus everything seems to work normally again.</p>
  
  <p>First of all why does this happen? Shouldn’t the AsyncTask be always executed in background? Is there some other better approach I should use to implement this?</p>
  
  <p>Thanks to everybody in advance!</p>
  
  <p>EDIT: Thanks to TrackRuler I implemented it like this:</p>
  
  <p>private Runnable Timer = new Runnable() {</p>
  
  <pre><code>  public void run() {
             long current = SystemClock.uptimeMillis();
                 String format = String.format("%%0%dd", 2);
                 timerdata=timer.split(":");
                 sec++;
  
                 if(sec==60){
  
                     min++;
                     sec=0;
  
                     if(min==60){
                         hours++;
                         min=0;
                     }
                 }
  
                 String Sec = String.format(format, sec);
                 String Min = String.format(format, min);
                 String Hours = String.format(format, hours);
  
                 ((EditText) findViewById(R.id.duration1)).setText(Hours + ":"+ Min + ":" + Sec);
  
  
                     TimerHandler.postAtTime(this,
                            current +  1000);
                      }                       
              }; 
  </code></pre>
  
  <p>From my main code I call it from a handler:
  TimerHandler.post(Timer);</p>
  
  <p>It works by now.. I just want to know if there is a problem in updating the view inside the Runnable(?)</p>