Android 4.4中不存在WebView缓存

时间:2015-11-28 13:47:52

标签: java android caching webview

WebView似乎没有在Android 4.4中创建或使用缓存,因此它不必要地消耗大量带宽。

在4.2.2中运行这个简单的测试应用程序会显示正确的结果:

========================================================
Dumping Webview cache at /data/data/com.example.user.myapplication/cache/webviewCacheChromium
0th file: f_000006     33026
1th file: f_000005     40828
2th file: f_000004     37666
3th file: f_000003     76566
4th file: f_000002     66429
5th file: f_000001     20327
6th file: data_3     4202496
7th file: data_2     1056768
8th file: data_1     270336
9th file: data_0     45056
10th file: index     262512
Webview cache contains 11 files and takes up 5 Mb.
========================================================

但是在4.4.4上,根本没有WebView缓存:

========================================================
Neither webviewCache nor webviewCacheChromium exists! Dumping App Cache instead.
Dumping App cache at /data/data/com.example.user.myapplication/cache
0th file: com.android.opengl.shaders_cache     10947
App cache contains 1 files and takes up 0 Mb.
========================================================

这是我的源代码:

MainActivity.java:

package com.example.user.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private WebView webview;
    private static final String TAG = "myapp";
    private static final String BAR = "========================================================";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webview = (WebView) findViewById(R.id.webview);

        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

        webview.getSettings().setAppCacheEnabled(true);
        webview.getSettings().setAppCacheMaxSize(100 * 1000 * 1000);

        webview.setWebViewClient(new WebViewClient() {
        });
        webview.loadUrl("https://www.reddit.com/");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            String msg = dumpWebviewCache();
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private String dumpWebviewCache() {
        Log.d(TAG, BAR);
        File appCache = getCacheDir();
        File webviewCache = new File(appCache, "webviewCache");
        File webviewCacheChromium = new File(appCache, "webviewCacheChromium");

        if (!webviewCache.exists() && !webviewCacheChromium.exists()) {
            Log.e(TAG, "Neither webviewCache nor webviewCacheChromium exists! Dumping App Cache instead.");
            return dumpDir("App cache", appCache);
        }
        if (!webviewCache.exists())
            webviewCache = webviewCacheChromium;

        return dumpDir("Webview cache", webviewCache);
    }

    private String dumpDir(String dirName, File dirFile) {
        Log.d(TAG, "Dumping " + dirName + " at " + dirFile.getAbsolutePath());
        int i = 0, totalSize = 0;
        for (File file : dirFile.listFiles()) {
            Log.d(TAG, String.format("%1$3s", i++) + "th file: " + file.getName() + "     " + file.length());
            totalSize += file.length();
        }
        String msg = dirName + " contains " + dirFile.listFiles().length + " files and takes up " + totalSize / (1024 * 1024) + " Mb.";
        Log.d(TAG, msg);
        Log.d(TAG, BAR);
        return msg;
    }
}

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.myapplication.MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

的strings.xml:

<resources>
    <string name="app_name">My Application</string>
</resources>

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.myapplication" >

    <uses-permission android:name="android.permission.INTERNET" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label=""
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

menu_main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="Dump WebView cache"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

1 个答案:

答案 0 :(得分:0)

是什么让您认为必须调用缓存目录webviewCachewebviewCacheChromium?这是一个实现细节,您不应该依赖于此。在更新的版本中,缓存目录在Java包名称之后调用 - org.chromium.android_webview