Issue in using Zxing via intent if google goggles is installed

6.6k Views Asked by At

I am using following code to invoke Barcode scanner apps from Zing

public Button.OnClickListener mScan = new Button.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    }
};

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
        }
    }
}

The problem is if Barcode scanner app is not installed and user has any other scanning app like google goggles i dont get desired result back.This breaks my application.

Is there any way in which i can prevent this ??

2

There are 2 best solutions below

6
On BEST ANSWER

Yes. call Intent.setPackage() with value "com.google.zxing.client.android". This will force it to only accept a response from Barcode Scanner.

Note however that this will make it impossible for other apps to respond, like Barcode Scanner+.

1
On

its better to integrate the bar code scanner to your app. Zxing is an open source code you can download it from here. And for the Integration please refer this: http://www.falatic.com/index.php/12/building-zxing-for-android-part-3-using-eclipse. I think this will solve your issue.