1

When you open a browser and scan a QR code, you need to start a specific app. To achieve this requirement, we first need to parse the QR code of the HTML page. Usually, the format of the parsed content is:

 <a href="[scheme]://[host]/[path]?[query]">启动App</a> 
//测试链接
<a href="myapp://jp.app/openwith?name=zhangsan&age=26">启动App</a>
  • scheme: Identify the launched App.
  • host: Describe appropriately.
  • path: the required key when passing the value, if there is none, it can be omitted.
  • query: Get the Key and Value of the value, you can not pass it if there is none.

First, we open the AndroidManifest.xml configuration file of the Android project, and then append the following content under the Activity on the startup page.

 <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="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter>

It should be noted that the content of the intent-filter [android.intent.action.MAIN] and [android.intent.category.LAUNCHER] cannot be mixed with the content added this time. Because if the same Activity is added, it will cause the application icon to disappear on the desktop, so the general configuration is as follows.

 <intent-filter>  
    <action android:name="android.intent.action.MAIN"/>  
    <category android:name="android.intent.category.LAUNCHER" />  
</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="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter>

Next, add the following code in the Activity where the value needs to be taken, which can be written directly in the OnCreate function.

 val value = intent
        val action = value.action
        if (Intent.ACTION_VIEW == action) {
            val uri: Uri? = value.data
            if (uri != null) {
                val name: String? = uri.getQueryParameter("name")
               val age: String? = uri.getQueryParameter("age")
                ...  //处理业务
            }
        }

After the above processing, you can complete the function of scanning the code in the browser to open the Android/iOS App. It should be noted that you must use your own browser or Google Chrome, do not use Uc or Tencent browser.

Recently encountered such a requirement: when the user clicks a button in the mobile phone browser, if the application is already on the mobile phone, it will be opened directly, and if it is not installed, it will turn to the application download page. For this requirement, you can use the method of [scheme://host:port/path or pathPrefix or pathPattern], the following Android development documents:

 http://developer.android.com/guide/topics/manifest/data-element.html

The following is the specific sample code:

 <a id="applink1" href="http://test.xx.com/demo/test.php">打开</a>

Then, add the following filter to the target Activity.

 <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:host="test.xx.com "
       android:path="/demo/test.php"
       android:scheme="http" /> 
</intent-filter>

After adding the filter, the Activity can handle http://test.xx.com/demo/test.php . When you click "Start" in the browser and initiate a request for the URL, if the application is installed on the machine, the system will pop up a selection, asking you whether you want to use the browser to open it or use the application to open it.
在这里插入图片描述


xiangzhihong
5.9k 声望15.3k 粉丝

著有《React Native移动开发实战》1,2,3、《Kotlin入门与实战》《Weex跨平台开发实战》、《Flutter跨平台开发与实战》1,2和《Android应用开发实战》