簡體   English   中英

webview android 中的 web 頁面的緩存集

[英]Cache set of web pages in a webview android

我正在開發一個 web 應用程序,我還將在 Android 中使用 WebView 應用程序,我想緩存一些頁面集以為我的用戶提供離線訪問。 現在只有那些我訪問過的頁面被緩存,但我想預先緩存一組頁面,以便用戶可以離線工作。

我現在使用的代碼:

private WebView webView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.webView = (WebView)
            findViewById(R.id.webview);

    webView.setWebViewClient(new WebViewClient());
    WebSettings webSettings = webView.getSettings();

    String appCachePath;
    appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
    // Enable responsive layout
    webSettings.setUseWideViewPort(true);
    // Zoom out if the content width is greater than the width of the viewport
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setAppCachePath(appCachePath);
    webSettings.setAppCacheEnabled(true);
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
    if(cm.getActiveNetworkInfo() == null || !cm.getActiveNetworkInfo().isConnected())
    {
        Log.i("B","INSIDE NO NETWORK");
        appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        webSettings.setAppCachePath(appCachePath);

        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
        webView.loadUrl("https://www.example.com");
    }
    else
    {
        Log.i("A","INSIDE YES NETWORK");
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        webView.loadUrl("https://www.example.com");
    }
 }
public class XXXWebViewClient extends WebViewClient {
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        if ("https://www.example.com".equals(url)) {
            String mimeType;//the resource response's MIME type, for example text/html
            String encoding;//the resource response's encoding
            Inputstream is;//cache of pages
            ...
            return new WebResourceResponse(mimeType,codeing,is);
        }
        return null;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM