Android: TimerTask scheduled to restart only once

Well, this is a very strange problem that I am facing, and I am sure that I am confused somewhere, but I can not understand where.

What I'm trying is

  • Schedule a Timer to execute a TimerTask every five seconds
  • TimerTask in turn executes AsyncTask (which in this case simply falls asleep for a second before returning a static amount of AsyncTasks number).
  • Finally, the above account is updated in the user interface.

And, of course, the corresponding Handler and Runnable were used to host asynchronous messages from other threads to the user interface.

This code is executed only once. I expect him to fire every 5 seconds. Here is the code.

Note: I had no idea what to do with Looper . I put it there after trial and error!

 public class TimerAsyncMixActivity extends Activity { public static final String TAG = "TimerAsyncMix"; static int executionCount = 0; Handler mHandler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new Timer().schedule(new MyTimerTask(this), 0, 5000); } class MyAsyncTask extends AsyncTask<String, Void, Integer>{ @Override protected Integer doInBackground(String... params) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ++executionCount; } @Override protected void onPostExecute(Integer result) { mHandler.post(new UpdateUiThread(TimerAsyncMixActivity.this, result)); super.onPostExecute(result); } } } class MyTimerTask extends TimerTask{ private TimerAsyncMixActivity tma; public MyTimerTask(TimerAsyncMixActivity tma) { this.tma = tma; } @Override public void run() { Looper.prepare(); Log.d(TimerAsyncMixActivity.TAG, "Timer task fired"); tma.new MyAsyncTask().execute(); Looper.loop(); Looper.myLooper().quit(); } } class UpdateUiThread implements Runnable{ int displayCount; TimerAsyncMixActivity tma; public UpdateUiThread(TimerAsyncMixActivity tma, int i) { this.displayCount = i; this.tma = tma; } @Override public void run() { TextView tv = (TextView) tma.findViewById(R.id.tvDisplay); tv.setText("Execution count is : "+displayCount); } 

Can someone point me to what I am doing wrong?

+5
android handler android-asynctask looper timertask
source share
2 answers

techie, that’s how I implemented things like that. I will not argue that this is the best way, but it worked for me and does not look too bad.

I have the following code in my activity. I create an async task when the action starts, and I stop it onPause. AsyncTask does everything it needs and updates the onProgressUpdate () user interface (which runs in the user interface thread, so there is no need to use a handler).

 private Task task; @Override protected void onPause() { task.stop(); task = null; } @Override protected void onResume() { task = new Task(); task.execute(); } private class Task extends AsyncTask<Void, String, Void> { private boolean running = true; @Override protected Void doInBackground(Void... params) { while( running ) { //fetch data from server; this.publishProgress("updated json"); Thread.sleep(5000); // removed try/catch for readability } return null; } @Override protected void onProgressUpdate(String... values) { if( ! running ) { return; } String json = values[0]; //update views directly, as this is run on the UI thread. //textView.setText(json); } public void stop() { running = false; } } 
+5
source share

Do not use a timer. If your phone goes into sleep mode, the timer also pauses. Use AlarmManager.

+1
source share

All Articles