0

在我的应用程序中,我有扫描二维码的扫描按钮。代码是这样的:

btnScan.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                            startActivityForResult(intent, 1);

                            ClearForm();
                            //if (!CheckCHFID())return;

                            pd = ProgressDialog.show(EnquireActivity.this, "", getResources().getString(R.string.GetingInsuuree));
                            new Thread(){
                                public void run(){
                                    getInsureeInfo();

                                    pd.dismiss();
                                }
                            }.start();

                        }
                    });

现在的问题是在我扫描代码之前它开始查找 getInsureeInfo(); 的信息。我如何控制它只在用户成功扫描代码后才执行?

提前致谢。

4

2 回答 2

2

您需要将扫描后想要发生的部分移动到 onActivityResult() 方法。

/*Here is where we come back after the Barcode Scanner is done*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            // contents contains whatever the code was
            String contents = intent.getStringExtra("SCAN_RESULT");

            // Format contains the type of code i.e. UPC, EAN, QRCode etc...
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

            // Handle successful scan. 

        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel. If the user presses 'back' before a code is scanned.

        }
    }
}

此外,我认为当隐藏进度对话框时,您将不得不使用 Handler 将消息从工作线程发送到主线程。我认为它不会让你从后台线程调用它。不过,这只是一种预感,未经测试。

于 2012-07-19T13:57:15.357 回答
0

把它放在OnActivityResult方法中,覆盖它。

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode) {
        case 1: 
        if (resultCode == RESULT_OK) {

            //put your stuff here....

            break;
        }
      }
    }
于 2012-07-19T13:55:03.527 回答