[英]How to display alert dialog in running thread?
我想顯示一個警報對話框,具體取決於屬性,並且當用戶單擊“確定”按鈕時,再次調用該函數以獲取運行過程中的更新值。
我有以下代碼:
importingProgress = ProgressDialog.show(context, getString(R.string.progressNewsListTitle),
getString(R.string.progressProjectListMessage), true);
new Thread(new Runnable() {
public void run() {
try {
app.SetOtherTaskRunning(true);
Ib_clients client = db.Ib_clients_GetById(app.GetCustomerId());
try {
LogManager.WriteToFile("---------------- Getting News from Webservice :- " + DateFormat.getDateTimeInstance().format(new Date()) + "----------------");
CommonFuctions.CreateXml(context, h, client, db, app.GetBookMonth(), app.GetBookQuater(), app.GetBookYear(), Constants.News, app.GetWebServiceLastSyncDate(Constants.ServiceType.NEWS.toString()), Constants.ServiceType.NEWS, null, null, null, null, null);
Return reponse = null;
do {
reponse = CommonFuctions.SendingRequest(context, handler, db);
if (reponse.type.compareTo("warning") == 0) {
h.post(new Runnable() {
public void run() {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle(context.getString(R.string.information));
alert.setMessage("dsgdgd");
alert.setPositiveButton(context.getString(R.string.logoutDialogOk), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
});
}
} while (reponse.type.compareTo("warning") == 0);
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
//Log.d(Constants.TAG, e.getMessage());
e.printStackTrace();
}
if (importingProgress != null) {
importingProgress.dismiss();
importingProgress = null;
}
}
}).start();
如果響應類型為warning
,則向用戶顯示一條消息,如果用戶單擊“ OK
按鈕,則再次調用CommonFuctions.SendingRequest(context, handler, db)
以獲取更新的值。 在得到warning
.response類型之前,我們需要向用戶顯示一個警告對話框,然后再次調用CommonFuctions.SendingRequest(context, handler, db)
。
返回的類:
public class Return {
public String type;
public String msg;
public boolean isSuccess;
public int client_id; // for getting clientid from server
public int booking_id; // for getting bookingid form server
}
您將必須使用處理程序來顯示AlertDialog,因為UI只能由主線程處理。
另一種方法是使用asyncTask進行多重處理,然后使用asyncTask的onPostExcecute()顯示AlertDialog
請隨時提出任何進一步的疑問。
嘗試在runonUIthread中如下運行對話框:
runOnUiThread(new Runnable() { @Override public void run() { AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setTitle(context.getString(R.string.information)); alert.setMessage("dsgdgd"); alert.setPositiveButton(context.getString(R.string.logoutDialogOk), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }); alert.show(); } });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.