Skip to content Skip to sidebar Skip to footer

Executing A Thread Inside Asynctask's Doinbackground()

I have several AsyncTasks doing network operations. I was recently asked to add, after each of these operations, another network call that invokes a remote service sending some sta

Solution 1:

Try starting a second AsyncTask in the first AsyncTasks 'onPostExecute' Method. For me this worked without any issues.

Solution 2:

If you want to start thread in doInBackground method, you can start it in onProgressUpdate() method

Example:

protectedclassMyTaskextendsAsyncTask<Void,Integer,Void> {

        publicstaticfinalintSTART_THREAD= -1;
        @OverrideprotectedvoidonProgressUpdate(Integer... values) {
            if(values[0] == START_THREAD){
                startThread();
            }
        }

    @Overrideprotected Void doInBackground(Void... params) {
        publishProgress(START_THREAD);
        returnnull;
    }
}

Post a Comment for "Executing A Thread Inside Asynctask's Doinbackground()"