我正在使用zxing android整理QR码。
要在onCreateView的HomeFragment中初始化扫描对象:
//intializing scan object
//qrScan = new IntentIntegrator(this.getActivity()); // this is for activity
qrScan = IntentIntegrator.forSupportFragment(this); // this is for fragment
要获取扫描仪结果:
// Get the results:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
但是,我得到了:
错误:HomeFragment中的onActivityResult(int,int,Intent)无法 在Fragment中尝试覆盖onActivityResult(int,int,Intent)尝试 分配较弱的访问权限;是公开的
将其从“受保护”更改为公共后,在HomeFragment中未调用onActivityResult。
答案 0 :(得分:0)
尝试
IntentIntegrator scanIntegrator = IntentIntegrator.forSupportFragment(YourFragmentClass.this);
scanIntegrator.setPrompt("Scan");
scanIntegrator.setBeepEnabled(true);
scanIntegrator.setBarcodeImageEnabled(true);
scanIntegrator.initiateScan();
然后在onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
if (scanningResult.getContents() != null) {
scanContent = scanningResult.getContents().toString();
scanFormat = scanningResult.getFormatName().toString();
}
Toast.makeText(getActivity(), scanContent + " type:" + scanFormat, Toast.LENGTH_SHORT).show();
textView.setText(scanContent + " type:" + scanFormat);
} else {
Toast.makeText(getActivity(), "Nothing scanned", Toast.LENGTH_SHORT).show();
}
}
您可以检查this回购