当扫描QR码时,ZXing没有调用onActivityResult

时间:2017-10-20 07:44:05

标签: android qr-code zxing

我使用ZXing Android Embedded库读取QRCode,成功扫描QRCode后未调用onActivityResult,请提供帮助。

public class ScanActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_money);

        Button scanButton = (Button) findViewById(R.id.scan_button);
        scanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentIntegrator integrator = new IntentIntegrator(ScanActivity.this);
                integrator.initiateScan();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        System.out.println("*************** Why is this method not called?");
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Result is: ", Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

此代码的问题在于我在清单文件中android:noHistory="true"上使用了ScanActivity

<activity
    android:name=".ScanActivity" android:noHistory="true" />

删除android:noHistory="true"解决了问题

<activity
    android:name=".ScanActivity" />

答案 1 :(得分:0)

IntentIntegrator integrator = new IntentIntegrator(ScanActivity.this);
            integrator.initiateScan();

integrator.initiateScan();不会调用或不反映startActivityForResult操作。

您必须按顺序调用startActivityForResult,以便在onActivityResult中进行回调