如果您的应用使用 WebView
显示 Web 内容,您可能需要考虑优化点击行为,原因如下:
WebView
不支持自定义网址架构,如果点击目标是单独的应用,则可以在广告中返回此类网址。例如,Google Play 点击后到达网址可能使用market://
。
WebView
不支持 Google 登录和 Facebook 登录。
本指南提供了建议步骤,以便在保留网页视图内容的同时优化移动网页视图中的点击行为。
前提条件
- 完成设置网页视图指南。
实现
请按照以下步骤优化 WebView
实例中的点击行为:
替换
WebViewClient
上的shouldOverrideUrlLoading()
。当网址即将在当前WebView
中加载时,系统会调用此方法。确定是否替换点击网址的行为。
代码示例会检查当前网域是否与目标网域不同。这只是一种方法,因为您使用的条件可能会有所不同。
决定是在外部浏览器、Android 自定义标签页还是现有网页视图中打开网址。本指南介绍了如何通过启动 Android Custom Tabs 来打开网址,从而离开相应网站。
代码示例
首先,将 androidx.browser
依赖项添加到模块级 build.gradle
文件(通常为 app/build.gradle
)。自定义标签页需要满足以下要求:
dependencies {
implementation 'androidx.browser:browser:1.5.0'
}
以下代码段展示了如何实现 shouldOverrideUrlLoading()
:
Java
public class MainActivity extends AppCompatActivity {
private WebView webView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ... Register the WebView.
webView = new WebView(this);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(
new WebViewClient() {
// 1. Implement the web view click handler.
@Override
public boolean shouldOverrideUrlLoading(
WebView view,
WebResourceRequest request) {
// 2. Determine whether to override the behavior of the URL.
// If the target URL has no host, return early.
if (request.getUrl().getHost() == null) {
return false;
}
// Handle custom URL schemes such as market:// by attempting to
// launch the corresponding application in a new intent.
if (!request.getUrl().getScheme().equals("http")
&& !request.getUrl().getScheme().equals("https")) {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
// If the URL cannot be opened, return early.
try {
MainActivity.this.startActivity(intent);
} catch (ActivityNotFoundException exception) {
Log.d("TAG", "Failed to load URL with scheme:" + request.getUrl().getScheme());
}
return true;
}
String currentDomain;
// If the current URL's host cannot be found, return early.
try {
currentDomain = new URI(view.getUrl()).toURL().getHost();
} catch (URISyntaxException | MalformedURLException exception) {
// Malformed URL.
return false;
}
String targetDomain = request.getUrl().getHost();
// If the current domain equals the target domain, the
// assumption is the user is not navigating away from
// the site. Reload the URL within the existing web view.
if (currentDomain.equals(targetDomain)) {
return false;
}
// 3. User is navigating away from the site, open the URL in
// Custom Tabs to preserve the state of the web view.
CustomTabsIntent intent = new CustomTabsIntent.Builder().build();
intent.launchUrl(MainActivity.this, request.getUrl());
return true;
}
});
}
}
Kotlin
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// ... Register the WebView.
webView.webViewClient = object : WebViewClient() {
// 1. Implement the web view click handler.
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
// 2. Determine whether to override the behavior of the URL.
// If the target URL has no host, return early.
request?.url?.host?.let { targetDomain ->
val currentDomain = URI(view?.url).toURL().host
// Handle custom URL schemes such as market:// by attempting to
// launch the corresponding application in a new intent.
if (!request.url.scheme.equals("http") &&
!request.url.scheme.equals("https")) {
val intent = Intent(Intent.ACTION_VIEW, request.url)
// If the URL cannot be opened, return early.
try {
this@MainActivity.startActivity(intent)
} catch (exception: ActivityNotFoundException) {
Log.d("TAG", "Failed to load URL with scheme: ${request.url.scheme}")
}
return true
}
// If the current domain equals the target domain, the
// assumption is the user is not navigating away from
// the site. Reload the URL within the existing web view.
if (currentDomain.equals(targetDomain)) {
return false
}
// 3. User is navigating away from the site, open the URL in
// Custom Tabs to preserve the state of the web view.
val customTabsIntent = CustomTabsIntent.Builder().build()
customTabsIntent.launchUrl(this@MainActivity, request.url)
return true
}
return false
}
}
}
}
测试页面导航
如需测试网页导航更改,请加载
https://webview-api-for-ads-test.glitch.me#click-behavior-tests
到您的网页视图中。点击每种不同类型的链接,查看它们在应用中的行为方式。
请检查以下各项:
- 每个链接都会打开预期的网址。
- 返回应用时,测试页面的计数器不会重置为零,以验证页面状态是否已保留。