125

如何显示来自线程的Toast消息?

4

12 回答 12

256

您可以通过从线程中调用Activity's方法来做到这一点:runOnUiThread

activity.runOnUiThread(new Runnable() {
    public void run() {
        Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
    }
});
于 2010-06-28T17:37:15.537 回答
67

我喜欢在我的活动中调用一个方法,showToast我可以从任何地方调用它......

public void showToast(final String toast)
{
    runOnUiThread(() -> Toast.makeText(MyActivity.this, toast, Toast.LENGTH_SHORT).show());
}

然后,我最常在MyActivity这样的任何线程上从内部调用它...

showToast(getString(R.string.MyMessage));
于 2012-10-15T14:06:32.153 回答
32

这与其他答案类似,但已针对新的可用 API 进行了更新,并且更加简洁。此外,不要假设您处于活动上下文中。

public class MyService extends AnyContextSubclass {

    public void postToastMessage(final String message) {
        Handler handler = new Handler(Looper.getMainLooper());

        handler.post(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
            }
        });
    }
}
于 2015-12-10T23:12:05.180 回答
23

一种在几乎任何地方都有效的方法,包括从没有Activityor的地方View,将 a 抓取Handler到主线程并显示 toast:

public void toast(final Context context, final String text) {
  Handler handler = new Handler(Looper.getMainLooper());
  handler.post(new Runnable() {
    public void run() {
      Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }
  });
}

这种方法的优点是它适用于任何Context,包括ServiceApplication

于 2016-12-06T22:12:08.280 回答
10

这样还是这样,用Runnable那个表示了Toast。即,

Activity activity = // reference to an Activity
// or
View view = // reference to a View

activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        showToast(activity);
    }
});
// or
view.post(new Runnable() {
    @Override
    public void run() {
        showToast(view.getContext());
    }
});

private void showToast(Context ctx) {
    Toast.makeText(ctx, "Hi!", Toast.LENGTH_SHORT).show();
}
于 2010-06-28T17:35:42.660 回答
6

有时,您必须将消息从另一个发送Thread到 UI 线程。当您无法在 UI 线程上执行网络/IO 操作时,就会出现这种情况。

下面的示例处理该场景。

  1. 你有 UI 线程
  2. 您必须启动 IO 操作,因此您不能Runnable在 UI 线程上运行。因此,将您Runnable的处理程序发布到HandlerThread
  3. 从中获取结果Runnable并将其发送回 UI 线程并显示一条Toast消息。

解决方案:

  1. 创建一个HandlerThread并启动它
  2. 创建一个带有Looper处理程序:HandlerThreadrequestHandler
  3. 从主线程创建一个带有 Looper 的处理程序: responseHandler并覆盖handleMessage方法
  4. post一个Runnable任务requestHandler
  5. 内部任务,Runnable调用sendMessageresponseHandler
  6. sendMessage导致handleMessagein 的调用responseHandler
  7. 从中获取属性Message并对其进行处理,更新 UI

示例代码:

    /* Handler thread */

    HandlerThread handlerThread = new HandlerThread("HandlerThread");
    handlerThread.start();
    Handler requestHandler = new Handler(handlerThread.getLooper());

    final Handler responseHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            //txtView.setText((String) msg.obj);
            Toast.makeText(MainActivity.this,
                    "Runnable on HandlerThread is completed and got result:"+(String)msg.obj,
                    Toast.LENGTH_LONG)
                    .show();
        }
    };

    for ( int i=0; i<5; i++) {
        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                try {

                    /* Add your business logic here and construct the 
                       Messgae which should be handled in UI thread. For 
                       example sake, just sending a simple Text here*/

                    String text = "" + (++rId);
                    Message msg = new Message();

                    msg.obj = text.toString();
                    responseHandler.sendMessage(msg);
                    System.out.println(text.toString());

                } catch (Exception err) {
                    err.printStackTrace();
                }
            }
        };
        requestHandler.post(myRunnable);
    }

有用的文章:

handlerthreads-and-why-you-should-be-using-them-in-your-android-apps

android-looper-handler-handlerthread-i

于 2017-08-28T15:18:13.690 回答
5
  1. 获取 UI Thread Handler 实例并使用handler.sendMessage();
  2. 调用post()方法handler.post();
  3. runOnUiThread()
  4. view.post()
于 2016-09-07T03:17:14.270 回答
3

您可以Looper用来发送Toast消息。通过此链接了解更多详细信息。

public void showToastInThread(final Context context,final String str){
    Looper.prepare();
    MessageQueue queue = Looper.myQueue();
    queue.addIdleHandler(new IdleHandler() {
         int mReqCount = 0;

         @Override
         public boolean queueIdle() {
             if (++mReqCount == 2) {
                  Looper.myLooper().quit();
                  return false;
             } else
                  return true;
         }
    });
    Toast.makeText(context, str,Toast.LENGTH_LONG).show();      
    Looper.loop();
}

它在你的线程中被调用。上下文可能Activity.getContext()来自Activity您必须展示的祝酒词。

于 2014-05-09T05:43:03.037 回答
2

我根据 mjaggard 的回答提出了这种方法:

public static void toastAnywhere(final String text) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        public void run() {
            Toast.makeText(SuperApplication.getInstance().getApplicationContext(), text, 
                    Toast.LENGTH_LONG).show();
        }
    });
}

对我来说效果很好。

于 2017-12-06T18:42:16.303 回答
2

带有 runOnUiThread 的 Kotlin 代码

runOnUiThread(
        object : Runnable {
            override fun run() {
                Toast.makeText(applicationContext, "Calling from runOnUiThread()", Toast.LENGTH_SHORT)  
            }
        }
)
于 2020-09-17T13:36:53.640 回答
0

我也遇到了同样的问题:</p>

E/AndroidRuntime: FATAL EXCEPTION: Thread-4
              Process: com.example.languoguang.welcomeapp, PID: 4724
              java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()
                  at android.widget.Toast$TN.<init>(Toast.java:393)
                  at android.widget.Toast.<init>(Toast.java:117)
                  at android.widget.Toast.makeText(Toast.java:280)
                  at android.widget.Toast.makeText(Toast.java:270)
                  at com.example.languoguang.welcomeapp.MainActivity$1.run(MainActivity.java:51)
                  at java.lang.Thread.run(Thread.java:764)
I/Process: Sending signal. PID: 4724 SIG: 9
Application terminated.

之前:onCreate 函数

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(getBaseContext(), "Thread", Toast.LENGTH_LONG).show();
    }
});
thread.start();

之后:onCreate 函数

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(getBaseContext(), "Thread", Toast.LENGTH_LONG).show();
    }
});

有效。

于 2018-05-31T08:38:35.377 回答
0

爪哇 11:

var handler = new Handler(Looper.getMainLooper);
handler.post(() -> Toast.makeText(your_context, "Hi!", Toast.LENGTH_SHORT).show());

不过,Lambda 在 java 8 中可用。var在 Java 11 中引入。

于 2021-10-19T00:21:17.663 回答