android 起一个线程,多线程 - Android:在一个线程中吐司

多线程 - Android:在一个线程中吐司

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

10个解决方案

236 votes

您可以通过从您的主题中调用Activity的runOnUiThread方法来执行此操作:

activity.runOnUiThread(new Runnable() {

public void run() {

Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();

}

});

Lauri Lehtinen answered 2019-07-17T16:00:16Z

58 votes

我喜欢在我的活动中有一个名为MyActivity的方法,我可以从任何地方打电话......

public void showToast(final String toast)

{

runOnUiThread(() -> Toast.makeText(MyActivity.this, toast, Toast.LENGTH_SHORT).show());

}

然后我经常在MyActivity之内在任何这样的线程上调用它...

showToast(getString(R.string.MyMessage));

mjaggard answered 2019-07-17T16:00:58Z

26 votes

这与其他答案类似,但是针对新的可用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();

}

});

}

}

ChrisCM answered 2019-07-17T16:01:23Z

10 votes

喜欢这个或者这个,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();

}

yanchenko answered 2019-07-17T16:01:52Z

9 votes

一种适用于任何地方的方法,包括你没有Context或Service的地方,就是在主线程中获取Application并显示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.DURATION_LONG).show();

}

});

}

这种方法的优点是它适用于任何Context,包括Service和Application。

Mike Laren answered 2019-07-17T16:02:28Z

6 votes

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

下面的示例处理该场景。

你有UI线程

您必须启动IO操作,因此您无法在UI线程上运行Message。 所以发布您的handleMessage到responseHandler处理程序

从Message获取结果并将其发送回UI线程并显示handleMessage消息。

解:

创建一个HandlerThread并启动它

使用Looper创建一个处理程序,自Message:handleMessage

从主线程创建一个带有Looper的处理程序:Message并覆盖handleMessage方法

Message a handleMessage任务于responseHandler

Message内部任务,请致电handleMessage responseHandler

Message结果调用handleMessage在responseHandler。

从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和 - 为什么 - 你 - 应该待使用了他们,在你的Android的应用程序

Android的尺蠖处理程序,handlerthread-I

Ravindra babu answered 2019-07-17T16:04:27Z

5 votes

获取UI线程处理程序实例并使用view.post()

请致电view.post()方法handler.post();

view.post()

view.post()

Kerwin You answered 2019-07-17T16:06:20Z

3 votes

您可以使用Activity.getContext()发送Activity消息。 通过此链接了解更多详情。

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获得你要展示的祝酒词。

Vinoj John Hosan answered 2019-07-17T16:06:59Z

2 votes

我根据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();

}

});

}

对我来说工作得很好。

Ângelo Polotto answered 2019-07-17T16:07:34Z

0 votes

我遇到了同样的问题:

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.(Toast.java:393)

at android.widget.Toast.(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();

}

});

有效。

Languoguang answered 2019-07-17T16:08:28Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值