2015-11-06 95 views
-4

好吧,我有一個webview加載網頁時,它是聯機的(例如google.com),但當它是脫機它說網頁無法加載.. 我想顯示手機連接互聯網時的最後一個網頁。我如何加載webview脫機android

我是新來的,所以你可以讓我成爲一個java類,這樣我就可以複製/粘貼。

我的源代碼

public class tab1 extends Fragment { 

WebView webView ; 
ProgressBar progressBar; 



public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View view=inflater.inflate(R.layout.tab1,container,false); 




    webView = (WebView) view.findViewById(R.id.webview2); 
    webView.getSettings().setDomStorageEnabled(true); 
    webView.getSettings().setAppCacheEnabled(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. 
    // UPDATE: no hardcoded path. Thanks to Kevin Hawkins 
    // String appCachePath = this.getCacheDir().getAbsolutePath(); 
    // Log.e(TAG, "appCachePath = " + appCachePath); 
    // webView.getSettings().setAppCachePath(appCachePath); 
    progressBar = (ProgressBar)view.findViewById(R.id.progressBar2); 
    webView.setWebViewClient(new myWebClient()); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 
    webView.loadUrl("http://google.com"); 

    return view; 
} 

public class myWebClient extends WebViewClient 
{ 
    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     // TODO Auto-generated method stub 
     super.onPageStarted(view, url, favicon); 
    } 




    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // TODO Auto-generated method stub 
     progressBar.setVisibility(View.GONE); 
     Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
     startActivity(browserIntent); 
     return true; 



    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
     // TODO Auto-generated method stub 
     super.onPageFinished(view, url); 

     progressBar.setVisibility(View.GONE); 
    } 
} 


} 

回答

0

你必須從緩存中加載如果網絡脫機: -

1.首先在你的manifest-

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

2添加這些權限。加載你的網址,但檢查網絡是否在線。 使用 -

if (!isNetworkAvailable()) { // loading offline 
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 
} 
//just add this statement before you calls load. 


private boolean isNetworkAvailable() { 
ConnectivityManager connectivityManager = (ConnectivityManager)  getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
} 
+0

你好@Noorudheen我添加代碼,但是說不能解析方法'isNetworkAvailable()'參見[截圖](http://i.imgur.com/BICogSE.png)由isNetworkAvailable – Stevenjobs

+0

( ) - 我的意思是你必須在這裏檢查網絡狀態。看我的編輯來這樣做。 –