Java 与浏览器相比,Android Web View速度非常慢且响应速度不快,为什么?

Java 与浏览器相比,Android Web View速度非常慢且响应速度不快,为什么?,java,android,android-webview,Java,Android,Android Webview,我在stack overflow中尝试了类似问题的答案,但没有成功,所以请告诉我我的代码有什么问题,我需要添加什么 在我的android代码中,我包含了以下内容: webView = (WebView) findViewById(R.id.webviews); webView.setWebViewClient(new MyBrowser()); webView.getSettings().setRenderPriority(WebSettings.

我在stack overflow中尝试了类似问题的答案,但没有成功,所以请告诉我我的代码有什么问题,我需要添加什么

在我的android代码中,我包含了以下内容:

        webView = (WebView) findViewById(R.id.webviews);
        webView.setWebViewClient(new MyBrowser());
        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.getSettings().setDomStorageEnabled(true);
然后呢,

connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                connect.setVisibility(View.INVISIBLE);
                editText.setVisibility(View.INVISIBLE);
                uunnamed.setVisibility(View.VISIBLE);
                tv.setVisibility(View.INVISIBLE);

                webView.setVisibility(View.VISIBLE);

                webView.loadUrl("http://192.168.1.8:8081");


            }
        });
和我的webclient类:

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

问题是什么?为什么它在web视图中速度慢,而在chrome浏览器中速度快?

我不是基于web技术的应用程序的专家,但我的团队是

根据她的代码()尝试以下设置:

webView.setInitialScale(0);
webView.setVerticalScrollBarEnabled(false);
// Enable JavaScript
final WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

// Enable AppCache
// Fix for CB-2282
settings.setAppCacheMaxSize(5 * 1048576);
settings.setAppCachePath(databasePath);
settings.setAppCacheEnabled(true);
他们做的更多,但这可能是个骗局

initwebview的完整方法

@SuppressLint({"NewApi", "SetJavaScriptEnabled"})
@SuppressWarnings("deprecation")
private void initWebViewSettings() {
    webView.setInitialScale(0);
    webView.setVerticalScrollBarEnabled(false);
    // Enable JavaScript
    final WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

    // Set the nav dump for HTC 2.x devices (disabling for ICS, deprecated entirely for Jellybean 4.2)
    try {
        Method gingerbread_getMethod =  WebSettings.class.getMethod("setNavDump", new Class[] { boolean.class });

        String manufacturer = android.os.Build.MANUFACTURER;
        Log.d(TAG, "CordovaWebView is running on device made by: " + manufacturer);
        if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB &&
                android.os.Build.MANUFACTURER.contains("HTC"))
        {
            gingerbread_getMethod.invoke(settings, true);
        }
    } catch (NoSuchMethodException e) {
        Log.d(TAG, "We are on a modern version of Android, we will deprecate HTC 2.3 devices in 2.8");
    } catch (IllegalArgumentException e) {
        Log.d(TAG, "Doing the NavDump failed with bad arguments");
    } catch (IllegalAccessException e) {
        Log.d(TAG, "This should never happen: IllegalAccessException means this isn't Android anymore");
    } catch (InvocationTargetException e) {
        Log.d(TAG, "This should never happen: InvocationTargetException means this isn't Android anymore.");
    }

    //We don't save any form data in the application
    settings.setSaveFormData(false);
    settings.setSavePassword(false);

    // Jellybean rightfully tried to lock this down. Too bad they didn't give us a whitelist
    // while we do this
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        settings.setAllowUniversalAccessFromFileURLs(true);
    }
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
        settings.setMediaPlaybackRequiresUserGesture(false);
    }
    // Enable database
    // We keep this disabled because we use or shim to get around DOM_EXCEPTION_ERROR_16
    String databasePath = webView.getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    settings.setDatabaseEnabled(true);
    settings.setDatabasePath(databasePath);


    //Determine whether we're in debug or release mode, and turn on Debugging!
    ApplicationInfo appInfo = webView.getContext().getApplicationContext().getApplicationInfo();
    if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0 &&
        android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        enableRemoteDebugging();
    }

    settings.setGeolocationDatabasePath(databasePath);

    // Enable DOM storage
    settings.setDomStorageEnabled(true);

    // Enable built-in geolocation
    settings.setGeolocationEnabled(true);

    // Enable AppCache
    // Fix for CB-2282
    settings.setAppCacheMaxSize(5 * 1048576);
    settings.setAppCachePath(databasePath);
    settings.setAppCacheEnabled(true);

    // Fix for CB-1405
    // Google issue 4641
    String defaultUserAgent = settings.getUserAgentString();

    // Fix for CB-3360
    String overrideUserAgent = preferences.getString("OverrideUserAgent", null);
    if (overrideUserAgent != null) {
        settings.setUserAgentString(overrideUserAgent);
    } else {
        String appendUserAgent = preferences.getString("AppendUserAgent", null);
        if (appendUserAgent != null) {
            settings.setUserAgentString(defaultUserAgent + " " + appendUserAgent);
        }
    }
    // End CB-3360

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                settings.getUserAgentString();
            }
        };
        webView.getContext().registerReceiver(this.receiver, intentFilter);
    }
    // end CB-1405
}
@SuppressLint({“NewApi”,“SetJavaScriptEnabled”})
@抑制警告(“弃用”)
私有void initWebViewSettings(){
webView.setInitialScale(0);
webView.setVerticalScrollBarEnabled(false);
//启用JavaScript
最终WebSettings=webView.getSettings();
settings.setJavaScriptEnabled(true);
setJavaScriptCanOpenWindowsAutomatically(true);
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
//为HTC 2.x设备设置导航转储(禁用ICS,完全不推荐用于Jellybean 4.2)
试一试{
方法gingerbread_getMethod=WebSettings.class.getMethod(“setNavDump”,新类[]{boolean.class});
字符串制造商=android.os.Build.manufacturer;
Log.d(标签“CordovaWebView正在由以下制造商制造的设备上运行”);
if(android.os.Build.VERSION.SDK_INT=android.os.Build.VERSION\u code.JELLY\u BEAN){
settings.setAllowUniversalAccessFromFileURLs(true);
}
if(android.os.Build.VERSION.SDK\u INT>=android.os.Build.VERSION\u code.JELLY\u BEAN\u MR1){
settings.setMediaPlaybackRequireservesture(false);
}
//启用数据库
//我们之所以禁用此选项,是因为我们使用或填充来绕过DOM\u异常\u错误\u 16
字符串databasePath=webView.getContext().getApplicationContext().getDir(“数据库”,Context.MODE_PRIVATE).getPath();
settings.setDatabaseEnabled(true);
setDatabasePath(databasePath);
//确定我们是处于调试模式还是发布模式,然后打开调试!
ApplicationInfo appInfo=webView.getContext().getApplicationContext().getApplicationInfo();
如果((appInfo.flags和ApplicationInfo.FLAG_debugable)!=0&&
android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_code.KITKAT){
启用远程调试();
}
setGeolocationDatabasePath(databasePath);
//启用DOM存储
settings.setDomStorageEnabled(true);
//启用内置地理定位
设置。setGeolocationEnabled(真);
//启用AppCache
//CB-2282的修复
settings.setAppCacheMaxSize(5*1048576);
setAppCachePath(数据库路径);
settings.setAppCacheEnabled(true);
//CB-1405的修复
//谷歌第4641期
字符串defaultUserAgent=settings.getUserAgentString();
//CB-3360的固定装置
String overrideUserAgent=preferences.getString(“overrideUserAgent”,null);
if(overrideUserAgent!=null){
setUserAgentString(overrideUserAgent);
}否则{
String appendUserAgent=preferences.getString(“appendUserAgent”,null);
if(appendUserAgent!=null){
settings.setUserAgentString(defaultUserAgent+“”+appendUserAgent);
}
}
//完CB-3360
IntentFilter IntentFilter=新的IntentFilter();
intentFilter.addAction(Intent.ACTION\u配置\u已更改);
if(this.receiver==null){
this.receiver=new BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
settings.getUserAgentString();
}
};
webView.getContext().registerReceiver(this.receiver,intentFilter);
}
//完CB-1405
}

我不是基于web技术的应用程序的专家,但我的团队是

根据她的代码()尝试以下设置:

webView.setInitialScale(0);
webView.setVerticalScrollBarEnabled(false);
// Enable JavaScript
final WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

// Enable AppCache
// Fix for CB-2282
settings.setAppCacheMaxSize(5 * 1048576);
settings.setAppCachePath(databasePath);
settings.setAppCacheEnabled(true);
他们做的更多,但这可能是个骗局

initwebview的完整方法

@SuppressLint({"NewApi", "SetJavaScriptEnabled"})
@SuppressWarnings("deprecation")
private void initWebViewSettings() {
    webView.setInitialScale(0);
    webView.setVerticalScrollBarEnabled(false);
    // Enable JavaScript
    final WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

    // Set the nav dump for HTC 2.x devices (disabling for ICS, deprecated entirely for Jellybean 4.2)
    try {
        Method gingerbread_getMethod =  WebSettings.class.getMethod("setNavDump", new Class[] { boolean.class });

        String manufacturer = android.os.Build.MANUFACTURER;
        Log.d(TAG, "CordovaWebView is running on device made by: " + manufacturer);
        if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB &&
                android.os.Build.MANUFACTURER.contains("HTC"))
        {
            gingerbread_getMethod.invoke(settings, true);
        }
    } catch (NoSuchMethodException e) {
        Log.d(TAG, "We are on a modern version of Android, we will deprecate HTC 2.3 devices in 2.8");
    } catch (IllegalArgumentException e) {
        Log.d(TAG, "Doing the NavDump failed with bad arguments");
    } catch (IllegalAccessException e) {
        Log.d(TAG, "This should never happen: IllegalAccessException means this isn't Android anymore");
    } catch (InvocationTargetException e) {
        Log.d(TAG, "This should never happen: InvocationTargetException means this isn't Android anymore.");
    }

    //We don't save any form data in the application
    settings.setSaveFormData(false);
    settings.setSavePassword(false);

    // Jellybean rightfully tried to lock this down. Too bad they didn't give us a whitelist
    // while we do this
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        settings.setAllowUniversalAccessFromFileURLs(true);
    }
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
        settings.setMediaPlaybackRequiresUserGesture(false);
    }
    // Enable database
    // We keep this disabled because we use or shim to get around DOM_EXCEPTION_ERROR_16
    String databasePath = webView.getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    settings.setDatabaseEnabled(true);
    settings.setDatabasePath(databasePath);


    //Determine whether we're in debug or release mode, and turn on Debugging!
    ApplicationInfo appInfo = webView.getContext().getApplicationContext().getApplicationInfo();
    if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0 &&
        android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        enableRemoteDebugging();
    }

    settings.setGeolocationDatabasePath(databasePath);

    // Enable DOM storage
    settings.setDomStorageEnabled(true);

    // Enable built-in geolocation
    settings.setGeolocationEnabled(true);

    // Enable AppCache
    // Fix for CB-2282
    settings.setAppCacheMaxSize(5 * 1048576);
    settings.setAppCachePath(databasePath);
    settings.setAppCacheEnabled(true);

    // Fix for CB-1405
    // Google issue 4641
    String defaultUserAgent = settings.getUserAgentString();

    // Fix for CB-3360
    String overrideUserAgent = preferences.getString("OverrideUserAgent", null);
    if (overrideUserAgent != null) {
        settings.setUserAgentString(overrideUserAgent);
    } else {
        String appendUserAgent = preferences.getString("AppendUserAgent", null);
        if (appendUserAgent != null) {
            settings.setUserAgentString(defaultUserAgent + " " + appendUserAgent);
        }
    }
    // End CB-3360

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                settings.getUserAgentString();
            }
        };
        webView.getContext().registerReceiver(this.receiver, intentFilter);
    }
    // end CB-1405
}
@SuppressLint({“NewApi”,“SetJavaScriptEnabled”})
@抑制警告(“弃用”)
私有void initWebViewSettings(){
webView.setInitialScale(0);
webView.setVerticalScrollBarEnabled(false);
//启用JavaScript
最终WebSettings=webView.getSettings();
settings.setJavaScriptEnabled(true);
setJavaScriptCanOpenWindowsAutomatically(true);
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
//为HTC 2.x设备设置导航转储(禁用ICS,完全不推荐用于Jellybean 4.2)
试一试{
方法gingerbread_getMethod=WebSettings.class.getMethod(“setNavDump”,新类[]{boolean.class});
字符串制造商=android.os.Build.manufacturer;
Log.d(标签“CordovaWebView正在由以下制造商制造的设备上运行”);
如果(android.os.Build.VERSION.SDK_INT