如何在android中加载webview离线

时间:2015-11-06 21:22:47

标签: android webview offline-caching

好吧,我有一个webview,当它在线时加载网页(例如google.com),但是当它离线时它说无法加载网页.. 我希望在手机连接互联网时显示最后一个网页。

我是新手,所以你可以让我成为一个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);
    }
}


}

1 个答案:

答案 0 :(得分:0)

如果网络处于脱机状态,您必须从缓存加载: -

1.首先在清单中添加这些权限 -

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