gpt4 book ai didi

android - 当服务在单独的线程上运行时如何显示 toast ?

转载 作者:行者123 更新时间:2023-11-29 21:38:09 25 4
gpt4 key购买 nike

当服务在单独的线程上运行时,有什么方法可以显示 toast 吗?我使用下面的代码。

public void onStart(Intent intent, int startid){
final String name = intent.getStringExtra("name");
Log.d(TAG,"onStart()");
new Thread(new Runnable() {


public void run() {
try
{
Toast.makeText(getApplicationContext(), "Ashish 1",Toast.LENGTH_LONG).show();

}
catch(Exception e)
{
Log.d(TAG,"Exception....."+e);
}

}
}).start();
}

最佳答案

Toast 消息只能在 uithread 上显示。但是如果你想在另一个线程上使用,你可以这样实现。

yourActivityObject.runOnUiThread(new Runnable() {
public void run()
{
Toast.makeText(yourContextObject, "some text",Toast.LENGTH_LONG).show();
}
});

关于android - 当服务在单独的线程上运行时如何显示 toast ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17829492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com