我有一个广播接收器。在那里我创建了一个新线程。如何在该线程中显示敬酒?
谢谢
使用以下代码从非 UI 线程执行 UI 操作
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
// your stuff to update the UI
}
});
试试这个代码
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();
}
};
Activity_Name.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// your stuff to update the UI
}
});
使用此代码更新 UI 线程或执行任何 UI 相关操作。