我正在尝试创建条形码阅读器。我使用 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.");
}
}
}