为传入链接添加 intent 过滤器
可以为URI指定首选应用,当在浏览器中点击链接时,Android系统可以选择直接打开App定位对应页面。
官方案例中XML配置如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<activity
android:name="com.example.android.GizmosActivity"
android:label="@string/title_gizmos" >
<intent-filter android:label="@string/filter_view_http_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/gizmos" />
<!-- note that the leading "/" is required for pathPrefix-->
</intent-filter>
<intent-filter android:label="@string/filter_view_example_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "example://gizmos” -->
<data android:scheme="example"
android:host="gizmos" />
</intent-filter>
</activity>
这几个属性都是必须要的,其中category
中android.intent.category.BROWSABLE
表示如果需要从网络浏览器中访问intent
过滤器,才有可能点击链接时才能链接到对应应用。
以上两组intent
过滤器唯一区别都在data
上,是因为当同一个intent
目录下,若出现多组data
,其scheme
和host
属性是会进行组合,形成更多组的组合情况。例如:
1
2
3
4
5
<intent-filter>
...
<data android:scheme="https" android:host="www.example.com" />
<data android:scheme="app" android:host="open.my.app" />
</intent-filter>
对应的组合就会支持https://www.example.com
、app://open.my.app
、app://www.example.com
和https://open.my.app
这四种情况的链接。
配置应用链接
若想使浏览器中的uri可以正确跳转到对应的app,还需要在对应域名网站的对应URL下托管一下json文件,声明网站链接与app之间的关系。尽管data
中可能配置的scheme
为http链接,但这里的网址必须要为https的。
1
https://domain.name/.well-known/assetlinks.json
例如
1
https://static.jiangker.cn/.well-known/assetlinks.json
在json文件中添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
{
"relation": [
"delegate_permission/common.handle_all_urls"
],
"target": {
"namespace": "android_app",
"package_name": "cn.jiangker.jrouter",
"sha256_cert_fingerprints": [
"16:C6:BF:9D:66:E5:D5:93:2E:2A:53:31:C7:1F:8A:BE:E3:8A:D4:CF:C6:9F:10:ED:B9:2F:71:11:D4:E6:11:43"
]
}
}
]
此json文件的生成可以使用Statement List Generator and Tester来生成,并测试能否正确配置网站。
生成json文件需要获取app的sha256,可以使用./gradlew signingReport
命令来生成
AS配置intent以及生成json文件
intent
的配置以及json文件的生成都可以由AS来完成,在Tools -> App Links Assistant来完成
我目前AS的Associate website有一点问题,所以不在这里演示了。
adb测试链接支持情况
1
2
3
adb shell dumpsys package domain-preferred-apps
// or
adb shell dumpsys package d
在Android11以及之前
1
2
3
4
App verification status:
Package: cn.jiangker.jrouter
Domains: static.jiangker.cn
Status: always : 20000000b
读取传入的intent
当通过url跳转到app之后,可以通过intent来获取对应的url,以便解析对应的url来获取参数。
1
2
3
4
5
6
7
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
val action: String? = intent?.action
val data: Uri? = intent?.data
}
Applink测试
1
2
3
adb shell am start -a android.intent.action.VIEW \
-c android.intent.category.BROWSABLE \
-d <URI>
例如
1
2
3
adb shell am start -a android.intent.action.VIEW \
-c android.intent.category.BROWSABLE \
-d "https://static.jiangker.cn"
在app中进行测试,浏览器中经常不太能跳转过去,浏览器中目前测试<a/>
标签的形式手动点击才行,别的都只能在浏览器中跳转。但是如果是短信中的链接就比较顺利。