管理 WebView 对象

Android 提供了一些 API 来帮助您管理在应用中显示 Web 内容的 WebView 对象。

本页介绍了如何使用这些 API 更有效地处理 WebView 对象,从而提高应用的稳定性和安全性。

Version API

从 Android 7.0(API 级别 24)开始,用户可以在 WebView 对象中使用多个不同的软件包来显示 Web 内容。AndroidX.webkit 库包含 getCurrentWebViewPackage() 方法,用于提取与在应用中显示 Web 内容的软件包相关的信息。此方法用于分析仅当您的应用尝试使用特定软件包的 WebView 实现显示 Web 内容时发生的错误。

要使用此方法,请添加以下代码段中显示的逻辑:

Kotlin

val webViewPackageInfo = WebViewCompat.getCurrentWebViewPackage(appContext)
Log.d("MY_APP_TAG", "WebView version: ${webViewPackageInfo.versionName}")

Java

PackageInfo webViewPackageInfo = WebViewCompat.getCurrentWebViewPackage(appContext);
Log.d("MY_APP_TAG", "WebView version: " + webViewPackageInfo.versionName);

Google 安全浏览服务

为了向用户提供更安全的浏览体验,WebView 对象会使用 Google 安全浏览功能验证网址,让您的应用可以在用户尝试访问可能不安全的网站时向用户显示警告。

虽然 EnableSafeBrowsing 的默认值为 true,但在某些情况下,您可能只想根据条件启用或停用安全浏览功能。Android 8.0(API 级别 26)及更高版本支持使用 setSafeBrowsingEnabled() 为单个 WebView 对象开启/关闭“安全浏览”功能。

如果您希望所有 WebView 对象都选择停用安全浏览检查,请将以下 <meta-data> 元素添加到应用的清单文件中:

<manifest>
    <application>
        <meta-data android:name="android.webkit.WebView.EnableSafeBrowsing"
                   android:value="false" />
        ...
    </application>
</manifest>

定义程序化操作

WebView 的实例尝试加载被 Google 归类为已知威胁的页面时,WebView 默认会显示一个插页,以警告用户存在已知威胁。通过该界面,用户可以选择仍然加载网址或返回到上一个安全的页面。

如果您以 Android 8.1(API 级别 27)或更高版本为目标平台,则可以通过编程方式定义应用如何通过以下方式响应已知威胁:

  • 您可以控制应用是否向安全浏览功能报告已知威胁。
  • 您可以让应用在每次遇到被归类为已知威胁的网址时都自动执行特定操作(例如返回安全状态)。

以下代码段展示了如何指示应用的 WebView 实例在遇到已知威胁后始终返回安全状态:

MyWebActivity.java

Kotlin

private lateinit var superSafeWebView: WebView
private var safeBrowsingIsInitialized: Boolean = false

// ...

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    superSafeWebView = WebView(this)
    superSafeWebView.webViewClient = MyWebViewClient()
    safeBrowsingIsInitialized = false

    if (WebViewFeature.isFeatureSupported(WebViewFeature.START_SAFE_BROWSING)) {
        WebViewCompat.startSafeBrowsing(this, ValueCallback<Boolean> { success ->
            safeBrowsingIsInitialized = true
            if (!success) {
                Log.e("MY_APP_TAG", "Unable to initialize Safe Browsing!")
            }
        })
    }
}

Java

private WebView superSafeWebView;
private boolean safeBrowsingIsInitialized;

// ...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    superSafeWebView = new WebView(this);
    superSafeWebView.setWebViewClient(new MyWebViewClient());
    safeBrowsingIsInitialized = false;

    if (WebViewFeature.isFeatureSupported(WebViewFeature.START_SAFE_BROWSING)) {
        WebViewCompat.startSafeBrowsing(this, new ValueCallback<Boolean>() {
            @Override
            public void onReceiveValue(Boolean success) {
                safeBrowsingIsInitialized = true;
                if (!success) {
                    Log.e("MY_APP_TAG", "Unable to initialize Safe Browsing!");
                }
            }
        });
    }
}

MyWebViewClient.java

Kotlin

class MyWebViewClient : WebViewClientCompat() {
    // Automatically go "back to safety" when attempting to load a website that
    // Google identifies as a known threat. An instance of WebView calls this
    // method only after Safe Browsing is initialized, so there's no conditional
    // logic needed here.
    override fun onSafeBrowsingHit(
            view: WebView,
            request: WebResourceRequest,
            threatType: Int,
            callback: SafeBrowsingResponseCompat
    ) {
        // The "true" argument indicates that your app reports incidents like
        // this one to Safe Browsing.
        if (WebViewFeature.isFeatureSupported(WebViewFeature.SAFE_BROWSING_RESPONSE_BACK_TO_SAFETY)) {
            callback.backToSafety(true)
            Toast.makeText(view.context, "Unsafe web page blocked.", Toast.LENGTH_LONG).show()
        }
    }
}

Java

public class MyWebViewClient extends WebViewClientCompat {
    // Automatically go "back to safety" when attempting to load a website that
    // Google identifies as a known threat. An instance of WebView calls this
    // method only after Safe Browsing is initialized, so there's no conditional
    // logic needed here.
    @Override
    public void onSafeBrowsingHit(WebView view, WebResourceRequest request,
            int threatType, SafeBrowsingResponseCompat callback) {
        // The "true" argument indicates that your app reports incidents like
        // this one to Safe Browsing.
        if (WebViewFeature.isFeatureSupported(WebViewFeature.SAFE_BROWSING_RESPONSE_BACK_TO_SAFETY)) {
            callback.backToSafety(true);
            Toast.makeText(view.getContext(), "Unsafe web page blocked.",
                    Toast.LENGTH_LONG).show();
        }
    }
}

HTML5 Geolocation API

对于以 Android 6.0(API 级别 23)及更高版本为目标平台的应用,仅 HTTPS 等安全的起点支持 Geolocation API。如果不调用相应的 onGeolocationPermissionsShowPrompt() 方法,非安全起点向 Geolocation API 发出的任何请求都会被自动拒绝。

停用指标收集功能

在征得用户同意后,WebView 能够将匿名诊断数据上传到 Google。实例化 WebView 的每个应用都会按应用收集数据。您可以通过在清单的 <application> 元素中创建以下标记来停用此功能:

<manifest>
    <application>
    ...
    <meta-data android:name="android.webkit.WebView.MetricsOptOut"
               android:value="true" />
    </application>
</manifest>

只有在用户同意并且应用未拒绝的情况下,才能从应用上传数据。如需详细了解如何选择停用诊断数据报告,请参阅 WebView 报告中的用户隐私

Termination Handling

Terification Handling API 可以处理 WebView 对象的渲染器进程消失(可能是因为系统终止渲染器以回收必要的内存,或者渲染器进程崩溃)的情况。通过使用此 API,即使渲染程序进程消失了,您也可以继续执行应用。

如果渲染程序在加载特定网页时崩溃,尝试再次加载同一网页可能会导致新的 WebView 对象表现出相同的渲染崩溃行为。

以下代码段说明了如何在 Activity 中使用此 API:

Kotlin

    
inner class MyRendererTrackingWebViewClient : WebViewClient() {
    private var mWebView: WebView? = null

    override fun onRenderProcessGone(view: WebView, detail: RenderProcessGoneDetail): Boolean {
        if (!detail.didCrash()) {
            // Renderer is killed because the system ran out of memory. The app
            // can recover gracefully by creating a new WebView instance in the
            // foreground.
            Log.e("MY_APP_TAG", ("System killed the WebView rendering process " +
                "to reclaim memory. Recreating..."))

            mWebView?.also { webView ->
                val webViewContainer: ViewGroup = findViewById(R.id.my_web_view_container)
                webViewContainer.removeView(webView)
                webView.destroy()
                mWebView = null
            }

            // By this point, the instance variable "mWebView" is guaranteed to
            // be null, so it's safe to reinitialize it.

            return true // The app continues executing.
        }

        // Renderer crashes because of an internal error, such as a memory
        // access violation.
        Log.e("MY_APP_TAG", "The WebView rendering process crashed!")

        // In this example, the app itself crashes after detecting that the
        // renderer crashed. If you handle the crash more gracefully and let
        // your app continue executing, you must destroy the current WebView
        // instance, specify logic for how the app continues executing, and
        // return "true" instead.
        return false
    }
}

Java

public class MyRendererTrackingWebViewClient extends WebViewClient {
    private WebView mWebView;

    @Override
    public boolean onRenderProcessGone(WebView view,
            RenderProcessGoneDetail detail) {
        if (!detail.didCrash()) {
            // Renderer is killed because the system ran out of memory. The app
            // can recover gracefully by creating a new WebView instance in the
            // foreground.
            Log.e("MY_APP_TAG", "System killed the WebView rendering process " +
                    "to reclaim memory. Recreating...");

            if (mWebView != null) {
                ViewGroup webViewContainer =
                        (ViewGroup) findViewById(R.id.my_web_view_container);
                webViewContainer.removeView(mWebView);
                mWebView.destroy();
                mWebView = null;
            }

            // By this point, the instance variable "mWebView" is guaranteed to
            // be null, so it's safe to reinitialize it.

            return true; // The app continues executing.
        }

        // Renderer crashes because of an internal error, such as a memory
        // access violation.
        Log.e("MY_APP_TAG", "The WebView rendering process crashed!");

        // In this example, the app itself crashes after detecting that the
        // renderer crashed. If you handle the crash more gracefully and let
        // your app continue executing, you must destroy the current WebView
        // instance, specify logic for how the app continues executing, and
        // return "true" instead.
        return false;
    }
}

Renderer Importance API

WebView 对象在多进程模式下运行时,您可以灵活地控制应用处理内存不足情况的方式。您可以使用 Android 8.0 中引入的 Renderer Importance API 来为分配给特定 WebView 对象的渲染器设置优先级政策。具体而言,当显示应用的 WebView 对象的渲染器已终止时,您可能希望应用的主要部分继续执行。例如,如果您预计长时间不显示 WebView 对象,以便系统可以回收渲染程序正在使用的内存,则可以这样做。

以下代码段展示了如何为与应用的 WebView 对象关联的渲染程序进程分配优先级:

Kotlin

val myWebView: WebView = ...
myWebView.setRendererPriorityPolicy(RENDERER_PRIORITY_BOUND, true)

Java

WebView myWebView;
myWebView.setRendererPriorityPolicy(RENDERER_PRIORITY_BOUND, true);

在此特定代码段中,渲染程序的优先级与应用的默认优先级相同或绑定到应用的默认优先级。当关联的 WebView 对象不再可见时,true 参数会将渲染器的优先级降低为 RENDERER_PRIORITY_WAIVED。换句话说,true 参数表示您的应用并不关心系统是否会让渲染器进程保持活跃状态。事实上,这个较低的优先级可能会导致渲染器进程在内存不足的情况下被终止。

如需详细了解系统如何处理内存不足的情况,请参阅进程和应用生命周期