Android 应用开发中,有时需要对应用是否能对 deeplink 做出正确处理进行测试,这时通过 ADB 模拟 deeplink 的打开是最方便的。
介绍
可以通过 ADB 的 am start 命令打开一个 deeplink/深度链接,无需指定处理它的 Activity,除了可以指定 deeplink 外,可以指定 action,包名 等信息。
adb shell am start -a android.intent.action.VIEW -d "https://itmob.cn/page/about" cn.itmob.demo
Starting: Intent { act=android.intent.action.VIEW dat=https://itmob.cn/... pkg=cn.itmob.deo }
Warning: Activity not started, its current task has been brought to the front
上面的命令即指定了 action 参数,也指定了用于打开 deeplink 的包名,此时这个 deeplink 会直接交由指定包名的应用处理,即使没有在应用设置的 “默认打开” 中添加要处理的 deeplink 链接,也会由该应用处理。
![Deeplink settings](/upload/2024/05/Deeplink-settings-e83a0212ea4d4aa1a2b6873e588b0ceb.jpg)
其他
也可以不指定包名,这时模拟的与真实应用场景更接近,系统会根据已知的过滤条件查找合适的应用来处理该 deeplink。
比如:
未指定包名,但指定了 action,将调用浏览器或支持处理它的应用来打开
adb shell am start -a android.intent.action.VIEW -d "https://itmob.cn/page/about"
Starting: Intent { act=android.intent.action.VIEW dat=https://itmob.cn/... }
![Deeplink handled by browser](/upload/2024/05/Deeplink-handled-by-browser-678d87169e70400b98a5227ae1d2b5a1.jpg)
未指定包名,也未指定 action,支持处理它的应用可能有多个,会弹出选择应用的弹窗
adb shell am start -d "https://itmob.cn/page/about"
Starting: Intent { dat=https://itmob.cn/... }
![Deeplink app selector](/upload/2024/05/Deeplink-app-selector-c3f79a41f9b14ca1957edc80422c25aa.jpg)