Android 如何在AsyncTask中举杯,提示我使用Looper

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2837676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:34:16  来源:igfitidea点击:

How to raise a toast in AsyncTask, I am prompted to used the Looper

androidandroid-asynctasktoastlooper

提问by Pentium10

I have tasks completed by AsyncTask in background. At some point I need to issue a Toast that something is completed.

我有 AsyncTask 在后台完成的任务。在某些时候,我需要发出一个 Toast 来表示某事已完成。

I've tried and I failed because Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

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

How can I do that?

我怎样才能做到这一点?

回答by Alex Volovoy

onPostExecute - executes on UI thread or publishProgress(); in your doinbackground and

onPostExecute - 在 UI 线程上执行或 publishProgress();在你的 doinbackground 和

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

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

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

回答by Hyman K Fouani

you can Toast inside doInBackground

你可以在 doInBackground 中 Toast

add this code where you want to Toast appear

将此代码添加到您想要 Toast 出现的位置

runOnUiThread(new Runnable() {
public void run() {

    Toast.makeText(<your class name>.this, "Cool Ha?", Toast.LENGTH_SHORT).show();
    }
});

回答by Alexander Oleynikov

You can also use runOnUiThreadmethod to manipulate your UI from background threads.

您还可以使用runOnUiThread方法从后台线程操作您的 UI。

回答by Korean

If you want to use Toast You should use this method : onProgressUpdate()

如果你想使用 Toast 你应该使用这个方法: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(); 
}

回答by Brandon O'Rourke

If you want to display the Toast from the background thread you'll have to call runOnUiThread from doInBackground. I don't believe there's another way.

如果要从后台线程显示 Toast,则必须从 doInBackground 调用 runOnUiThread。我不相信还有其他方法。

Edit: I take that back. I think you can implement onProgressUpdate, which runs on the UI thread, to show the Toast and make calls to publishProgress from doInBackground.

编辑:我收回。我认为您可以实现在 UI 线程上运行的 onProgressUpdate 来显示 Toast 并从 doInBackground 调用 publishProgress。

回答by Nilay

If you want to display the Toast in doInBackground, you can use it in the OnPostExecute method of AsyncTask.

如果要在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
}