【问题标题】:How to show toast in AsyncTask in doInBackground如何在 doInBackground 的 AsyncTask 中显示 toast
【发布时间】:2012-11-27 05:46:38
【问题描述】:

在我的一项活动中,我使用了AsyncTask。在doInBackground() 中,我正在调用各种方法。在其中一种方法中,我遇到了异常,因此在 catch 块中,我想在 Toast 中显示错误。 我知道我可以使用Log,但我仍然更喜欢 Toast。 那么,如何在 doInBackground() 中的 AsyncTask 中使用 Toast?

【问题讨论】:

  • 为什么你没有从 doInBackground() 将错误重新调整到 onPostExecute,然后在 onPostExecute 的吐司中出现这个错误
  • 你不能从 doInBackground() 方法修改 UI,尝试返回一些结果,并在 onPostExecute() 方法中测试该结果,如果是,那么显示 Toast跨度>
  • @Sam:简单的人!我没有投反对票,我刚刚添加了我的评论,我知道您可以在 doInBackground() 中授予访问权限,但不建议这样做,如果是这样,那么为什么有方法 onProgressUpdate() 和 @ 987654326@??
  • 在这里考虑用户。真正的答案是,你可能一开始就不应该扔 Toast。作为开发人员,很高兴看到您在后台尝试执行的任何操作的更新。但是你的用户真的在乎吗?当您尝试从 doInBackground 更新 UI 时,您正在与框架作斗争。

标签: android android-asynctask


【解决方案1】:

从doInBackground返回

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
    }
}

【讨论】:

    【解决方案2】:

    您可以将 Toast 包装在 runOnUIThread() 中,但这不是最佳解决方案。
    发生错误时,您应该在 catch 块中设置一个布尔标志,然后在 onProgressUpdate()onPostExecute() 或任何其他具有 UI 访问权限的方法中显示适当的 Toast,只要标志为 true

    【讨论】:

    • 我也不明白... +1 了
    • 应该将哪个 Context 传递给 toast getBaseContext()getApplication() Context?
    【解决方案3】:

    doInBackground() 方法中必须显示 toast 的地方编写以下代码

    runOnUiThread(new Runnable() {
    
    public void run() {
    
      Toast.makeText(getApplicationContext(), "Example for Toast", Toast.LENGTH_SHORT).show();
    
       }
    });
    
    • 顺便说一句:如果您使用的是Fragments,则需要通过您的活动调用runOnUiThread(...)

    getActivity().runOnUiThread(...)

    【讨论】:

      【解决方案4】:

      您可以在可以访问 UI 线程的方法中显示它,例如 onPreExecute()onProgressUpdate()onPostExecute()

      【讨论】:

        【解决方案5】:

        创建一个处理程序对象并使用它执行所有 Toast 消息。

        @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(); 
                }
            });
          }
        

        【讨论】:

          【解决方案6】:
          runOnUiThread(new Runnable() {
          
          public void run() {
          
            Toast.makeText(getApplicationContext(), "Example for Toast", Toast.LENGTH_SHORT).show();
          
             }
          }); 
          

          在 doInBackground() 方法中显示 toast 效果很好

          【讨论】:

            【解决方案7】:
            activity.runOnUiThread(new Runnable() {
             public void run() 
             {
                Toast.makeText(activity, "Toast teaxt", Toast.LENGTH_SHORT).show();
             }
            });
            

            【讨论】:

              【解决方案8】:

              试试这个代码

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

              【讨论】:

                【解决方案9】:

                如果你必须声明任何与 Thread 相关的东西,那么它必须在 runOnUiThread() 方法之外,那么只有它会执行,

                    @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 "";
                    }
                

                【讨论】:

                  猜你喜欢
                  • 2014-12-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-05-18
                  • 1970-01-01
                  • 2014-05-25
                  • 2014-05-23
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多