Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android start application from QR Code with params

Tags:

I want to know if it's possible in android to start application using QR Code reader. The things that I want to achieve is :

I create QR Code and after scanning it with QR Code reader I need to start my application with some params, maybe it will looks something like this : myApp://org.hardartcore.myApp?myParams or maybe something similar to this, not really sure.

Is there anyway to achieve this and to get the param which is build in the qr code with the intent for launching the application.

like image 962
Android-Droid Avatar asked Apr 21 '12 11:04

Android-Droid


2 Answers

Create QR CODE with this text : myApp://extraString and read it with any qr code reader. Or even you can integrate your own qr code reader using Zxing's open source. And you can get the extraString as @Sean Owen mentioned using getIntent().getDataString(). And don't forget to add this in your manifest file :

<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="myApp"/>
</intent-filter>

That should work.

like image 114
Android-Droid Avatar answered Sep 19 '22 06:09

Android-Droid


Yes. In AndroidManifest.xml, in your <activity>, declare that the app responds to this URL:

  <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="myApp" android:host="org.hardartcore.myApp" android:path="/"/>
  </intent-filter>

(I think you may have to end with a "/" before your "?" for this to work.)

Then anything that uses the platform to resolve a URL will open your app. Hyperlinks in a web browser will work.

The URL itself can be retrieved with getIntent().getDataString(). You can parse it as an android.net.Uri as you like.

Look at CaptureActivity in ZXing for an example of how it does this.

like image 37
Sean Owen Avatar answered Sep 20 '22 06:09

Sean Owen