如何在第一个活动完成后才启动线程?

时间:2012-07-19 13:52:57

标签: android barcode zxing

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

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();如何在用户成功扫描代码后才能控制它应该执行?

提前致谢。

2 个答案:

答案 0 :(得分: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将工作线程中的消息发送到主线程。我不认为它会让你从后台线程调用dismiss。这只是一种预感,没有经过测试。

答案 1 :(得分:0)

将其放在OnActivityResult方法中,覆盖itp>

   @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;
        }
      }
    }