美文网首页
Android中使用App Links

Android中使用App Links

作者: 治學涯 | 来源:发表于2016-12-02 21:52 被阅读3235次

    App Links是谷歌在2015年的I/O大会上宣布了一个新特性:允许开发者将app和他们的web域名关联。这一举措是为了最小化用户遇到“打开方式”对话框的概率。

    开发者如何实现App Links

    在manifest中添加<intent-filter>标签


    AppLinks intent-filter.png
    <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="ohjh9h7st.qnssl.com" android:scheme="http"/>    
        <data android:host="ohjh9h7st.qnssl.com" android:scheme="https"/>
    </intent-filter>
    

    android:autoVerify="true" 为配置的自动验证机制
    此机制会在App第一次安装中自动验证,因此在每次点击跳转不会有应用打开延时的问题


    创建语句列表

    详见Anroid developers官网

    Statement List Generator and Tester 工具生成assetlinks.json

    • 首先获取得到你应用的签名证书SHA256


      App package fingerprint.png

    可以通过下面语句取得SHA256
    keytool -list -v -keystore FausgoalRepositoryApp.jks

    • 下面便是生成的assetlinks.json语句直接复制出来保存assetlinks.json文件即可


      assetlinks.json.png

    填写完成后,把assetlinks.json上传到你的服务器上可以使用TEST STATEMENT 按钮测试是否验证成功
    验证成功了,重新编译安装你的程序,当点击http[s]://ohjh9h7st.qnssl.com 相关链接时即可直接启动你的APP

    <i>Note:assetlinks.json文件中是一个JSON数组,可存放多个配置</i>


    语句列表位置

    详见Anroid developers官网

    • 网站声明语句

    scheme://domain/.well-known/assetlinks.json
    Note :.well-known/assetlinks.json 为固定值
    assetlinks.json 文件必须是 Content-Type: application/json格式类型的json文件

    例如:

    我这里配置的的为:https://ohjh9h7st.qnssl.com/.well-known/assetlinks.json

    • Android应用程序声明语句

    AndroidManifest.xml:

    <manifest>
     <application>
       ...
       <meta-data android:name="asset_statements" android:resource="@string/asset_statements" />
       ...
     </application>
    </manifest>
    > ```
    > res/values/strings.xml:
    > ```
    <resources>
     ...
     <string name="asset_statements">
       ... statement list ...
     </string>
    </resources>
    > ```
    
    ##### 例如:
    ![asset_statements.png](https://img.haomeiwen.com/i1871104/587f307cea148134.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    > 我这里配置的为:
    

    <string name="asset_statements" translatable="false">
    [{
    "include": "https://ohjh9h7st.qnssl.com/.well-known/assetlinks.json"
    }]
    </string>

    也可直接配置为:
    
    <resources>
        ...
        <string name="asset_statements">
          [{
            \"relation\": [\"delegate_permission/common.share_location\"],
            \"target\": {
              \"namespace\": \"web\",
              \"site\": \"https://example.com\"
            }
          }]
        </string>
    </resources>
    
    测试:
    adb shell am start -a android.intent.action.VIEW \ -c android.intent.category.BROWSABLE \ -d "http://ohjh9h7st.qnssl.com"
    

    相关代码

    相关文章

      网友评论

          本文标题:Android中使用App Links

          本文链接:https://www.haomeiwen.com/subject/xfpqmttx.html