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

Kotlin Android WebView应用中深度链接的捕获与处理

解决Android深度链接唤起应用后WebView无法加载目标链接的问题

1. 正确获取Intent中的深度链接数据

外部通过深度链接唤起应用时,目标URL会携带在Intent的data字段中。如果之前未读取该数据,WebView只会加载默认页面,无法显示目标链接。

在MainActivity的onCreate方法中优先读取并加载这个深度链接:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView webView = findViewById(R.id.webview);

    // 配置WebView基础属性,保证页面正常渲染
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setUseWideViewPort(true);

    // 读取深度链接并加载
    Intent intent = getIntent();
    Uri deepLinkUri = intent.getData();
    if (deepLinkUri != null) {
        webView.loadUrl(deepLinkUri.toString());
    } else {
        // 无深度链接时加载默认首页
        webView.loadUrl("https://mynewdom.com");
    }

    // 设置WebViewClient处理链接跳转
    setupWebViewClient(webView);
}

2. 修正shouldOverrideUrlLoading的跳转逻辑

确保mynewdom.com域名下的链接由WebView自行加载,外部域名链接交给系统浏览器处理,同时兼容API 24前后的方法重载:

private void setupWebViewClient(WebView webView) {
    webView.setWebViewClient(new WebViewClient() {
        // API 24+ 版本适配
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            Uri uri = request.getUrl();
            return handleUrlLoad(view, uri);
        }

        // 兼容API 24以下版本
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Uri uri = Uri.parse(url);
            return handleUrlLoad(view, uri);
        }

        // 统一处理链接加载逻辑
        private boolean handleUrlLoad(WebView view, Uri uri) {
            String host = uri.getHost();
            if (host != null && host.equals("mynewdom.com")) {
                // 同一域名,让WebView加载目标链接
                view.loadUrl(uri.toString());
                return true;
            }
            // 外部域名,跳转系统浏览器
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
            return true;
        }
    });
}

3. 处理应用后台唤起的场景

如果应用已在后台运行,外部点击深度链接会触发onNewIntent而非onCreate,需在此方法中更新并加载新链接:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent); // 更新当前Intent为新的深度链接Intent
    WebView webView = findViewById(R.id.webview);
    Uri deepLinkUri = intent.getData();
    if (deepLinkUri != null) {
        webView.loadUrl(deepLinkUri.toString());
    }
}

4. 确认AndroidManifest的深度链接配置

确保intent-filter能匹配mynewdom.com下的所有子链接,避免仅匹配根域名:

<activity android:name=".MainActivity">
    <!-- 主入口规则 -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </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协议下mynewdom.com的所有路径 -->
        <data
            android:scheme="https"
            android:host="mynewdom.com"
            android:pathPattern=".*" />
    </intent-filter>
</activity>

关键排查点

  • 打印deepLinkUri确认Intent数据是否正确传递:Log.d("DeepLink", "Uri: " + deepLinkUri)。
  • 检查WebView是否存在自定义拦截逻辑,阻止了链接加载。
  • 测试两种唤起场景:应用未启动时唤起、应用后台运行时唤起,确保都能正确加载目标链接。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 20:31:03