如何在Android AppLink pathPrefix配置中排除指定URL不被应用唤起
解决方案
原有配置会匹配所有https://url.com下的路径,所以才会把需要排除的about、privacy路径也捕获到。我们可以通过精准指定需要应用捕获的路径前缀实现需求,无需额外做排除配置,未匹配到规则的路径会自动走系统默认浏览器打开。
最终intent-filter配置
<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" /> <!-- 精准匹配 https://url.com/usernames 完整路径 --> <data android:scheme="https" android:host="url.com" android:path="/usernames" /> <!-- 匹配 https://url.com/brand/ 开头的所有子路径 --> <data android:scheme="https" android:host="url.com" android:pathPrefix="/brand/" /> <!-- 匹配 https://url.com/data/ 开头的所有子路径 --> <data android:scheme="https" android:host="url.com" android:pathPrefix="/data/" /> </intent-filter>
android:path="/usernames":严格匹配完整路径/usernames,只有路径完全一致的URL才会被应用捕获android:pathPrefix="/brand/":匹配所有以/brand/开头的路径,例如/brand/123、/brand/abc/def都符合规则- 未被上述三个规则命中的路径(包括你需要排除的
/about/、/privacy/开头的路径)都不会触发应用打开,会自动使用系统浏览器加载
补充方案:复杂规则下的动态排除
如果后续你的匹配规则变得更复杂,也可以保留原有全路径匹配的intent-filter配置,在接收跳转的Activity中做路径判断实现排除:
// 在接收跳转意图的Activity中添加如下逻辑 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Uri jumpUri = getIntent().getData(); if (jumpUri != null) { String path = jumpUri.getPath(); // 命中排除规则时直接调用浏览器打开并关闭当前页 if (path.startsWith("/about/") || path.startsWith("/privacy/")) { Intent browserIntent = new Intent(Intent.ACTION_VIEW, jumpUri); startActivity(browserIntent); finish(); return; } } // 原有业务逻辑 }
内容的提问来源于stack exchange,提问作者Mohsen Haydari
相关产品推荐
相关产品推荐

