Pages

Showing posts with label qr-code. Show all posts
Showing posts with label qr-code. Show all posts

Sunday, March 20, 2011

Including QR-reader in your app

Thanks to android intents, it is easy to include functionality from other apps to you own. Say for example you have an app that should be able to read QR-codes. You could easily call a barcode scanner to do the dirty work for you and just bother about the result.

Create a button that will launch Barcode Scanner from Zxing:
public Button.OnClickListener mScan = new Button.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.setPackage("com.google.zxing.client.android");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    }
};

Handle the callback for result:
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
        }
    }
}

This is a raw copy/paste from the ZXING-project. Nothing that I can take credit for, but I have tested it and it works as a charm.

Yes, it requires the user to have the Barcode Scanner installed. This can be handled smoothly by calling url instead - if the barcoder is installed - it will launch - otherwise launch a web browser (with a install guide of course). Very nice!

Wednesday, March 9, 2011

QR-code to launch your app

Android intents are powerful, you can even set you application to launch on a certain URL.

Launch app on defined URL

To make your app launch on a specific URL you simply create an intent filter.

<intent-filter>
  <action android:name="android.intent.action.VIEW"/>
  <category android:name="android.intent.category.DEFAULT"/>
  <category android:name="android.intent.category.BROWSABLE"/>
  <data android:scheme="http" android:host="xebralabs.blogspot.com" android:path="/travalert"/>
</intent-filter>

This way you can have your application launched from a QR-code containing the specific URL. Nice thing is that if the app isn't installed the user will most likely have the url opened in a browser instead.