reiley
reiley

Reputation: 3761

How to show toast in AsyncTask in doInBackground

In one of my activities I'm using AsyncTask. In doInBackground() I'm making calls to various methods. In one of these methods I'm getting an exception, so in the catch block I want to show the error in the Toast. I know I can use Log, but still I prefer Toast. So, how can I use Toast in AsyncTask in doInBackground()?

Upvotes: 14

Views: 24939

Answers (9)

smit mehta
smit mehta

Reputation: 101

If you have to declare anything related to Thread, then it must be outside the runOnUiThread() method, then only it is going to execute,

    @Override
    protected String doInBackground(String... strings) {
        for(int i=0; i<10; i++) {

            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(getApplicationContext(), "Example for Toast "+i, Toast.LENGTH_SHORT).show();
                }
            });

            try {
                Thread.sleep(10);
            } catch (Exception e) {}
        }

        return "";
    }

Upvotes: 0

Ashish Tikarye
Ashish Tikarye

Reputation: 870

try this code

void showError(final String err) {
    runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(downloadprogress.this, err + "error in download", Toast.LENGTH_LONG)
                    .show();
        }
    });
  }

Upvotes: 0

user3073127
user3073127

Reputation: 29

runOnUiThread(new Runnable() {

public void run() {

  Toast.makeText(getApplicationContext(), "Example for Toast", Toast.LENGTH_SHORT).show();

   }
}); 

is working perfectly fine to show toast in doInBackground() method

Upvotes: 1

Rasel
Rasel

Reputation: 5734

activity.runOnUiThread(new Runnable() {
 public void run() 
 {
    Toast.makeText(activity, "Toast teaxt", Toast.LENGTH_SHORT).show();
 }
});

Upvotes: 0

Harshal Voonna
Harshal Voonna

Reputation: 349

Create a handler object and execute all your Toast messages using that.

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

    Handler handler=new handler();
    handler=  new Handler(context.getMainLooper());
    handler.post( new Runnable(){
        public void run(){
            Toast.makeText(context, "Created a server socket",Toast.LENGTH_LONG).show(); 
        }
    });
  }

Upvotes: 3

Harshad07
Harshad07

Reputation: 608

Write the following code where you have to show toast in doInBackground() method

runOnUiThread(new Runnable() {

public void run() {

  Toast.makeText(getApplicationContext(), "Example for Toast", Toast.LENGTH_SHORT).show();

   }
});
  • BTW: if you are using Fragments, you need to call runOnUiThread(...) through your activity:

getActivity().runOnUiThread(...)

Upvotes: 6

Sam
Sam

Reputation: 86948

You could wrap the Toast in runOnUIThread() but this isn't the best solution.
You should set a boolean flag in the catch block when an error occurs, then display an appropriate Toast in onProgressUpdate(), onPostExecute(), or any of the other methods with UI access whenever the flag is true.

Upvotes: 13

Robin Chander
Robin Chander

Reputation: 7425

return from doInBackground as

protected String doInBackground(String... params){
    //some code
    try{
       //some code
     }catch(Exception e){
        return "Exception Caught";
     }
     return someValidResult;
}

protected void onPostExecute(String result){
    if(result.equalsIgnoreCase("Exception Caught")){
       //Display Toast
    }else{
       // // whatever you wana do with valid result
    }
}

Upvotes: 17

Ahmad
Ahmad

Reputation: 72603

You can display it in a method, that has access to the UI thread like onPreExecute(), onProgressUpdate() and onPostExecute()

Upvotes: 3

Related Questions