Android中使用App Links

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](http://upload-images.jianshu.io/upload_images/1871104-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"

相關代碼

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容