Android start application from QR Code with params
Asked Answered
P

3

26

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.

Pet answered 21/4, 2012 at 11:51 Comment(0)
P
23

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.

Pet answered 8/5, 2012 at 7:38 Comment(0)
O
10

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.

Observer answered 21/4, 2012 at 12:36 Comment(0)
O
-3

Yes It is possible by using intent.

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) 
     {
              final String contents = intent.getStringExtra("SCAN_RESULT");
        Toast.makeText(this, contents, Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) 
        {
    Toast.makeText(this, "Not proper QRCODE...!",Toast.LENGTH_SHORT).show();
    }
}
}
Outoftheway answered 21/4, 2012 at 12:26 Comment(1)
This lets an app start the Barcode Scanner. The OP is asking how the Barcode Scanner can start an app using a QR code.Observer

© 2022 - 2024 — McMap. All rights reserved.