title: High frequency Android UI Thread updating from a separate thread?
body: <p>I'm trying to update an EditText at a high rate of speed so the numbers flow more smoothly.  But I've tried different combinations of techniques and objects, such as AsyncTask, Thread->Runnable with a handler, Timer->TimerTask with a handler, and no matter how long I set the timing for, it doesn't seem to get any better than approximately 1 second.</p>
  
  <p>I'm thinking that it may be that fact that it is having to travel through the message pump, but I can't be sure.  Does anyone have any idea how I could get an update frequency of at least 250 to 500 milliseconds?  Higher frequency would be preferable, but that would do.</p>
  
  <p>Update 2:
  I commented out everything but the count++; and I update one of the edit texts with this value and it is indeed updating very fast.  So something that is happening in updateUi() is slowing it down.  I just don't know what.</p>
  
  <p>Update 1:
  I switched real-time usage of SharedPreferences which I was using to test with primitives just in case that was part of the problem.  The performance appears to be the same.  The only think that happens on a regular basis is an updateUi() function which runs on a timer.  The code is below, and this is what I'm expecting to update quickly, but doesn't:</p>
  
  <pre><code>private void updateUi() {
  
      lastDate = cal;
      cal = Calendar.getInstance();
  
      synchronized(cal) {
  
          //if the current date is greater than the last stored date
          if(compareDates(cal, lastDate) &gt; 0 &amp;&amp; count == 0) {
              //disable timer
              calorieTimer.cancel();
              //dereference timer...probably not necessary
              calorieTimer = null;
              double bankBmr = currentBmr.getBMR() * 
                      activityLevelMultipliers[activityLevelSpinner.getSelectedItemPosition()];
              metrics.calorieBank += (int)bankBmr - metrics.caloriesEaten;
              prefsEditor.putInt("CALORIE_BANK", metrics.calorieBank);
              prefsEditor.putInt("CALORIES_EATEN", 0);
              prefsEditor.commit();
              metrics.caloriesEaten = 0;
              //update lastDate to prevent multiple calorie banking
              lastDate = cal;
              count++;
              Log.e("updateUi()", "count #" + count);
              //set up timer again
              setupCalorieTimer();
          }
  
          caloriesEatenEditText.setText("" + metrics.caloriesEaten);
          caloriesRemainingEditText.setText(String.format("%d", (int)activeBmr - metrics.caloriesEaten));
          bankEditText.setText("" + metrics.calorieBank);
          estimatedWeightEditText.setText(String.format("%.2f", metrics.currentWeight - metrics.calorieBank / 3500.0)) ;
          //update the time
          time.setToNow();
          //update calories available
          caloriesAvailableEditText.setText(
                  String.format("%.2f", activeBmr * percentageOfDay(time) - metrics.caloriesEaten));
  
      } //End synchronized(cal)
  
  } //End updateUi()
  
  private void setupCalorieTimer() {              
      //create handler to post update back to ui thread
      final Handler handler = new Handler();          
  
      calorieTimer = new Timer();
  
      calorieTimer.schedule(new TimerTask() {
          @Override
          public void run() {
              //post to ui thread
              handler.post(new Runnable() {
                  @Override
                  public void run() {
                      updateUi();
                  }                   
              });     
          }           
      }, 0, 50);      
  }
  </code></pre>