如何配置AndroidManifest的path/pathPattern以匹配DeepLink唤起应用?
AndroidManifest 配置方案适配指定URL唤起应用
针对你给出的URL https://somesite.com//#/?token=H41SB9SN,需要明确一个关键点:URL中#之后的部分属于Fragment(锚点),Android系统在匹配Intent Filter的path/pathPattern规则时,只会识别#之前的路径内容(也就是该URL的路径部分为/)。
以下是具体的AndroidManifest配置步骤:
- 在需要被唤起的Activity标签内,添加
<intent-filter>配置,指定对应的scheme、host,同时配置必要的action和category:
<activity android:name=".YourTargetActivity" android:exported="true"> <intent-filter> <!-- 指定URL的scheme为https --> <data android:scheme="https" android:host="somesite.com" android:path="/" /> <!-- 必须配置的action和category --> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> </activity>
- 如果需要适配更灵活的路径匹配(比如该域名下任意路径都能唤起应用),可以把
path="/"替换为pathPattern=".*":
<data android:scheme="https" android:host="somesite.com" android:pathPattern=".*" />
补充说明
因为#后的Fragment部分不会被Intent Filter的路径规则匹配,所以如果需要获取token参数,需要在目标Activity中通过以下代码解析:
Uri uri = getIntent().getData(); if (uri != null) { String fragment = uri.getFragment(); // fragment内容为"?token=H41SB9SN",可自行解析token参数 if (fragment != null && fragment.startsWith("?")) { String[] params = fragment.substring(1).split("&"); for (String param : params) { if (param.startsWith("token=")) { String token = param.split("=")[1]; // 处理token逻辑 } } } }
内容的提问来源于stack exchange,提问作者pulancheck1988
相关产品推荐
相关产品推荐

