WebViewローカルキャッシュ

8188 ワード

のaSampleCacheで作ったキャッシュ.しかし、webViewページがどのようにキャッシュされるかは以前は知らなかったが、ネットで調べると、意外にもwebviewがキャッシュを持っていて、勉強が浅いので、簡単なdemoを作った.
public class MainActivity extends Activity {
    private WebView webView;
    private String url =" http://www.3dmgame.com/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        webView = (WebView) findViewById(R.id.activity_webview);
        webView.requestFocus();
        webView.setHorizontalScrollBarEnabled(false);
        webView.setVerticalScrollBarEnabled(false);
        initWebView();
    }
    @SuppressWarnings("deprecation")
    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        webView.getSettings().setJavaScriptEnabled(true);
        //        
        if (StateTest.isNetworkAvailable(MainActivity.this)) {
            webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        } else {
            webView.getSettings().setCacheMode(
                    WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }
        // webView.getSettings().setBlockNetworkImage(true);//               
        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        //      
        webView.getSettings().setSupportMultipleWindows(true);
        //    DOM storage API   
        webView.getSettings().setDomStorageEnabled(true);
        //    Application Caches   
        webView.getSettings().setAppCacheEnabled(true);
        onLoad();
    }
    @SuppressWarnings("deprecation")
    @SuppressLint("SetJavaScriptEnabled")
    public void onLoad() {

        try {
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onLoadResource(WebView view, String url) {
                    Log.i("tag", "onLoadResource url=" + url); //     
                    super.onLoadResource(view, url);
                }
                @Override
                public boolean shouldOverrideUrlLoading(WebView webview,
                                                        String url) {
                    Log.i("tag", "intercept url=" + url);
                    //                       webview   ,        
                    webview.loadUrl(url);
                    return true;
                }
                @Override
                public void onPageFinished(WebView view, String url) {
                    String title = view.getTitle(); //       
                    Log.e("tag", "onPageFinished WebView title=" + title);
                }

                @Override
                public void onReceivedError(WebView view, int errorCode,
                                            String description, String failingUrl) {
                    Toast.makeText(getApplicationContext(), "    ",
                            Toast.LENGTH_LONG).show();
                }
            });
            webView.loadUrl(url);
        } catch (Exception e) {
            return;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack(); // goBack()    WebView     
            return true;
        } else {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    /***
     *   WebView      
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.removeAllViews();
        webView.destroy();
    }
}

webView本地缓存_第1张图片 webView本地缓存_第2张图片という簡単な機能が終わりました.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>