从一个线程发布吐司消息 [英] Posting Toast message from a Thread

查看:124
本文介绍了从一个线程发布吐司消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序将启动一个线程来查询网站的一些数据。我想没事的时候被发现,但我的应用程序总是崩溃,显示敬酒消息。

My application launches a thread to query the web for some data. I want to display a Toast message when nothing is found, but my application always crashes.

我已经使用的应用程序上下文的线程中尝试过,像这样:

I've tried using the application Context from within the thread, like so:

Toast.makeText(getApplicationContext(), "testttt", Toast.LENGTH_LONG).show();

我也试着创造一个Runnable与吐司通话,然后从线程中调用runOnUiThread(可运行)(在此可运行的吐司调用使用活动作为第一个参数)。

I've also tried creating a Runnable with the Toast call and calling runOnUiThread(runnable) from the Thread (the Toast call in this runnable uses the Activity as the first parameter).

有没有人对如何做到这一点任何想法?

Does anyone have any ideas on how to accomplish this?

推荐答案

尝试发布里面Handler对象。

Try to post inside to a Handler object.

final Handler mHandler = new Handler();
final Runnable mUpdateResults = new Runnable() {
    public void run() {
        Toast(this, message, duration).show();
    }

new Thread() {
    public void run() {
        mHandler.post(mUpdateResults);
    }
}.start();

这篇关于从一个线程发布吐司消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆