我想在点击按钮时扫描二维码,问题是当我在我的设备上运行代码时,Activity Result Intent变量总是返回0。
如何知道条形码读取器是否正常工作?我目前在设备屏幕上看到了黄色的圆点。
下面是我的代码:
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);
}谢谢
发布于 2012-06-28 19:08:49
你在代码中有错误
你应该有
@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));
}
}
}您不会像onCreate或onStart那样覆盖onActivityResult
相反,您编写的onActivityResult像普通方法一样,这是最常见的错误。
另外,如果你能提到integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES);或integrator.initiateScan(IntentIntegrator.PRODUCT_CODE_TYPES);,那就更好了。
https://stackoverflow.com/questions/11225298
复制相似问题