如何在Applinks(Deeplink)中排除特定路径?
Android Applinks 排除特定URL路径的实现方案
针对你的需求,这里有几种可行的实现方式:
1. 在处理Applinks的Activity中做拦截判断
这是最直接且灵活的方案,无需修改assetlinks.json或intent-filter的基础配置。核心逻辑是:当Applinks触发应用跳转后,在接收Intent的Activity里先判断URL路径是否为需要排除的/redirects,如果是,就将链接转交给浏览器打开,然后结束当前Activity;其他正常路径则继续执行应用内跳转逻辑。
示例代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handleIncomingLink(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); handleIncomingLink(intent); } private void handleIncomingLink(Intent intent) { Uri incomingUri = intent.getData(); if (incomingUri == null) return; String path = incomingUri.getPath(); // 判断是否为需要排除的路径 if (path != null && path.startsWith("/redirects")) { // 跳转至浏览器打开链接 Intent browserIntent = new Intent(Intent.ACTION_VIEW, incomingUri); startActivity(browserIntent); finish(); return; } // 其他正常路径的应用内跳转逻辑 // ... }
2. 拆分intent-filter,精确配置允许的路径
虽然intent-filter不支持排除规则,但可以通过精确匹配所有允许的路径间接实现排除效果。这种方式适合允许的路径数量不多的场景,把所有需要打开应用的路径都明确配置在intent-filter中,未配置的路径(比如/redirects)就不会触发应用跳转。
示例配置:
<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" /> <!-- 匹配根路径 --> <data android:scheme="http" android:host="www.example.com" android:path="/" /> <!-- 匹配dashboard路径及其所有子路径 --> <data android:scheme="http" android:host="www.example.com" android:pathPrefix="/dashboard" /> <!-- 可继续添加其他允许的路径前缀或精确路径 --> <!-- <data ... /> --> </intent-filter>
3. 通过中间跳转服务做路由判断(需后端配合)
如果你的项目有后端支持,可以搭建一个简单的路由服务:将原Applinks域名的请求全部转发到该服务,服务层判断URL路径,若为/redirects则直接返回跳转至浏览器的响应,其他路径则返回标准的Applinks配置,引导跳转至应用。这种方式适合路径规则复杂、需要动态调整的场景。
内容的提问来源于stack exchange,提问作者radkrish
相关产品推荐
相关产品推荐

