返回AlertDialog对话框结果之前的方法



我有一个问题,我有一个方法,我调用processInfo()。该方法主要处理从NFC标签或QR码收集的信息,并将其写入数据库。根据某些情况,processInfo方法处理如何将信息写入数据库。processInfo方法返回一个布尔值,如果设置为true,则数据库中的信息将被发送到web服务器。

我在processInfo中有一些逻辑,说如果条件A,那么写到DB并返回true。然后将其发送到webservice。如果条件B,则显示一个警告对话框。Alertdialog对话框中有一个OK和CANCEL按钮。如果按下ok键,则执行条件A中发生的操作,如果按下CANCEL键,则取消对话框并返回false。

发生的情况是,如果条件B发生,则对话框按预期显示,但在按下任何按钮之前它返回到调用方法。我如何使应用程序挂起,直到至少一个按钮被按下?

我试过使用while(! alertDialog.isShowing == true) -> return boolean。但在alertDialog.show()后退出并返回到调用方法。

success = false;
if(condition A) {
    // write to DB and return to caller
    return success = true;
} else {
    success = false;
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(NfcscannerActivity.this);
    // set title
    alertDialogBuilder.setTitle("Please logout after ");
     // set dialog message
    alertDialogBuilder.setMessage("Click Ok to return to the menu")
    .setCancelable(false)
    .setPositiveButton("Ok",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            success = true;
        // write to DB                                          
        }
    })
    .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            // if this button is clicked, just close
            // the dialog box and do nothing
            success = false;
            dialog.cancel();
        }
    });
    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();
    // show it
    alertDialog.show();
}
return success;
}

你可以使用回调方法。比如onResultObtained(boolean result):

boolean onResultObtained(boolean result) {
    if(result) {
         //write to DB and return to caller
         return true; 
    } else {
       return false; 
    }
}
实际代码

if(condition A){
   onResultChanged(true);
}else{
   AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                NfcscannerActivity.this);
                            // set title
                            alertDialogBuilder.setTitle("Please logout after ");
                            // set dialog message
                            alertDialogBuilder
                                .setMessage("Click Ok to return to the menu")
                                .setCancelable(false)
                                .setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,int id) {
                               onResultChanged(true);

                                    }
                                  })
                                .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,int id) {
                                        // if this button is clicked, just close
                                        // the dialog box and do nothing
                                        dialog.dismiss();
                                        onResultChanged(false);   
                                    }
                                });
                                alertDialogBuilder.show();

}
}

另一种方法是将变量success设为全局。

boolean success;

processInfo ()

private void processInfo() {
    if (condition A) {
        success = true;
        //Save data to DB
    } else {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                MainActivity.this);
        // set title
        alertDialogBuilder.setTitle("Please logout after ");
        // set dialog message
        alertDialogBuilder
                .setMessage("Click Ok to return to the menu")
                .setCancelable(false)
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                success = true;
                                //Save data to DB
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.dismiss();
                                success = false;
                            }
                        });
        alertDialogBuilder.show();
    }
}

And, in your calling()

private void callingMethod(){
    if(success){
        //do your stuff
    } else {
        //do your stuff
    }
}

@turtleboy这是一个简单的例子

公共类TestActivity扩展Activity {

    static final String TAG="TestActivity";
    static final int MSG_SEND_INFO_TO_SERVER = 1;
    static final int MSG_INFO_NOT_SEND_TO_SERVER = 2;
    boolean condition=false;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i(TAG, "0000000000000000");
        your_methode();
    }
    Handler communication_handler =new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_SEND_INFO_TO_SERVER: 
                //send info to the web server
                Log.i(TAG, "11111111111111111");
                Toast.makeText(getApplicationContext(), "send msg to the server", Toast.LENGTH_SHORT).show();
                break;
            case MSG_INFO_NOT_SEND_TO_SERVER: 
                //send info to the web server
                Log.i(TAG, "222222222222222222");
                Toast.makeText(getApplicationContext(), "no msg to send to the server", Toast.LENGTH_SHORT).show();
                break;  
            default:
                break;
            }
        }
    };
    public void your_methode()
    {
        if(condition) {
            // write to DB
            communication_handler.sendMessage(Message.obtain(null,MSG_INFO_NOT_SEND_TO_SERVER,0));
            return ;
        } else {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            // set title
            alertDialogBuilder.setTitle("Please logout after ");
            // set dialog message
            alertDialogBuilder.setMessage("Click Ok to return to the menu").setCancelable(false);
            alertDialogBuilder.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // write to DB
                    // send msg 
                    communication_handler.sendMessage(Message.obtain(null,MSG_SEND_INFO_TO_SERVER,0));                                       
                }
            });
            alertDialogBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                    communication_handler.sendMessage(Message.obtain(null,MSG_INFO_NOT_SEND_TO_SERVER,0));
                }
            });
            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();
            // show it
            alertDialog.show();
        }
    }
}

Android中没有阻塞UI模型,一切都是异步的。你必须改变这种行为。您可以在线程或asyncast或looperThread中调用alertDialog如果数据库中的信息必须发送到web服务器,则发送一个消息到主线程(使用Handler),并在handleMessage(Message msg)中发送信息。

最新更新