App Links的使用以及坑

724 阅读2分钟

前言

App Links可以从一个URL或者短信中直接启动APP。不过只能在Android 6.0的版本上才能使用。

配置

在想调用的Activity上加上

<intent-filter android:autoVerify="true">
        <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="www.example.com" />
        <data android:scheme="https" />
    </intent-filter>

但是如果是在启动页,不要将启动页的intent-filter和这个一起写,要分开。比如

  <activity
            android:name=".LaunchActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="xxx.com"
                    android:pathPrefix="/xxx"
                    android:scheme="https"/>
            </intent-filter>
        </activity>

android:host可以填写:baidu.com android:pathPrefix是匹配的路径,比如baidu.com/download,这里android:pathPrefix就填:/download 这里需要注意的是,不要在baidu.com的域名加上www.,我在小米手机上试了,是无法启动APP的。比如你的网址是www.baidu.com,那么android:host="baidu.com"。切记切记切记。我在调试的时候就是因为这个无法启动APP。

在网站上创建assetlinks.json文件

文件名一定要是assetlinks.json。不要更改。

文件的内容

[{  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "你的包名",
    "sha256_cert_fingerprints":
    ["证书指纹SHA256的值"]
  }
}]

获取SHA256的值 输入adb的命令:keytool -list -v -keystore "填写jks的路径" 输入后会输入密码,然后就找证书指纹SHA256的值。 记得给服务器的证书指纹SHA256要和你运行的APP一致。有人就有可能用了正式的签名,却用了debug的包。 然后将该文件放在网站的.well-known目录下,放了之后要试试能不能用:

https://domain.name/.well-known/assetlinks.json

测试是否绑定成功

在Android studio上命令行输入:

adb shell dumpsys package domain-preferred-apps

如果存在绑定的会显示如下结果:

Package: com.android.vending Domains: play.google.com market.android.com Status: always : 200000002

Status: always是代表绑定了,但是Status: undefined或者Status: ask都是没有绑定成功。如果Status: ask时,检查一下host是否加了www.。如果加了就要去掉。

优点:

  • 如果APP安装的话,不会弹选择框,直接启动APP
  • 可以直接通过url跳到对应的activity

缺点:

  • 需要Android 6.0以上
  • 网站需要支持https
  • 有个校验过程,步骤麻烦些。