0

I want to Scan QR codes on button Click, problem is when I run code on my device Activity Result Intent variable always returns 0.

How do I know if the barcode reader work? I currently see yellow dots on the device's screen.

Here is my code:

private OnClickListener scanner = new OnClickListener() {
    public void onClick(View v) {
        IntentIntegrator.initiateScan(BarCodeScannerActivity.this);
    }
}; 

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
     IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
     if (scanResult != null) {
           // handle scan result
           String s = "http://www.google.com/search?q=";
           s += scanResult.getContents();

           Intent myIntent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(s));
           startActivity(myIntent1);
     }

Thanks

shkschneider
  • 17,833
  • 13
  • 59
  • 112
sara
  • 131
  • 4
  • 14

1 Answers1

2

You have error in code

You should have

@Override
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (result != null) {
      String contents = result.getContents();
      if (contents != null) {
        showDialog(R.string.result_succeeded, result.toString());
      } else {
        showDialog(R.string.result_failed, getString(R.string.result_failed_why));
      }
    }
  }

You are not overriding the onActivityResult like onCreate or onStart

Rather you are writing onActivityResult like normal method which is most common mistake.

Also if you can mention integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES); or integrator.initiateScan(IntentIntegrator.PRODUCT_CODE_TYPES); it would be great.

Vipul
  • 27,808
  • 7
  • 60
  • 75
  • integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES); is giving error it accept only Activity name – sara Jun 28 '12 at 11:40
  • I was going mad over this, and then came your post about the `@Overrride`, great help, and the `initiateScan(IntentIntegrator.PRODUCT_CODE_TYPES);` works fine, but remember that it does not return anything when you provide a parameter – Hoku Nov 19 '15 at 23:57