6

I am trying to execute same Asynctask sequentially. For example, Let there be an Asynctask A. Now assume A checks if some server is busy or not. If it's busy, it checks again.... And so on. Let's say it checks this for 5 times then let users know that the service is unavailable.
So same Asynctask can obviously be used.
Trying to achieve it with loops may yield unexpected result as according to this link,
Asynctask is a fire-and-forget instance and AsyncTask instances can only be used one time..
So looping 5 times means Asynctask will be called 5 times. But if the server is free on the 2nd attempt, I don't need additional checking. Also android application may hang(mine did). Correct me if I'm wrong on this topic.

Also the answer was, "calling task like new MyAsyncTask().execute("");"
So if i do like this -- for example,

new someAsyncTask() {
            @Override
            protected void onPostExecute(String msg) {
                new someAsyncTask() {
                    @Override
                    protected void onPostExecute(String msg) {
                      ....and so on 5 times.
                   }
               }.execute(this);
            }
        }.execute(this);

It should be called 5 times sequentially and no problems should occur.
Is it the only way to do this or any other solution is present?

Community
  • 1
  • 1
Shubhashis
  • 10,411
  • 11
  • 33
  • 48

1 Answers1

4

You could use a field variable in the outer class:

private int count;
private void attemptConnect(){
    count = 0;

    new MyTask().execute("");
}

class MyTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        count++;
        if(count < 5){
            new MyTask().execute("");
        }
    }
}

Or pass in a count to the AsyncTask constructor:

private void attemptConnect(){
    new MyTask(0).execute("");
}

class MyTask extends AsyncTask<String, String, String> {
    private int count;

    public MyTask(int count){
        this.count = count;
    }

    @Override
    protected String doInBackground(String... params) {
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        if(count < 4){
            new MyTask(++count).execute("");
        }
    }
}
tachyonflux
  • 20,103
  • 7
  • 48
  • 67
  • if i use anonymous asynctask will this work? i mean in the example i provided in the question, i implemented the asynctask class in other java class. I just used it on another class. will this work there? because then i would have to write the `onPostExecute` again. – Shubhashis May 02 '15 at 16:58
  • I posted an alternative that would work for a standalone class. – tachyonflux May 02 '15 at 16:59
  • But I'm using anonymous Asynctask. and in the standalone class onPostExecute was not overridden. the overriding was done in another class. The example you provided will work if onPostExecute was on standalone class. But will it work on anonymous Asynctask? – Shubhashis May 02 '15 at 17:04
  • I mean like this `new someAsyncTask() { @Override protected void onPostExecute(String msg) { } }.execute(this);` for this type of implementation how can i call that Asynctask sequentially? – Shubhashis May 02 '15 at 17:05
  • 1
    Nope, not possible since async tasks can only be executed 1 time and you can't create another instance of an anonymous class. – tachyonflux May 02 '15 at 21:47