Alin Alin -4 years ago 125
Android Question

Asynctask and queue OR delay in execution

Scenario:

The user has a list of items, let's say 10 items. Each item has an Operation button, which calls an AsyncTask which makes a web call. When a call is made, the item displays a spinner during the execution of the task

Problem:

Some of the users abuse this, and press quickly more Operation buttons, quicklt one after another, executing the web calls too often. So I want to be able to somehow, execute each of the AsyncTasks one after another with a delay of 2 seconds between executions. I do not want to switch to something else from AsyncTask if possible. So basically if there are 3 Operation buttons pressed, the execution should be:

-> Operation 1
-> 2 seconds delay
-> Operation 2
-> 2 seconds delay
-> Operation 3
-> ....

What would be the best way to do this in Android?

LE:

I have just realized something, for executing my task I ran the following code:

myTask = new MyTask();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
myTask.execute();
}


Well, I've been using this code for a lot of time now, knowing that after honeycomb the tasks were not executed in parallel anymore without using an Executor. So it seems that only doing a simple myTask.execute() and adding a Thread.sleep() makes my AsyncTasks execute, one after another just as expected.

Answer Source

You will need to maintain a list of the operations that needs to be performed. on click of the button add the task in the list, call a method which check the list for the task and executes it if there is no other task is running..

in onPostExecute method call the same method to check if there is any other task / operation that needs to be performed..

It may not be the full code you require... but may give you some idea..

public class TestActivity extends AppCompatActivity {

    private static boolean isTaskRunning =false;
    static ArrayList<CustomTask> customTaskList = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
    }

    public void onBtnClick(View view)
    {
        // create custom task with required values and actions 
        CustomTask customTask = new CustomTask();
        customTaskList.add(customTask);         

        checkAndExecuteTask();
    }


    private static void checkAndExecuteTask()
    {
        //checks if there is any task in the list and is there any other         running task
        if(customTaskList.size()>0 && !isTaskRunning) {
            new MyAsync(customTaskList.get(0)).execute();
        }
    }

    static class MyAsync extends AsyncTask<Void,Void,Void>
    {
        CustomTask currentCustomTask;

        public MyAsync(CustomTask customTask)
        {
            currentCustomTask = customTask;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            isTaskRunning= true;
        }

        @Override
        protected Void doInBackground(Void... voids) {

            // do your stuff
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            customTaskList.remove(currentCustomTask);
            isTaskRunning =false;
            checkAndExecuteTask(); // task is completed so check for another     task and execute (if any).
        }
    }

    class CustomTask
    {
        // create class with required fields and method
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download