How to show toast in AsyncTask in doInBackground

后端 未结 9 720
灰色年华
灰色年华 2020-11-27 23:01

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 exc

相关标签:
9条回答
  • 2020-11-27 23:33

    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
        }
    }
    
    0 讨论(0)
  • 2020-11-27 23:35
    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

    0 讨论(0)
  • 2020-11-27 23:35
    activity.runOnUiThread(new Runnable() {
     public void run() 
     {
        Toast.makeText(activity, "Toast teaxt", Toast.LENGTH_SHORT).show();
     }
    });
    
    0 讨论(0)
提交回复
热议问题