Skip to content Skip to sidebar Skip to footer

Can't Show Alertdialog In Doinbackground

I am trying to use a AlertDialog in Android which will notify the users that they are runing in offline mode after checking the internet connection. I have used the following codes

Solution 1:

doInBackground() runs on a separate thread, other than the main thread. You can use UI elements only on main thread. Try using runOnUiThread() method inside doInBackground() to show the dialog.

Solution 2:

here AlertDialog will not work, you can use System.out.println("some text"); if you are using alert just for testing purpose. otherwise you have to set into a parameter and what ever text you want to put in alertbox and just use it in main thread.

if you have any confusion please let me know .

Solution 3:

In Android you can do UI operations only in main thread. So you can show dialog in onPostExecute method. Example:

classanyClassNameextendsAsyncTask<String,String,String>{

    /* uses worker thread */protectedStringdoInBackground(Void... params) {
                if (this.isOnline()) {
                        newGetJson().execute();
                } else { returnnull }
// ... rest of your codereturn""
    }

    /* uses main thread*/protectedvoidonPostExecute(String result){
    if(result == null ){
    AlertDialog.Builder builder1 = newAlertDialog.Builder(homeFragment);
            builder1.setMessage("INTERNET CONNECTION NOT AVAILABLE. Now you are viewing the news in Offline Mode.");
            builder1.setCancelable(true);

            AlertDialog alert1 = builder1.create();
            alert1.show();
    }
    }
    }

Solution 4:

classmyAsyncextendsAsyncTask<String, String, String>{

    @OverrideprotectedStringdoInBackground(String... arg0) {
        // TODO Auto-generated method stub          if(yourWorkIsDone){
            returnnewString("done");
        }else{
            returnnewString("Error message");
        }

        //BUT, if you'd like to check and update user on the error and keep trying then this way// use just one of these two examplesif(yourWorkIsDone){
            // definately is done but hey it depends on youpublishProgress(newString("done")); 
        }else{
            // this is when you have error and wona let the user know about and keep trying to hit it rightpublishProgress(newString("error message")); 
        }
    }

    @OverrideprotectedvoidonPostExecute(String result) {
        // TODO Auto-generated method stubsuper.onPostExecute(result);
        // here your task is done// you can put myAlertDialogToShow(Context context,String message) depending on ur choice
    }

    @OverrideprotectedvoidonPreExecute() {
        // TODO Auto-generated method stubsuper.onPreExecute();
    }

    @OverrideprotectedvoidonProgressUpdate(String... values) {
        // TODO Auto-generated method stubsuper.onProgressUpdate(values);
        // here your task is running but you are just informing user, runs on the ui..// you can put myAlertDialogToShow(Context context,String message) depending on ur choice
    }

}

voidmyAlertDialogToShow(Context context,String message){
    AlertDialog.Builder builder1 = newAlertDialog.Builder(context);
    builder1.setMessage(message);
    builder1.setCancelable(true);
    AlertDialog alert1 = builder1.create();
    alert1.show();

}

ayt..check for silly errors and spellings.. im tired

Post a Comment for "Can't Show Alertdialog In Doinbackground"