缓存android webview以缩小js和css调用

时间:2016-09-26 12:13:50

标签: javascript android css caching webview

在我的网页中,我启用了minify js和css调用。我使用以下

为Android webview启用了缓存以获得更快的性能
 mWebView.getSettings().setJavaScriptEnabled(true);
    //mWebView.addJavascriptInterface();
    mWebView.addJavascriptInterface(new MyJavascriptInterface(getActivity(), StartFragment.this.getActivity()), "MyJavascriptInterface");

    String UserAgent = "Wifog android-" + versionCodestr + "/" + versionName;
    mWebView.getSettings().setUserAgentString(mWebView.getSettings().getUserAgentString() + " " + UserAgent);
    //in this call i set the webview client and the calls to process
    setWebViewClient();
    mWebView.setWebChromeClient(new WebChromeClient()
    {
        @Override
        public void onReachedMaxAppCacheSize(long requiredStorage, long quota, WebStorage.QuotaUpdater quotaUpdater) {
            quotaUpdater.updateQuota(requiredStorage*2);
        }
    });

    mWebView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
    String appCachePath = getContext().getApplicationContext().getCacheDir().getAbsolutePath();
    System.out.println("App- cache paht webview:  "+appCachePath);
    mWebView.getSettings().setAppCachePath(appCachePath);
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setAppCacheEnabled(true);
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    mWebView.getSettings().setPluginState(PluginState.ON);
    mWebView.getSettings().setDatabaseEnabled(true);


    if (Build.VERSION.SDK_INT < VERSION_CODES.KITKAT) {
        mWebView.getSettings().setDatabasePath("/data/data/" + mWebView.getContext().getPackageName() + "/databases/");
    }

但仍然没有缓存页面的minify调用并且一次又一次地命中服务器。我怎么能在android中处理这个?

1 个答案:

答案 0 :(得分:1)

试一试:

mWebView.clearCache(false);

如果它仍然不起作用

Method m1 = WebSettings.class.getMethod("setAppCacheEnabled", new Class[]{Boolean.TYPE});
                m6.invoke(ws, Boolean.TRUE);
ws中的

是您的WebSettings对象