如何在没有网络时加载webview(离线)

时间:2015-11-07 14:48:30

标签: android webview

我想在线加载网页,当没有网络(离线)时,它应该在连接到互联网时加载最后一个网页。

我试过,但它给了我一个错误,你可以看到这个screenshot

cannot resolve method 'isNetworkAvailable()'

这是源代码

public class tab1 extends Fragment {

WebView webView ;
ProgressBar progressBar;
protected File extStorageAppBasePath;
protected File extStorageAppCachePath;





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);
    progressBar = (ProgressBar)view.findViewById(R.id.progressBar2);
    webView.getSettings().setAppCacheMaxSize(8*1024*1024);
    webView.getSettings().setAppCachePath(getActivity().getCacheDir().getAbsolutePath() );
    webView.getSettings().setAllowFileAccess( true );
    webView.getSettings().setAppCacheEnabled( true );
    webView.getSettings().setJavaScriptEnabled( true );
    webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default

    if ( !isNetworkAvailable() ) { // loading offline
        webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
    }




    webView.setWebChromeClient(new WebChromeClient());
    webView.setWebViewClient(new myWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://freers250.com/wordpress/?page_id=72");

    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);
    }
}


}

1 个答案:

答案 0 :(得分:0)

这是因为你没有一个名为isNetworkAvailable()的函数。 尝试添加:

public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        NetworkInfo netWorkInfo = info[i];
                        if (netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                            return true;
                        } else if (netWorkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }