没有收到回意向从斑马线条形码扫描器(Not receiving an Intent back fro

2019-10-17 10:30发布

我知道有些人有过这样的问题,但我已经试过了解决方案,它仍然不是为我工作。

我创建了一个新的应用程序,它具有1个按钮(扫描按钮)和2个textviews(这只是要输出FORMATNAME和内容是斑马线返回的那一刻)1个活动。

我也跟着ScanningViaIntent教程,但它似乎并没有被击中onActivityResult

下面是我的代码:

@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从来不说“错误”,因此它似乎并不scanResult为空,我的Log.i()的行不会打,所以我认为onActivityResult甚至没有被击中。

难道是用做IntentIntegrator做final的的OnClick()方法? 当我创建IntentIntegrator内的OnClick(),我用的getParent()传递活动的构造函数,但这种强制关闭我的应用程序内IntentItegrator一个NullReferenceException。

我是否正确地使用图书馆?

谢谢你的时间,

雨披

Answer 1:

你实际上并没有覆盖方法onActivityResult()因为你已经实现OnActivityResult() 你的方法不会被调用的结果。 一切看起来是正确的。

这是你抓住,如果你使用的那种东西@Override注解-好习惯,因为它本来是可以赶上这一点。



Answer 2:

你在哪里调用startActivityForResult(..) 你可能想使用这样的:

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

要看到更完整的例子去这里 。



Answer 3:

你需要从库中的最新类https://github.com/zxing/zxing/tree/master/android-integration/src/main/java/com/google/zxing/integration/android

见类的Javadoc,以了解如何使用它。 首先添加代码来调用意图:

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

其次,这种添加到您的活动来处理结果:

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
}

这里更多信息https://github.com/zxing/zxing/wiki/Scanning-Via-Intent



文章来源: Not receiving an Intent back from Zxing barcode scanner