android webview 缓存以及无网状态显示处理
2016-10-21 10:57
375 查看
public class MainActivity extends AppCompatActivity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webview); webView.setWebViewClient(new WebViewClient()); WebSettings settings = webView.getSettings(); // 开启javascript设置 settings.setJavaScriptEnabled(true); // 设置可以使用localStorage settings.setDomStorageEnabled(true); // 应用可以有数据库 settings.setDatabaseEnabled(true); String dbPath =this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); settings.setDatabasePath(dbPath); // 应用可以有缓存 settings.setAppCacheEnabled(true); String appCaceDir =this.getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath(); settings.setAppCachePath(appCaceDir); if (isWifi(this)) { //当前有可用网络 settings.setCacheMode(WebSettings.LOAD_DEFAULT); //设置 缓存模式( 根据cache-control决定是否从网络上取数据。) } else { //当前没有可用网络 settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //设置 缓存模式(只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。) } webView.loadUrl("https://www.baidu.com"); } private static boolean isWifi(Context mContext) { ConnectivityManager connectivityManager = (ConnectivityManager) mContext .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; } return false; } }
相关文章推荐
- Android WebView自定义处理错误页面显示(404等)
- android webview缓存显示与管理
- Android WebView自定义处理错误页面显示
- Android WebView 缓存处理
- Android 7.0系统webview 显示https页面空白处理
- Android WebView 缓存处理
- Android使用Webview显示页面以及点击跳转startActivity问题
- Android WebView 缓存处理
- android Cache——webview的缓存处理
- Android - WebView 缓存处理
- Android WebView 缓存处理
- Android WebView 缓存处理
- android 关于 webview 控制其它view的显示 以及更改view数据失败的问题总结
- android webview & ios uiwebview和wkwebview的交互以及本地缓存
- android webview 使用以及一些常见的异常处理
- Android使用WebView显示网页(图片大小的处理及内容的自适应)
- Android WebView 缓存处理
- 关于android 端,人人网以及新浪网 webview 页面缓存问题
- android WebView实现播放网络视频以及全屏显示
- Android WebView 缓存处理