片段中的条码扫描器

时间:2015-05-13 20:55:31

标签: android android-fragments barcode-scanner

我正在尝试在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);
            }
        }
    }

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的ACTION_SCAN字符串中存在拼写错误。你写了“xzing”而不是“zxing”。

正确的字符串是

"com.google.zxing.client.android.SCAN"