【问题标题】:Android Webview slows down Nexus devices when javascript is enabled启用 javascript 时,Android Webview 会降低 Nexus 设备的速度
【发布时间】:2013-07-02 06:47:37
【问题描述】:

我在 Nexus 4、Galaxy Nexus 和 Nexus 7 等 Nexus 设备上遇到了这个奇怪的问题。 我有一个类似浏览器的应用程序,其中包含多个选项卡,每个选项卡都有一个 Web 视图。

如果我在多个选项卡(2 个或更多)中打开像 Google.com 之类的网站,则网页浏览量不会被点击。网站加载也很慢。 以下是我的 webview 设置

    private void doWebSettings(WebView webview) {
        webview.setScrollbarFadingEnabled(true);
        webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        // enable smooth transition for better performance during panning or
        // zooming
        if (currentSdk >= OS_3_0) {
            webSettings.setEnableSmoothTransition(true);
        }
        // For Zooming out completely
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportZoom(true);

        webSettings.setBuiltInZoomControls(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAllowFileAccess(true);
        try {
            webSettings.setAllowContentAccess(false);
        } catch (NoSuchMethodError e) {
            e.printStackTrace();
        }
        String databasePath = this.getApplicationContext()
                .getDir("database", Context.MODE_PRIVATE).getPath();
        webSettings.setDatabasePath(databasePath);
        webSettings.setGeolocationDatabasePath(this.getApplicationContext()
                .getDir("geolocation", 0).getPath());


        webSettings.setRenderPriority(RenderPriority.HIGH);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setDatabaseEnabled(true);
        // Dont focus on any webview item by default
        webSettings.setNeedInitialFocus(false);
        if (!isTablet) {
            webSettings
                    .setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
        }
}

如果我将启用的 Javascript 设置为“False”,则所有 webview 都可以顺利运行。在所有 nexus 设备上都明显观察到此问题,有时 web 视图不会在几分钟内点击。此行为使应用程序无法使用。我假设一些 Javascript 正在消耗 Webview 的 UI 线程,这就是它卡住的原因。我在 Eclipse 中收到关于 setJavascriptEnabled(true) 的警告;因为“使用 setJavaScriptEnabled 可能会在您的应用程序中引入 XSS 漏洞,请仔细查看。”我在互联网上搜索了这个警告,但没有找到任何相关的答案。我还检查了一些与 Jellybean 上的 Webview 性能问题有关的帖子。我在这里错过了什么?

【问题讨论】:

    标签: javascript android webview nexus-7 nexus-4


    【解决方案1】:

    找到罪魁祸首。

    webview.freeMemory() 
    

    我正在调用此函数以在每次选项卡更改时释放内存。评论此代码解决了这个问题。我不确定这背后的原因,但 Webview 会长时间进入某种处理状态并且不会点击 UI。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-06
      • 2011-02-16
      • 2012-10-24
      • 2011-05-27
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多