如何在运行线程中显示警告对话框?

时间:2013-01-24 09:04:16

标签: java android android-layout android-widget

我希望根据属性显示警告对话框,当用户点击“确定”按钮时,再次调用该函数以在正在运行的过程中获取更新值。

我有以下代码:

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的响应类型之前,我们需要向用户显示警告对话框并再次致电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
 }

2 个答案:

答案 0 :(得分:0)

您必须使用handler来显示AlertDialog,因为UI只能由主线程处理。

另一种方法是使用asyncTask进行多处理,然后使用asyncTask的onPostExcecute()来显示AlertDialog

请随时再提出疑问。

答案 1 :(得分:0)

尝试在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();
                        }
                 });