-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);
}
}
}
你好@Noorudheen我添加代碼,但是說不能解析方法'isNetworkAvailable()'參見[截圖](http://i.imgur.com/BICogSE.png)由isNetworkAvailable – Stevenjobs
( ) - 我的意思是你必須在這裏檢查網絡狀態。看我的編輯來這樣做。 –