
本教程详细介绍了如何在基于 webview 的 android 应用中实现深度链接,使其能够拦截并处理特定 url,从而在应用内部而不是外部浏览器中打开这些链接。通过配置 `androidmanifest.xml` 中的 `intent-filter`,并学习如何在 activity 中获取和解析传入的 url 数据,开发者可以为用户提供更流畅的应用内体验,确保从分享链接或其他应用启动时,内容直接在您的应用中呈现。
在 Android 应用开发中,深度链接(Deep Linking)允许用户直接跳转到应用内的特定内容,而不是仅仅打开应用的主界面。对于包含 WebView 的应用而言,一个常见的需求是当用户点击一个指向应用所加载网页的 URL 时,希望该链接能在应用内部的 WebView 中打开,而非启动外部浏览器。这不仅提升了用户体验,也保持了应用的上下文一致性。
当通过 WhatsApp 或其他社交媒体分享包含应用相关域名的 URL 时,如果未正确配置深度链接,点击该 URL 会默认在设备的默认浏览器中打开。要解决此问题,核心在于告知 Android 操作系统您的应用能够处理特定类型的 URL。
实现深度链接的关键在于在应用的 AndroidManifest.xml 文件中为相应的 Activity 添加一个 intent-filter。这个过滤器声明了您的应用可以响应特定的隐式 Intent,特别是那些用于查看(ACTION_VIEW)特定 URI 的 Intent。
假设您的应用旨在处理 https://tcg-wallet.ga 域名的所有链接,您需要将以下 intent-filter 添加到负责处理这些链接的 Activity(通常是启动 Activity 或一个专门的 DeepLinkActivity)中:
<activity android:name=".YourTargetActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="tcg-wallet.ga"
android:scheme="https" />
</intent-filter>
<!-- 如果此 Activity 也是主启动器,保留以下过滤器 -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>intent-filter 各部分详解:
配置完成后,当用户点击一个形如 https://tcg-wallet.ga/home?search=MP21-EN056 的链接时,Android 系统将识别出您的应用能够处理此 URL,并会提示用户选择在您的应用中打开,或者如果您的应用是唯一的处理程序,则直接在应用中打开。
一旦您的应用通过深度链接被启动,您需要在接收该 Intent 的 Activity 中提取并处理 URL 数据。这通常在 Activity 的 onCreate() 方法中完成。
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.webkit.WebView;
public class YourTargetActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 假设您的布局包含一个WebView
webView = findViewById(R.id.your_webview_id); // 替换为您的WebView ID
// 配置 WebView 设置,例如启用JavaScript等
webView.getSettings().setJavaScriptEnabled(true);
// 检查是否有传入的 Intent 数据
Intent intent = getIntent();
if (intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_VIEW)) {
Uri data = intent.getData();
if (data != null) {
String fullUrl = data.toString();
// 打印或处理接收到的完整 URL
System.out.println("Received Deep Link URL: " + fullUrl);
// 将 URL 加载到 WebView 中
webView.loadUrl(fullUrl);
// 您也可以解析 URL 的参数
String scheme = data.getScheme(); // "https"
String host = data.getHost(); // "tcg-wallet.ga"
String path = data.getPath(); // "/home"
String query = data.getQuery(); // "search=MP21-EN056&event=search"
// 根据需要解析查询参数
String searchValue = data.getQueryParameter("search");
if (searchValue != null) {
System.out.println("Search Parameter: " + searchValue);
// 可以在 WebView 中执行 JavaScript 来处理这个参数
// webView.evaluateJavascript("javascript:handleSearchParam('" + searchValue + "');", null);
}
}
} else {
// 如果没有深度链接,加载默认 URL
webView.loadUrl("https://tcg-wallet.ga");
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// 如果 Activity 已经是运行状态,并且接收到新的深度链接 Intent,
// 则需要在这里处理,否则 onCreate 不会被再次调用。
setIntent(intent); // 更新当前 Activity 的 Intent
// 重新执行 onCreate 中的 Intent 处理逻辑
// 注意:这里需要根据实际情况决定是否重新加载整个页面或仅更新内容
if (intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_VIEW)) {
Uri data = intent.getData();
if (data != null) {
String fullUrl = data.toString();
System.out.println("Received New Deep Link URL: " + fullUrl);
webView.loadUrl(fullUrl);
}
}
}
}在 onCreate 方法中,我们通过 getIntent() 获取启动 Activity 的 Intent。如果其 action 是 ACTION_VIEW 且包含数据,我们就可以通过 intent.getData() 获取到完整的 URI。然后,您可以将此 URI 直接加载到 WebView 中,或者解析其参数,根据业务逻辑进行进一步处理。如果您的 Activity 启动模式是 singleTop 或 singleTask,并且应用已经在运行,新的深度链接会通过 onNewIntent() 方法传递,因此也需要在此方法中处理。
原始问题中提到了从 WebView 内部通过 JavaScript 调用原生分享功能。这部分与深度链接的接收机制是独立的,但对于理解整个流程仍然有益。
JavaScript 代码(在网页中):
const shareData = {
title: '搜索价格',
text: '搜索卡片:"' + document.getElementById('search').value + '"',
url: document.URL // 当前页面的 URL
};
const btn = document.getElementById('share-url');
if (typeof btn !== 'undefined') {
btn.addEventListener('click', async () => {
try {
// 检查 AndroidShareHandler 接口是否存在,如果存在则调用原生分享
if (typeof (window.AndroidShareHandler) != 'undefined') {
window.AndroidShareHandler.shareSearch(JSON.stringify(shareData));
} else {
// 如果不存在,尝试使用 Web Share API
alert('Shared Working');
await navigator.share(shareData);
}
} catch (err) {
console.error('分享错误:', err);
alert('[错误]: 分享链接出错!');
}
});
}Java 代码(在 Android 应用中):
为了让 JavaScript 能够调用原生代码,需要使用 addJavascriptInterface 方法将一个 Java 对象暴露给 WebView:
public class YourActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.your_webview_id);
webView.getSettings().setJavaScriptEnabled(true);
// 将 Java 对象暴露给 JavaScript
webView.addJavascriptInterface(new JavaScriptShareInterface(this), "AndroidShareHandler");
// ... 其他 WebView 配置和加载 URL
}
// 用于 JavaScript 调用的接口
public class JavaScriptShareInterface {
Context app_context;
JavaScriptShareInterface(Context c) {
app_context = c;
}
@JavascriptInterface
public void shareSearch(String javascript_data) {
try {
JSONObject search = new JSONObject(javascript_data);
String title = search.getString("title");
String text = search.getString("text");
String url = search.getString("url");
String content = title + "\n" + text + "\n\n" + url;
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, content);
// 使用 createChooser 确保用户可以选择分享到哪个应用
app_context.startActivity(Intent.createChooser(sendIntent, "分享链接到..."));
} catch (Exception ex) {
System.err.println("分享数据解析错误: " + ex.getMessage());
}
}
}
}这段代码实现了从 WebView 内部触发原生分享,分享的内容中包含了 URL。当用户在 WhatsApp 中点击这个分享出去的 URL 时,如果您的应用已经正确配置了深度链接,系统就会优先选择在您的应用中打开该链接。
通过在 AndroidManifest.xml 中配置 intent-filter,并结合 Activity 中对 Intent 数据的解析,Android WebView 应用可以有效地实现深度链接功能。这使得应用能够无缝地拦截并处理特定 URL,从而在应用内部提供更一致、更流畅的用户体验。正确实现深度链接是提升应用用户参与度和可发现性的重要一步。
以上就是Android WebView 应用实现深度链接:拦截外部 URL 并内部打开的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号