当我们在应用中点击一个协议链接,通常会提示我们选择合适的浏览器或者app去打开它。
- <activity android:name=".MainActivity">
- <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="http"
- android:host="www.stone86.top"
- android:pathPattern="/magic"/>
- </intent-filter>
- </activity>
如上,定义后 , 安卓上打开web链接: www.stone86.top/magic 可以匹配到 该 MainActivity
scheme可以随意设置。访问形式还是跟上面类似的: scheme://host/pathPatteren
只是在6.0以后多了一个默认处理。
要想实现Android 6.0 中的”让app作为对给定类型链接的默认处理者”。要再增加如下配置:
- <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="https"
- android:host="stone86.top"
- android:pathPattern="/magic"/>
- </intent-filter>
自动会访问该站点进行验证。为了自动验证,向阿里云申请了一天的https+cdn来测试,实际访问的是:www.stone86.top/.well-known… 该验证服务必须是https的站点;实际测试时,明明站点的json是可以访问的,但验证还是没通过,猜想,可能是国内服务器的问题
- android:autoVerify="true"
文件夹名必须是
,文件名为
- .well-known
,内容是:
- assetlinks.json
- [{
- "relation": ["delegate_permission/common.handle_all_urls"],
- "target": {
- "namespace": "android_app",
- "package_name": "com.stone.mvp",
- "sha256_cert_fingerprints":
- ["81:6D:ED:45:12:AE:0D:89:04:CC:5F:DB:E6:BB:62:31:64:4A:14:4D:54:9B:CA:48:95:33:C6:AF:F3:3D:4A:6F"]
- }
- }]
可以使用java的keytool命令来获取fingerprints:$ keytool -list -v -keystore my-release-key.keystore。
当app运行,验证通过后;再打开该链接,就会查找到系统中,已配置了其对应的app,就不会弹出其它可接受网页category的应用选择框了;会直接打开app。
- adb shell am start -a android.intent.action.VIEW \
- -c android.intent.category.BROWSABLE \
- -d "http://www.stone86.top/magic" "com.stone.mvp"
当不含后面的package值时,还是会弹出多app选择框。因为验证可能失败了。
测试网站与url的绑定命令:
- adb shell dumpsys package domain - preferred - apps
输出:
- Package: com.google.android.calendar
- Domains: www.google.com calendar.google.com
- Status: undefined //未明确定义
- Package: com.stone.mvp
- Domains: stone86.top www.stone86.top
- Status: ask //询问
- Package: com.google.android.youtube
- Domains: youtu.be m.youtube.com youtube.com www.youtube.com
- Status: always : 200000001 //有always,表示是 成功
- String url = "http://stone86.top/magic?key=stone"; //这里加了 ?key参数
- Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
- in.setPackage("com.stone.mvp"); //没有这句可能会有选择框,看验证的绑定情况;反之,有就能直接打开
- startActivity(in);
- val queryParameter = intent?.data?.getQueryParameter("key")
- if (queryParameter != null) {
- println("stone.stone queryParameter=" + queryParameter)
- }
- String url = "http://stone86.top/magic?key=stone";
- WebView view = new WebView(this);
- view.loadUrl(url); //可能会有选择框,看验证的绑定情况
AS3.0 , 菜单栏 Tools > App Links Assistant 可以快速操作 App Links
来源: https://juejin.im/entry/59faed14518825603a37f5cd