2

processInfo()というメソッドを呼び出すときに問題が発生します。この方法では、基本的にNFCタグまたはQRコードから収集した情報を処理し、DBに書き込みます。特定の状況に応じて、processInfoメソッドは情報がDBに書き込まれる方法を処理します。processInfoメソッドはブール値を返します。これをtrueに設定すると、DB内の情報がWebサーバーに送信されます。

processInfo内に、条件Aの場合はDBに書き込み、trueを返すというロジックがあります。これにより、wewbserviceに送信されます。条件Bの場合は、Alertdialogボックスを表示します。Alertdialogボックスには、[OK]ボタンと[キャンセル]ボタンがあります。okが押された場合は、条件Aで何が起こるかを実行し、CANCELが押された場合は、ダイアログを閉じてfalseを返します。

何が起こっているのかというと、条件Bが発生した場合、ダイアログは期待どおりに表示されますが、いずれかのボタンが押される前に呼び出し元のメソッドに戻ります。少なくとも1つのボタンが押されるまでアプリをハングさせるにはどうすればよいですか?

を使ってみました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;

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

    }
}

そして、あなたのcalling()で

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

AndroidにはブロッキングUIモデルはなく、すべてが非同期です。動作を変更する必要があります。スレッド、asyncTast、またはlooperThreadでalertDialogを呼び出すことができ、DB内の情報をWebサーバーに送信する必要がある場合は、プリンシパルスレッドにメッセージを送信し(ハンドラーを使用)、handleMessage(メッセージメッセージ)で情報を送信します。

于 2013-03-27T13:52:26.497 に答える
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 に答える