Android start application from QR Code with params

2019-01-17 17:00发布

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.

3条回答
聊天终结者
2楼-- · 2019-01-17 17:46

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.

查看更多
Viruses.
3楼-- · 2019-01-17 17:49

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();
    }
}
}
查看更多
三岁会撩人
4楼-- · 2019-01-17 17:56

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"/>

That should work.

查看更多
登录 后发表回答