You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Android应用实现自定义URL跳转并传递参数

配置Android应用以拦截指定URL并打开应用传递参数

1. 在AndroidManifest.xml中添加URL关联的Intent Filter

你需要在承载WebView的Activity标签内,添加一个新的<intent-filter>,用于匹配你的目标URL。这个过滤器会告诉系统,当用户点击符合规则的URL时,你的应用是可选的打开方式。

修改后的Manifest配置示例:

<activity android:name=".YourWebViewActivity">
    <!-- 原有的LAUNCHER过滤器 -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <!-- 新增的URL拦截过滤器 -->
    <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="https"
            android:host="tcg-wallet.ga"
            android:pathPrefix="/home" />
    </intent-filter>
</activity>
  • android:autoVerify="true":适用于Android 12+,配置后系统会自动验证应用与域名的关联,无需用户每次选择打开方式(需配合后续的assetlinks.json配置)。
  • android:scheme="https":匹配HTTPS协议的URL。
  • android:host="tcg-wallet.ga":指定你的域名。
  • android:pathPrefix="/home":匹配以/home开头的路径,若想拦截该域名下所有URL,可改为android:pathPrefix="/"。

2. 在Activity中处理传入的URL参数

当用户点击URL打开应用时,系统会将URL数据通过Intent传递给Activity,你需要在Activity中提取该URL并传递给WebView加载。

在WebView Activity的onCreate(或onNewIntent,若Activity启动模式为singleTop等)方法中添加以下代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    
    WebView webView = findViewById(R.id.your_webview_id);
    // 初始化WebView配置(启用JavaScript等,按需调整)
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    
    // 处理传入的Intent数据
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // 当Activity已存在时,处理新的Intent
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    Uri data = intent.getData();
    if (data != null) {
        String targetUrl = data.toString();
        // 让WebView加载传入的URL(包含搜索参数)
        webView.loadUrl(targetUrl);
    } else {
        // 无传入URL时加载默认首页
        webView.loadUrl("https://tcg-wallet.ga");
    }
}

3. Android 12+自动验证配置(可选但推荐)

为了让系统默认用你的应用打开该URL(无需用户选择),需在域名的.well-known目录下放置assetlinks.json文件,内容如下:

[{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "你的应用包名",
    "sha256_cert_fingerprints": ["你的应用签名的SHA256指纹"]
  }
}]
  • 替换你的应用包名为AndroidManifest中声明的包名。
  • 获取SHA256指纹:可通过Android Studio的Build > Generate Signed Bundle/APK流程查看,或使用命令行keytool -list -v -keystore 你的签名文件路径获取。

4. 测试验证

你可以通过以下方式测试配置是否生效:

  • 使用adb命令模拟打开URL:
adb shell am start -a android.intent.action.VIEW -d "https://tcg-wallet.ga/home?search=MP21-EN056&event=search"
  • 在浏览器或WhatsApp中点击分享的URL,检查是否会打开你的应用并加载对应页面。

内容的提问来源于stack exchange,提问作者user10771503

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 18:40:25