我使用this answer创建了一个独立的Android库项目,其中包含ZXing源代码(ZXing v2.1)。它编译得很好,如果我运行CaptureActivity,我可以按预期读取QR码。
我有另一个Android项目,我想从中吸引这个库。我已正确设置了库关系。
我遇到的问题是,如何通过IntentIntegrator(提到here)启动ZXing扫描仪的本地副本。
我尝试修改IntentIntegrator.initiateScan()方法以使用我的CaptureActivity本地副本,并正确加载QR扫描程序。 但是,一旦扫描了QR码,QR信息就会显示在屏幕上,而不是将结果发送回onActivityResult中的我的通话活动。
如何将QR扫描结果发送到我的通话活动的onActivityResult?
供参考,以下是我将IntentIntegrator.initiateScan()方法更改为:
public AlertDialog initiateScan(Activity act, Collection<String> desiredBarcodeFormats) {
//Hardcoding name of activity to call --> is this where I've gone wrong?
Intent intentScan = new Intent(act, CaptureActivity.class);
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());
}
//Commented this out because it didn't seem to find my class...
// 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);
return null;
}
我正在开始这样的扫描:
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.initiateScan(getActivity());
我觉得我在这里错过了一些简单的东西,任何正确方向的推动都会很棒。
解
这是最终起作用的。我仍然以同样的方式调用它:
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.initiateScan(getActivity());
但现在,initiateScan方法看起来像这样:
public AlertDialog initiateScan(Activity act, Collection<String> desiredBarcodeFormats)
{
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());
}
//THIS WAS THE KEY
setSingleTargetApplication(act.getPackageName());
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);
act.startActivityForResult(intentScan, REQUEST_CODE);
return null;
}
重要的是确保BS_PACKAGE指向CaptureActivity包,您调用“act.startActivityForResult ...”而不仅仅是“startActivityForResult ...”,并使用应用程序的包名称调用setSingleTargetApplication打电话给扫描仪。
答案 0 :(得分:1)
尝试更改第startActivityForResult(intentScan, REQUEST_CODE);
行
到act.startActivityForResult(intentScan, REQUEST_CODE);
您无需对包含findTargetAppPackage
的代码发表评论,只需通过调用setSingleTargetApplication()
设置目标应用程序的包(如果您是使用此库的唯一应用程序)