Not receiving an Intent back from Zxing barcode sc

2019-08-07 06:07发布

I know some others have had this problem but I've followed the solutions and it still isn't working for me.

I've created a new application, it has 1 activity which has 1 button (scan button) and 2 textviews (which are just going to output the formatname and contents that Zxing returns at the moment).

I have followed the ScanningViaIntent tutorial but it doesn't seem to be hitting onActivityResult

Below is my code:

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

    final IntentIntegrator integrator = new IntentIntegrator(this);
    Button btnScan = (Button) findViewById(R.id.button1);
    btnScan.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            integrator.initiateScan();
        }
    });
}

public void OnActivityResult(int requestCode, int resultCode, Intent intent)
{
    Log.i("result", "hit line");
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

    TextView tv1 = (TextView) findViewById(R.id.textView1);
    TextView tv2 = (TextView) findViewById(R.id.textView2);

    if(scanResult != null)
    {
        System.out.println("format: " + scanResult.getFormatName());
        System.out.println("contents: " + scanResult.getContents());



        tv1.setText(scanResult.getFormatName());
        tv2.setText(scanResult.getContents());
    }
    else
    {
        tv1.setText("ERROR");
    }
}

TextView1 never says "Error" so it doesn't seem that scanResult is null and my Log.i() line is never hit so I'm thinking that onActivityResult isn't even being hit.

Could it be to do with making IntentIntegrator final for the OnClick() method? When I created IntentIntegrator inside OnClick(), I used getParent() to pass the Activity to the constructor but this force closed my app with a NullReferenceException inside IntentItegrator.

Am I using the library correctly?

Thanks for your time,

Poncho

3条回答
狗以群分
2楼-- · 2019-08-07 06:50

You are not actually overriding the method onActivityResult() because you have implemented OnActivityResult(). Your method is not being called as a result. Everything else looks about right.

This is the kind of thing you catch if you use @Override annotations -- good habit, since it would have caught this.

查看更多
▲ chillily
3楼-- · 2019-08-07 07:02

Where are you calling startActivityForResult(..)? You may want to use something like this :

Intent intentScan = new Intent(BS_PACKAGE + ".SCAN");
intentScan.addCategory(Intent.CATEGORY_DEFAULT);

// check which types of codes to scan for
if (desiredBarcodeFormats != null) {
  // set the desired barcode types
  StringBuilder joinedByComma = new StringBuilder();
  for (String format : desiredBarcodeFormats) {
    if (joinedByComma.length() > 0) {
      joinedByComma.append(',');
    }
    joinedByComma.append(format);
  }
  intentScan.putExtra("SCAN_FORMATS", joinedByComma.toString());
}

String targetAppPackage = findTargetAppPackage(intentScan);
if (targetAppPackage == null) {
  return showDownloadDialog();
}
intentScan.setPackage(targetAppPackage);
intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
attachMoreExtras(intentScan);
startActivityForResult(intentScan, REQUEST_CODE);

findTargetAppPackage :

 private String findTargetAppPackage(Intent intent) {
    PackageManager pm = activity.getPackageManager();
    List<ResolveInfo> availableApps = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    if (availableApps != null) {
      for (ResolveInfo availableApp : availableApps) {
        String packageName = availableApp.activityInfo.packageName;
        if (targetApplications.contains(packageName)) {
          return packageName;
        }
      }
    }
    return null;
  }

To see a more complete example go here.

查看更多
forever°为你锁心
4楼-- · 2019-08-07 07:03

You need to get the latest classes from the repository https://github.com/zxing/zxing/tree/master/android-integration/src/main/java/com/google/zxing/integration/android

See the javadoc of the class to see how to use it. First add code to invoke the Intent:

IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();

Second, add this to your Activity to handle the result:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode,      intent);
    if (scanResult != null) {
        // handle scan result
    }
    // else continue with any other code you need in the method
}

More info here https://github.com/zxing/zxing/wiki/Scanning-Via-Intent

查看更多
登录 后发表回答