AndroidManifest.xml含#特殊字符的pathPrefix链接拦截问题
这个问题我之前也踩过坑!核心原因是Android的
你的目标URL是https://thus.customapp.it/#/app/customapp/itemDetail/45289348293423,系统解析后会把:
- scheme: https
- host: thus.customapp.it
- path: /
- fragment: /app/customapp/itemDetail/45289348293423
你之前写的所有pathPrefix="/#/..."或者pathPattern="/#/.*"都是在匹配path部分,但实际path只有/,所以这些规则根本不会命中,只有pathPattern="/.*"能生效,因为它匹配了根路径/。
下面给你两种可行的解决方案:
方案一:修改URL结构(推荐)
如果可以调整URL的格式,把Fragment部分移到路径里(去掉#),比如改成:https://thus.customapp.it/app/customapp/itemDetail/45289348293423
这时你只需要用最初的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="https" android:host="thus.customapp.it" android:pathPrefix="/app/customapp/itemDetail"/> </intent-filter>
方案二:在Activity中手动过滤Fragment(无法修改URL时使用)
如果不能改URL,只能先通过宽泛的规则捕获所有来自thus.customapp.it的HTTPS链接,然后在Activity里手动判断Fragment内容:
1. 调整Manifest配置
先配置一个能捕获目标域名所有HTTPS链接的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="https" android:host="thus.customapp.it" /> </intent-filter>
2. 在Activity中过滤并处理
在对应的Activity里,获取Intent中的Fragment部分,判断是否包含你的目标路径:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_activity_layout); handleDeepLink(getIntent()); } // 处理从后台唤起的情况 @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); handleDeepLink(intent); } private void handleDeepLink(Intent intent) { Uri uri = intent.getData(); if (uri == null) return; String fragment = uri.getFragment(); // 检查Fragment是否包含目标路径 if (fragment != null && fragment.startsWith("/app/customapp/itemDetail")) { // 这里处理你的业务逻辑,比如跳转到详情页 // 可以从fragment中提取ID:String itemId = fragment.split("/")[5]; } else { // 如果不是目标链接,跳转回浏览器打开 Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri); startActivity(browserIntent); finish(); } }
这样就能精准拦截包含itemDetail的链接,同时把其他链接(比如editItem)交还给浏览器处理。
内容的提问来源于stack exchange,提问作者panagulis72

