4

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 ??

Pranay Airan
  • 1,855
  • 6
  • 28
  • 52

2 Answers2

6

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+.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • 3
    You could iterate through the result of [`PackageManager.queryIntentActivities`](http://developer.android.com/reference/android/content/pm/PackageManager.html#queryIntentActivities(android.content.Intent,%20int)) and check a whitelist of apps, and then use any valid package name in `Intent.setPackage()` – David Snabel-Caunt Nov 08 '11 at 13:23
  • thanks this worked what should ideally be a whitelist?? Barcode scanner, Barcode Scanner+ and what else?? – Pranay Airan Nov 08 '11 at 17:59
  • That's all. com.google.zxing.client.android, com.srowen.bs.android and com.srowen.bs.android.simple. I will rewrite the integration code accordingly. – Sean Owen Nov 08 '11 at 18:08
  • Just for those interested, if you do want to force Google Goggles the line is 'intent.setPackage("com.google.android.apps.unveil")`. I'd still encourage the use of the zxing version ..its a great project. – Tim Nov 28 '11 at 12:36
  • 1
    The OP said that Goggles doesn't reply as expected -- does anyone have more info on that? FYI I incorporated this change into the integrator code: http://code.google.com/p/zxing/source/browse/trunk#trunk%2Fandroid-integration%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fintegration%2Fandroid – Sean Owen Nov 28 '11 at 14:34
  • @Sean dear respected developer of zxing. is there a way to do not show google goggles as an option on the intent?, because at least my own google goggles app do not recognize QR codes properly – Pedro Teran Mar 08 '12 at 19:27
0

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.

Basil
  • 2,163
  • 2
  • 16
  • 25