从处理程序调用webservice后,Android alertdialog会多次显示

时间:2013-07-22 08:43:51

标签: android

我用简单的方案调用webservice它工作正常但在处理程序中获得webservice响应后它多次显示alertdialog。

以下是网络服务电话的代码:

public void callUpdateUserInfo_WS() {

        if (NetworkAvailablity.checkNetworkStatus(ProfileScreen.this)) {
            // PREPARE URL
            Constant.methodURL = "http://admin.tvdevphp.com/goalmachine/update_profile.php";

            // PREPARE REQUEST PARAMETER
            ArrayList<NameValuePair> requestParaList = new ArrayList<NameValuePair>();

            requestParaList.add(new BasicNameValuePair("user_id", Constant.USER_ID));
            requestParaList.add(new BasicNameValuePair("first_name", name));
            requestParaList.add(new BasicNameValuePair("age",age));
            requestParaList.add(new BasicNameValuePair("age_visible", "Yes"));
            requestParaList.add(new BasicNameValuePair("parental_status", partentalStatus));
            requestParaList.add(new BasicNameValuePair("parental_status_visible", "Yes"));
            requestParaList.add(new BasicNameValuePair("marital_status", maritalStatus));
            requestParaList.add(new BasicNameValuePair("marital_status_visible", "No"));
            requestParaList.add(new BasicNameValuePair("short_bio", bio));
            requestParaList.add(new BasicNameValuePair("short_bio_visible", "Yes"));
            requestParaList.add(new BasicNameValuePair("country", country));

            requestParaList.add(new BasicNameValuePair("country_visible", "Yes"));
            requestParaList.add(new BasicNameValuePair("zipcode", zipCode));
            requestParaList.add(new BasicNameValuePair("zipcode_visible", "Yes"));
            requestParaList.add(new BasicNameValuePair("avatar", ""));

            requestParaList.add(new BasicNameValuePair("latest_status_update", recentUpdate));
            requestParaList.add(new BasicNameValuePair("latest_status_update_visible", "Yes"));
            requestParaList.add(new BasicNameValuePair("pain_scale", painLevel));
            requestParaList.add(new BasicNameValuePair("pain_scale_visible", "No"));

            // CALL WEBSERVICE
            WebServiceCommunicator.getInstance().registerForServerResponse(
                    ProfileScreen.this);
            WebServiceCommunicator.getInstance().callGetAppWebService(
                    Constant.showDialog, this.getParent(),
                    Constant.methodURL, this.getParent(), Constant.PID_UPDATE_USER_INFO,
                    false, requestParaList);
        } else {
            Constant.showAlertDialog(Constant.errorTitle,
                    Constant.MSG_CHECK_INTERNET_SETTING, this.getParent(),
                    false);
        }
    }

以下是处理程序的代码:

private Handler _handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.arg1) {
            case Constant.PID_GET_USER_INFO:
                if (parseResponse(msg.obj.toString(),Constant.PID_GET_USER_INFO) == true) {
                } else {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Constant.showAlertDialog(
                                    Constant.DIALOG_TITLE_ERROR,
                                    "Profile not available.",
                                    ProfileScreen.this, false);
                        }
                    });
                }
                break;

            case Constant.PID_UPDATE_USER_INFO:
                if (parseResponse(msg.obj.toString(),Constant.PID_UPDATE_USER_INFO) == true) {
                    /*Constant.showAlertDialog(
                            Constant.DIALOG_TITLE_ERROR,
                            "Profile updated successfully.",
                            ProfileScreen.this, false);*/
                } else {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Constant.showAlertDialog(
                                    Constant.DIALOG_TITLE_ERROR,
                                    "Profile not updated.",
                                    ProfileScreen.this, false);
                        }
                    });
                }
                break;

            default:
                break;
            }
        }
    };

在上面的代码中,警告对话框个人资料未更新。多次显示。

提醒对话框的代码:

public static void showAlertDialog(final String title, String message,
            final Context context, final boolean redirectToPreviousScreen) {
        AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
        alertbox.setMessage(message);
        //alertbox.setTitle(title);
        alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {

            }
        });
        try{
            alertbox.show();    
        }catch (Exception b) {
            b.printStackTrace();
        }

    }

1 个答案:

答案 0 :(得分:2)

根据您的更新,我可以向您建议以下更改:

// Next two lines will go at the top of the class Constant
static AlertDialog.Builder alertbox;
static AlertDialog alertDialog;

....
.... // Other code
....

public static void showAlertDialog(final String title, String message,
        final Context context, final boolean redirectToPreviousScreen) {
    if (alertDialog != null && alertDialog.isShowing()) {
        // A dialog is already open, wait for it to be dismissed, do nothing
    } else {
        alertbox = new AlertDialog.Builder(context);
        alertbox.setMessage(message);
        //alertbox.setTitle(title);
        alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                alertDialog.dismiss()
            }
        });

        alertDialog = alertbox.create();
        alertDialog.show();
    }
}

此代码检查屏幕上是否已显示AlertDialog。如果是,它什么都不做。否则,它会使用它收到的参数创建一个新的AlertDialog。