1

Imagine a chat-room style activity. It has a listview of the most recent chat messages.. Every 5 seconds I pull new messages from the server..

I am trying to find the best way to periodically do the server fetch in the background. I had it working great with ASyncTask in onCreate but then had to decide how to keep making it recurring..

When I start the download from server I display the text "Reloading" on screen, and I clear it when I am done..

I looked at:

1) Having ASyncTask doInBackground() run an infinite while() loop with a 5-second Thread.sleep() and regular publishUpdate but that didn't allow me to turn on and off the "Reloading" text as I need to send a message to the UI thread at the beginning and end of every while loop iteration.

2) I next looked at using a simple Thread.run(), t.start() in my onCreate method with a while loop and usng a Handler to pass the message to/from the UI to display/hide the Reloading text.. This works okay but now my issue is:

How to stop/resume the thread in onPause and onResume?

I had the while() loop in my run() function use a variable - while(runThread) and I set runThread to false in onPause.. but the Thread does not get cleaned up by the OS.. When I come back to onResume and I do t.start(), I throw an exception that the thread is already running...

Any thoughts on how to make this work? Is there a better approach I am missing? Given that I want a simple 5-second server fetch and only while this activity is in the foreground..

Cheers and thanks!

Daniel Guillamot
  • 843
  • 2
  • 9
  • 18

3 Answers3

0

What i would do:

create this class

public class GetInfosTask extends AsyncTask<A , B, C> {
     protected C doInBackground(A...) { //this is called in the ui thread
         show the text
     }

     protected void onProgressUpdate(B... progress) {
         download
     }

     protected void onPostExecute(C result) {//this is called in the ui thread
         hide the text
     }
 }

and then initialize the process with

mThread = new MyThread(new Runnable() {
    public void run() {
      pActivity.runOnUiThread(new Runnable() {
             new GetInfoTask().execute(arg1, arg2, arg3);
      });
      wait(5);
  });
mThread.start();

For pause/resuming the thread you can look here. I wouldn't "clean" the Thread, i would just pause it when i don't need it, and then restart it when i need it.

EDIT: corrected very stupid errors! Thanks Lalit and yorkw

Makers_F
  • 3,033
  • 4
  • 36
  • 51
  • [`AsyncTask instances can only be used one time.`](http://stackoverflow.com/questions/6373826/execute-asynctask-several-times) – Lalit Poptani Jan 07 '12 at 13:20
  • I doubt this will work, as it stated in the API, AsyncTask.execute() must involved on the UI thread. – yorkw Jan 08 '12 at 08:58
0

you can call thread.interrupt() to stop a thread, a thread can't be resumed, so every time you want to restart the thread, you need to create a new thread.

jeet
  • 29,001
  • 6
  • 52
  • 53
0

I would use a repeating alarm to handle the timing instead of managing a thread (less risky/cleaner).

You'd need to create a BroadcastReceiver to handle the alarm which could then execute the AsyncTask or put the work in an IntentService.

You can set the alarm up when your activity is started and remember to remove the alarm when it's stopped.. e.g. onResume() and onPause().

denizmveli
  • 2,558
  • 1
  • 23
  • 18