2

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

我在 processInfo 中有一些逻辑,如果条件 A 则写入 DB 并返回 true。这又将其发送到 webbservice。如果条件 B 则显示警报对话框。警报对话框有一个确定和取消按钮。如果按下 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;
}
4

3 回答 3

2

您可以使用回调方法。比方说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();


}
}

另一种方法是使变量成功作为全局变量。

boolean success;

进程信息()

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();

    }
}

而且,在你的 call()

private void callingMethod(){
    if(success){
        //do your stuff
    } else {
        //do your stuff
    }
}
于 2013-03-27T13:23:39.317 回答
1

@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();
        }
    }
}
于 2013-03-27T14:57:42.173 回答
1

Android 中没有阻塞的 UI 模型,一切都是异步的。你必须改变行为。您可以在线程或 asyncTast 或 looperThread 中调用 alertDialog ,如果必须将 DB 中的信息发送到 Web 服务器,则将消息发送到主线程(使用处理程序)并在 handleMessage(Message msg) 中发送信息。

于 2013-03-27T13:52:26.497 回答