【发布时间】:2014-12-08 14:46:47
【问题描述】:
嗨,我知道这个话题有很多答案。但是我尝试了很多,它不起作用。我想在服务的线程中显示祝酒词。我怎么解决这个问题。使用 getApplicationContext() 等不起作用。
我从 Activity 启动服务(无边界)。
public class CarDataService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
startThreadUpdatingDatabase();
Toast.makeText(this, message, Toast.LENGTH_LONG).show(); //it works
}
private void startThreadUpdatingDatabase(){
Log.d("Database", "startThreadUpdatingDatabase(was called)");
new Thread(new Runnable() {
public void run(){
..
// here i want to use a toast!!!
}
}).start();
}
}
谢谢!
【问题讨论】:
-
您无法从非 ui 线程显示 Toast。不要对明显正确的答案投反对票。
-
您只是对答案投了反对票,因为您没有从中受益吗?我想知道谁以这种方式对所有答案投了反对票?
标签: android multithreading service toast