1

I'm running httpsCommand (shown below, via clientTask() from MainActivity) and downloading about 1KB of data from a webserver. I plan to update a ListView in MainActivity (I think that's possible, but I recall it being a bit annoying last time I did it) with the downloaded data inside myListAllDoneListener(). I'd like to run this in a loop every 5 minutes to check for new data.

I've tried running new myCLientTask().execute()... inside a while loop (using Thread.sleep and try/catch) but it only seemed to run one loop and crashed after 15 seconds or so. Found a similar question but it's not quite answered. How can I background this data download?

// ** MainActivity.java **
public class MainActivity extends AppCompatActivity
{
   ...
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {    
    new clientTask(getApplicationContext(), myListAllDoneListener, ...);
        ...

   private AsyncTaskCompleteListener myListAllDoneListener = new AsyncTaskCompleteListener()
   {
      @Override
      public void onComplete(ArrayList<String> data, String msg, int statuscode)
      {
        // update listview with new data
        ...


//** clientTask.java **
public class clientTask extends Activity
{ 
  ArrayList<String> mData;
  ...

  public clientTask (Context ctx, AsyncTaskCompleteListener ... String cmd, ...)
  {
    ...
    new myClientTask().execute();
    ...


  private class myClientTask extends AsyncTask<Object, Object, Object>
  {
  ...

  protected Object doInBackground (Object... params)
  {
     mData = httpsCommand (mCmd);
     ...
Will T.
  • 11
  • 2

1 Answers1

0

You can't use Thread.sleep in activity. It will block your UI. To run a periodic task Look this stackoverflow answer using Handlers. Also, there are some things wrong with your code. You shouldn't create object of an Activity class and use it for such things.

karandeep singh
  • 2,294
  • 1
  • 15
  • 22