2011-01-07 35 views
25

Trên thiết bị Android tối đa và bao gồm 4.4.2, Trình duyệt mặc định và Chrome hỗ trợ tệp kê khai bộ nhớ cache HTML5. Tuy nhiên, trên các thiết bị tương tự, thành phần WebView dường như không hỗ trợ tệp kê khai bộ nhớ cache HTML5. Có ai biết cách tôi có thể làm cho thành phần WebView hỗ trợ tệp kê khai HTML5 không?Tôi làm cách nào để chế độ xem web sử dụng tệp kê khai bộ nhớ cache HTML5?

+4

tôi thấy kết quả ở đây: http://alex.tapmania.org/?p=110 –

+0

Có lẽ khía cạnh hầu hết các phi trực quan tôi đã lấy đi từ dung dịch đăng là bạn phải tự thiết lập appCachePath của WebView, hoặc nó sẽ không hoạt động. Bạn sẽ không biết tại sao nó không hoạt động. Nó sẽ không. – KevinH

+2

Tôi thực sự không nhận được nơi này quá địa phương – RMalke

Trả lời

0

Hãy thử mã này:

private void enableHTML5AppCache() { 

      webView.getSettings().setDomStorageEnabled(true); 

      // Set cache size to 8 mb by default. should be more than enough 
      webView.getSettings().setAppCacheMaxSize(1024*1024*8); 

      // This next one is crazy. It's the DEFAULT location for your app's cache 
      // But it didn't work for me without this line 
      webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache"); 
      webView.getSettings().setAllowFileAccess(true); 
      webView.getSettings().setAppCacheEnabled(true); 

      webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 
    } 

Here liên kết.

0
@Override 
public void onReceivedError(WebView view, int errorCode, 
     String description, String failingUrl) 
    { 
    // The magic redirect 
    if("http://HTML5app.com/app/".equals(failingUrl)) { 
     // main.html is the place we are redirected to by the server if we are online 
     mWebView.loadUrl("http://HTML5app.com/app/main.html"); 

    return; 
    } 
    else if("http://HTML5app.com/app/main.html".equals(failingUrl)) { 
    // The cache failed - We don't have an offline version to show 
    // This code removes the ugly android's "can't open page" 
    // and simply shows a dialog stating we have no network 
    view.loadData("", "text/html", "UTF-8"); 
    showDialog(DIALOG_NONETWORK); 
    } 
} 

Phương thức trên sẽ được sử dụng để xử lý chuyển hướng trong trường hợp ngoại tuyến. [Để triển khai appcache và đường dẫn, hãy tham khảo bình luận trước đó.

tham khảo Link: HTML5 Cache mechanism in android

1
webView.getSettings().setDomStorageEnabled(true); 

// Set cache size to 8 mb by default. should be more than enough 
webView.getSettings().setAppCacheMaxSize(1024*1024*8); 

// This next one is crazy. It's the DEFAULT location for your app's cache 
// But it didn't work for me without this line 
webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache"); 
webView.getSettings().setAllowFileAccess(true); 
webView.getSettings().setAppCacheEnabled(true); 

webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 
+0

'webView.getSettings(). SetAppCacheMaxSize (1024 * 1024 * 8)' được depricated –

Các vấn đề liên quan