代码之家  ›  专栏  ›  技术社区  ›  Pentium10

如何在AsyncTask中举杯,提示我使用循环器

  •  25
  • Pentium10  · 技术社区  · 14 年前

    我有AsyncTask在后台完成的任务。在某个时刻,我需要发出一个祝酒词,表示某件事已经完成。

    我尝试过但失败了,因为 Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

    我该怎么做?

    6 回复  |  直到 9 年前
        1
  •  37
  •   Alex Volovoy    14 年前

    onPostExecute-在UI线程上执行 或 publishProgress() 在你的背景下

    protected void onProgressUpdate(Integer... progress) {
    }
    

    http://developer.android.com/reference/android/os/AsyncTask.html

        2
  •  23
  •   Noob    11 年前

    你可以在室内烤面包

    将此代码添加到要Toast显示的位置

    runOnUiThread(new Runnable() {
    public void run() {
    
        Toast.makeText(<your class name>.this, "Cool Ha?", Toast.LENGTH_SHORT).show();
        }
    });
    
        3
  •  19
  •   Alexander Oleynikov    14 年前

    您也可以使用 runOnUiThread 方法从后台线程操作用户界面。

        4
  •  9
  •   Korean    13 年前

    如果你想用烤面包 您应该使用以下方法:onProgressUpdate()。

    protected Integer doInBackground(Void...Params) {
       int check_point = 1;
       publishProgress(check_point);
       return check_point;
    }
    
    protected void onProgressUpdate(Integer integers) {
      if(integers == 1) {
        Toast.makeText(classname.this, "Text", 0).show(); 
    }
    
        5
  •  1
  •   Brandon O'Rourke    14 年前

    如果您想显示后台线程中的Toast,那么您必须从doinbackground调用runounithread。我不相信还有别的办法。

    编辑:我收回。我认为您可以实现在UI线程上运行的OnProgressUpdate,以显示Toast并从DoinBackground调用PublishProgress。

        6
  •  1
  •   Pang Ajmal PraveeN    9 年前

    如果要在doinbackground中显示toast,可以在AsyncTask的onPostExecute方法中使用它。

    protected void onPostExecute(String file_url) {    
       Toast.makeText(getApplicationContext(),"Your Message", Toast.LENGTH_LONG).show();
    
       pDialog.dismiss();//dismiss the progress dialouge
    }