如果设备未安装条形码阅读器应用程序,则条形码扫描不起作用

时间:2014-03-31 12:46:47

标签: android zxing barcode-scanner android-vision

我正在尝试创建条形码阅读器。我使用Zxing库,我有一个有自己的条形码阅读器的设备的问题。我的应用程序运行正常。但在没有条形码阅读器应用程序的设备上,我的应用程序无法运行。

这是我的代码,如何检查设备是否有条形码阅读器?

public class MainActivity extends Activity {
TextView tvStatus;
TextView tvResult;
private static String BarCodeResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvStatus = (TextView) findViewById(R.id.tvStatus);
    tvResult = (TextView) findViewById(R.id.tvResult);
    BarCodeResult = tvResult.getText().toString();

    Button scanBtn = (Button) findViewById(R.id.btnScan);

    scanBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            try {

                Intent intent = new Intent(
                        "com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
                startActivityForResult(intent, 0);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "ERROR:" + e, 1)
                        .show();

            }

        }
    });

}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
            tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
            connectWithHttpGet(intent.getStringExtra("SCAN_RESULT"));
            BarCodeResult=intent.getStringExtra("SCAN_RESULT");

            Intent in = new Intent(getApplicationContext(), Result.class);
            in.putExtra("KEY_BarCodeResult", intent.getStringExtra("SCAN_RESULT"));

            startActivity(in);
        } else if (resultCode == RESULT_CANCELED) {
            tvStatus.setText("Press a button to start a scan.");
            tvResult.setText("Scan cancelled.");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您没有处理未安装应用的情况,是的,并且没有可用于处理Intent的内容。

只需使用应用中项目的IntentIntegrator类,即可处理此问题:

https://github.com/zxing/zxing/blob/4fb569cce8de8e7724b39cf3f62350441a559771/android-integration/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java

或者您可以看到它的作用,并使用ActivityNotFoundException执行类似的操作。

答案 1 :(得分:-1)

一个简单的方法。如果没有扫描仪,则重定向到Playstore进行安装

try {
        intentscan = new Intent(
                "com.google.zxing.client.android.SCAN");
        startActivityForResult(intentscan, 0);
    } catch (ActivityNotFoundException ex) {
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("You must install\nBarcode Scanner\nof Zxing")
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,int which) {
                                Intent i = new Intent(
                                        Intent.ACTION_VIEW,
                                        Uri.parse("https://play.google.com/store/apps/details?id=com.google.zxing.client.android"));
                                startActivity(i);
                                finish();
                            }
                        }).show();
    }