【问题标题】:is there any way to make a webview running android 4.0 perform at an acceptable level?有什么方法可以让运行 android 4.0 的 webview 在可接受的水平上运行?
【发布时间】:2012-03-28 08:09:58
【问题描述】:

在模拟器和我的galaxy nexus 设备上,这个简单的演示应用程序需要整整1000 毫秒或更长时间来选择或取消选择一个复选框。我想用 javascript 编写我的大部分应用程序,这样我就可以跨 ios/android/web 重用代码,但这是一个交易破坏者。

这是我的代码:

(活动)

package com.mycompanyname;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.mycompanyname.R;

public class JavascriptListViewTestActivity extends Activity {
    private JavascriptListViewTestActivity parent = this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);            

        WebChromeClient chrome = new WebChromeClient() {
            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                parent.showDialog(consoleMessage.message() + "\n" + consoleMessage.lineNumber());
                return true;
            }
        };

        WebViewClient client = new WebViewClient() {

        };

        WebView webView = (WebView)findViewById(R.id.webView1);
        webView.setWebChromeClient(chrome);
        webView.setWebViewClient(client);

        webView.getSettings().setJavaScriptEnabled(false);
        webView.getSettings().setAllowFileAccess(false);
        webView.getSettings().setAppCacheEnabled(false);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setDatabaseEnabled(false);
        webView.getSettings().setDomStorageEnabled(false);
        webView.getSettings().setGeolocationEnabled(false);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
        webView.getSettings().setLightTouchEnabled(false);
        webView.getSettings().setLoadWithOverviewMode(false);
        webView.getSettings().setNavDump(false);
        webView.getSettings().setNeedInitialFocus(false);
        webView.getSettings().setPluginsEnabled(false);
        webView.getSettings().setRenderPriority(RenderPriority.HIGH);
        webView.getSettings().setSaveFormData(false);
        webView.getSettings().setSavePassword(false);
        webView.getSettings().setSupportMultipleWindows(false);
        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setUseDoubleTree(false);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

        String html = readText(R.raw.listview);

        webView.loadData(html, "text/html", "UTF-8");

    }

    private void showDialog(String message)
    {
        AlertDialog alert = new AlertDialog.Builder(parent).create();
        alert.setMessage(message);
        alert.show();
    }

    private String readText(int resourceId) 
    {
        InputStream is = this.getResources().openRawResource(resourceId);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String readLine = null;
        StringBuffer outputBuffer = new StringBuffer();

        try 
        {
            while ((readLine = br.readLine()) != null) 
            {
                outputBuffer.append(readLine);
            }
            return outputBuffer.toString();
        } 
        catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        finally 
        {
            // TODO: might throw - use IOUtils.close()
            try 
            {
                is.close();
                br.close();
            } 
            catch (IOException ex) 
            {
                showDialog(ex.toString());
            }
        }
    }
}

(xml布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

(webview中加载的html)

<html>
<body>

<input type="checkbox" />

</body>
</html>

【问题讨论】:

  • 我注意到我在使用标准浏览器或 chrome for android 的常规网络浏览器中获得了同样糟糕的性能。转到 gmail.com,并在注销时选择或取消选择复选框以切换“记住我”仍然需要一秒钟!这太疯狂了……

标签: android performance dom webview


【解决方案1】:

我知道这已经有一年多了,但你可以尝试实现 fastclick.js:https://github.com/ftlabs/fastclick

我已经在很多项目中成功使用了它。正如页面解释的那样:

...移动浏览器将等待大约 300 毫秒 你点击按钮来触发点击事件。这样做的原因是 浏览器正在等待查看您是否真的在执行 双击。

webview 中的交互也是如此(据我所知)。

【讨论】:

    猜你喜欢
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2019-08-03
    • 2012-01-10
    • 1970-01-01
    相关资源
    最近更新 更多