我有一个BroadcastReceiver。 在那里我创建了一个新线程。 如何在该线程中展示吐司?
由于
答案 0 :(得分:2)
使用以下代码从非UI线程执行UI操作
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
// your stuff to update the UI
}
});
答案 1 :(得分:1)
尝试此代码
public void start_insert() {
pDialog.show();
new Thread() {
@Override
public void run() {
int what = 0;
try {
// Do Something in Background
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
what = 1;
e.printStackTrace();
}
handler22.sendMessage(handler22.obtainMessage(what));
}
}.start();
}
private Handler handler22 = new Handler() {
@Override
public void handleMessage(Message msg) {
pDialog.dismiss();
Toast.makeText(getApplicationContext(), "SuccessFull",
10).show();
}
};
答案 2 :(得分:0)
Activity_Name.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// your stuff to update the UI
}
});
使用此代码更新UI线程或执行任何与UI相关的操作。