当服务在单独的线程上运行时,有什么方法可以显示 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/