title: Which is the best method to excecute a task repeatedly in android ? (Eg :- Refreshing scores, Update Ui)
body: <p>In android there are some options for refresh handling such as <strong>Timer, TimerTask, ScheduledExecutorService, AlarmManager &amp; Handler</strong>. Which is the best method to do this. </p>
  
  <p>Did anyone checks the resource utilization of above mentioned methods?. I am listing the implementation of above mentioned methods here.</p>
  
  <p><strong>Using Handler for executing a task repeatedly</strong></p>
  
  <pre><code>final Handler handler = new Handler();
  handler.postDelayed(new Runnable() {
  
      public void run() {
          new MyScheduledTask.execute(param);
      }
  
  }, TimeInterval);
  </code></pre>
  
  <p><strong>Using Timer for executing a task repeatedly</strong></p>
  
  <pre><code>timer = new Timer();
  
  timer.scheduleAtFixedRate(new TimerTask() {
  
      synchronized public void run() {
          new MyScheduledTask.execute(param);
          }
  
  }}, 10000, 10000);
  </code></pre>
  
  <p><strong>Using ScheduledExecutorService for excuting a task repeatedly</strong></p>
  
  <pre><code>ScheduledExecutorService scheduler =
  Executors.newSingleThreadScheduledExecutor();
  scheduler.scheduleAtFixedRate
    (new Runnable() {
       public void run() {
          new MyScheduledTask.execute(param);
       }
    }, 0, 10, TimeInterval);
  </code></pre>
  
  <p><strong>Using Timer with TimerTask for executing a task repeatedly</strong></p>
  
  <pre><code>Timer timer = new Timer();
  timer.schedule(new UpdateTimeTask(),1, TimeInterval);
  class UpdateTimeTask extends TimerTask {
  
      public void run() 
         {        
          new MyScheduledTask.execute(param);
         }
  }
  </code></pre>
  
  <p><strong>AlarmManager for executing  scheduled task</strong>  </p>
  
  <pre><code>public void setupTask(){
  
      // check task is scheduled or not
      boolean alarmUp = (PendingIntent.getBroadcast(this, 0, 
              new Intent("YourPackageHere.AlarmReceiver"), 
              PendingIntent.FLAG_NO_CREATE) != null);
  
      if (  !alarmUp) {
          Intent intent = new Intent("YourPackageHere.AlarmReceiver");
          intent.putExtra("activate", true);
          PendingIntent pendingIntent =
                      PendingIntent.getBroadcast(this, 0, 
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
  
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.set(Calendar.HOUR_OF_DAY, 0);   
          calendar.set(Calendar.MINUTE, 1);
          calendar.set(Calendar.SECOND, 0);
  
          AlarmManager alarmManager =
                      (AlarmManager)
                      this.getSystemService(this.ALARM_SERVICE);
          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                      calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
                      pendingIntent);
  
          calendar = Calendar.getInstance();
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.set(Calendar.HOUR_OF_DAY, 7);   
          calendar.set(Calendar.MINUTE, 0);
  
          alarmManager = (AlarmManager)
                      this.getSystemService(this.ALARM_SERVICE);
          PendingIntent pendingIntent2 =
                      PendingIntent.getBroadcast(this, 1, 
                  intent, PendingIntent.FLAG_UPDATE_CURRENT);
          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                      calendar.getTimeInMillis(),
                      AlarmManager.INTERVAL_DAY, pendingIntent2);         
  
      }
  
  }
  </code></pre>
  
  <p>AlarmManager class</p>
  
  <pre><code>public class AlarmReceiver extends BroadcastReceiver { 
  
  @Override
  public void onReceive(Context context, Intent intent) {
  
      if (intent.hasExtra("activate")) {
  
          new MyScheduledTask.execute(param);
      }
  
  }
  }
  </code></pre>
  
  <p>Manifest</p>
  
  <pre><code>&lt;receiver android:name="YourPackageHere.AlarmReceiver"&gt;&lt;/receiver&gt;
  </code></pre>