我正在尝试使用以下代码通过 zxing 从 QR 码中提取数据(基本上是从教程中复制粘贴,更改名称以保护无辜者):
//Check for Barcode scanner, if not found put up an alert that allows user to install it.
PackageManager pm = getPackageManager();
try {
ApplicationInfo appInfo = pm.getApplicationInfo("com.google.zxing.client.android", 0);
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");//for Qr code, its "QR_CODE_MODE" instead of "PRODUCT_MODE"
intent.putExtra("SAVE_HISTORY", false);//this stops saving ur barcode in barcode scanner app's history
startActivityForResult(intent, 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
new AlertDialog.Builder(BitcoinAddressUtilityActivity.this)
.setTitle("WARNING:")
.setMessage("You don't have Barcode Scanner installed. Please install it.")
.setCancelable(false)
.setNeutralButton("Install it now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
})
.show();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
EditText passphrase = (EditText) findViewById(R.id.txtPassphrase);
passphrase.setText(contents);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
});
它执行得很好,没有错误,但 onActivityResults 永远不会触发,我什至收到警告:
The method onActivityResult(int, int, Intent) from the type new View.OnClickListener(){} is never used locally
我完全承认我是一个活动/意图菜鸟,所以如果有人想以教程的形式回答,我很乐意接受——我不只是想要修复代码,我想知道为什么会这样一开始不工作。