Intent Filter处理pathPattern、pathPrefix中#号匹配与转义问题咨询
问题核心原因
Android 系统对 Uri 的解析遵循 RFC 规范,# 是 Uri 中**片段(Fragment)**的分隔符,# 及之后的所有内容都会被解析到 Fragment 字段,不会被计入 Path 部分。所以无论怎么给 PathPattern 写转义规则,都匹配不到 # 相关的内容,这是你之前几种尝试都失败的根本原因。
可行解决方案
1. 调整 Intent Filter 配置
不要配置 android:path/android:pathPattern 这类路径匹配规则,只配置 scheme、host 即可,让所有对应域名的链接都能先拉起你的 Activity:
<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="https" android:host="myapp.com" /> </intent-filter>
2. 页面内自行解析 Uri 匹配路由
在对应 Activity 的 onCreate/onNewIntent 方法中,拿到传入的 Uri 后,自行拼接 Path 和 Fragment 做匹配:
// 拿到传入的Uri Uri uri = getIntent().getData(); if (uri != null) { // 拼接完整路径:原有path + # + fragment String fullPath = uri.getPath() + "#" + uri.getFragment(); // 自行实现匹配逻辑,示例为判断是否符合 /#/path1/ 开头的格式 if (fullPath.matches("/#/path1/.*")) { // 匹配成功,执行对应业务逻辑 } else { // 匹配失败,可做降级处理,比如跳转404页面或者返回系统浏览器打开 } }
关于覆写系统UriMatcher的说明
你无法覆写系统用于 Intent Filter 匹配的默认 UriMatcher,这部分逻辑运行在系统进程的 ActivityManagerService 中,应用层没有权限修改。但你完全可以在应用内自己实现 Uri 匹配逻辑,替代系统的路径匹配能力,完全可以满足需求。
内容的提问来源于stack exchange,提问作者Mourad Lachheb
相关产品推荐
相关产品推荐

