Xamarin Android深度链接无法唤起应用,点击链接跳转至浏览器
Xamarin Android Deep Link 无法唤起应用的排查与实现指南
问题排查:你的Intent Filter配置错误
看你的代码,Intent Filter的Categories数组里错误包含了Android.Content.Intent.ActionView——这属于动作(Action),并非类别(Category),会直接导致Intent匹配失败。正确的Categories只需保留CategoryDefault和CategoryBrowsable。
修正后的Intent Filter代码:
[Activity(Label = "Test", Icon = "@mipmap/icon", Theme = "@style/MainTheme.Splash", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)] [IntentFilter(new[] { Android.Content.Intent.ActionView }, Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable }, DataScheme = "https", DataHost = "live.test.it", DataPathPrefix = "/", AutoVerify = true) ] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { // ... 你的业务代码 }
其他需验证的环节
AssetLinks.json 有效性
- 确认文件放在网站根目录的
.well-known子目录下(可通过访问https://live.test.it/.well-known/assetlinks.json验证) - 文件内容必须包含应用包名和签名证书的SHA-256指纹,格式示例:
[{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "com.yourcompany.test", "sha256_cert_fingerprints": ["你的应用SHA256指纹"] } }] - 确保文件可公开访问,无404或权限限制,且返回的Content-Type为
application/json
- 确认文件放在网站根目录的
设备与系统版本限制
- App Links(AutoVerify特性)仅支持Android 7.0(API 24)及以上版本;低版本系统会 fallback 到普通Deep Link,需用户手动选择打开方式
- 优先使用物理设备测试,部分模拟器可能存在验证逻辑异常
快速测试方法
- 使用ADB命令直接触发Intent,验证配置是否生效:
adb shell am start -W -a android.intent.action.VIEW -d "https://live.test.it/" com.yourcompany.test - 如果命令能唤起应用,说明Intent配置正确,问题大概率出在AssetLinks验证或浏览器链接处理逻辑
- 使用ADB命令直接触发Intent,验证配置是否生效:
Xamarin Android Deep Link 最新实现步骤
1. 配置Activity的Intent Filter
- 为目标Activity添加
IntentFilter,指定ActionView动作、CategoryDefault+CategoryBrowsable类别,以及对应DataScheme、DataHost、DataPathPrefix - 开启
AutoVerify = true(Android 7.0+),启用App Links自动验证
2. 配置网站的AssetLinks.json
- 在网站
.well-known目录下创建assetlinks.json,填写正确的应用包名和SHA256指纹 - 可通过Visual Studio/Xamarin Studio获取指纹:右键项目 → Android选项 → 签名 → 查看证书详情
3. 处理Deep Link跳转逻辑
在Activity的OnCreate或OnNewIntent方法中,解析Intent数据并处理页面跳转:
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); HandleDeepLink(Intent); } protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); HandleDeepLink(intent); } private void HandleDeepLink(Intent intent) { if (intent != null && intent.Data != null) { var deepLinkUrl = intent.Data.ToString(); // 根据URL导航到对应Xamarin.Forms页面 // 示例:App.Current.MainPage = new NavigationPage(new TargetPage(deepLinkUrl)); } }
4. 验证与测试
- 使用ADB命令测试Intent匹配情况
- Android 7.0+设备安装应用后,系统会自动验证AssetLinks.json,验证通过后点击链接将直接唤起应用
- 可通过
adb shell dumpsys package domain-preferred-apps命令查看App Links验证状态
内容的提问来源于stack exchange,提问作者L.Zoffoli
相关产品推荐
相关产品推荐

