我试图在我的ListFragment
中插入条形码扫描仪,并使用本教程:BarcodeScanner
但是,如果我单击两个按钮(QR或条形码扫描),似乎我的应用程序找不到下载的XZing
条形码扫描仪。但是已经安装好了!我没有问题:-( ...
我认为try-part
中有问题
这是我的ListFragment的代码:
private Button b1;
private Button b2;
static final String ACTION_SCAN = "com.google.xzing.client.android.SCAN";
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
b1 = (Button) getView().findViewById(R.id.button_scan_barcode_ean);
b2 = (Button) getView().findViewById(R.id.button_scan_qr_code);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanQR(v);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanBar(v);
}
});
}
public void scanBar(View v){
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
}
catch (ActivityNotFoundException anfe){
showDialog(getActivity(), "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
}
}
public void scanQR(View v){
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
catch (ActivityNotFoundException anfe){
showDialog(getActivity(), "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
}
}
private static AlertDialog showDialog(final Activity act, CharSequence title,
CharSequence message,
CharSequence buttonYes,
CharSequence buttonNo) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
downloadDialog.setTitle(title);
downloadDialog.setMessage(message);
downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
act.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
}
}
});
downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return downloadDialog.show();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent){
if (requestCode == 0){
if (resultCode == Activity.RESULT_OK){
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Toast toast = Toast.makeText(getActivity(), "Content:" + contents + "Format" + format, Toast.LENGTH_LONG);
toast.show();
final EditText editTextBarcode = (EditText) getView().findViewById(R.id.editText_barcode);
editTextBarcode.setText(contents);
}
}
}
有任何想法吗?
最佳答案
您的ACTION_SCAN字符串中有错字。您写的是“ xzing”而不是“ zxing”。
正确的字符串是
"com.google.zxing.client.android.SCAN"
关于android - fragment 中的条形码扫描仪,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30225011/