Skip to content Skip to sidebar Skip to footer

How To Show An Alertdailogue In Doinbackground(..) Of An Asycnctask Running..?

Error: Getting error with UI thread.. when I'm trying to show an alert inside the doInBackground. Is there any posibility do this stuff? private class LoggingTask extends A

Solution 1:

Do in background method doesnot allowed for UI changes,if you want to make any UI changes do in onpostexecute method

Solution 2:

Do this way

@OverrideprotectedIntegerdoInBackground(String... params) {
        // Get the sever connection and return the statusif (status == 0) {
            newHandler().post(newRunnable() {

                @Overridepublicvoidrun() {
                    // show an alert here....

                }
            });

        }
        return status;
    }

Solution 3:

Make a method that creates the dialog in your activty ( context would be the activity ) not in the AsyncTask. So when your

if ( status == 0 )

returns true, you call the method from the UI thread. In this way you are not creating the dialog in the AsyncTask and it should work.

Solution 4:

doInBackground() is not the right place to do the UI related Task. You have to do it in onPostExecute. Or you can can start the dialog in OnPreExecute() and than cancel the dialog onPostExecute().

Solution 5:

Async tasks don't run on the ui thread that's why you can't do that. Store a boolean. Then call the alert dialog after the asynchronous task completes in the onpostexecute method.

Post a Comment for "How To Show An Alertdailogue In Doinbackground(..) Of An Asycnctask Running..?"