android WebView常用总结

常用方法

webview 官方文档:传送门

WebView wv = findViewById(R.id.web_view);
WebSettings settings = getSettings();
settings.setJavaScriptEnabled(true); //启用js
//设置缓存模式,缓存模式如下
settings.setCacheMode(WebSettings.LOAD_DEFAULT);

缓存的模式

  • WebSettings.LOAD_DEFAULT 如果本地缓存可用且没有过期则使用本地缓存,否加载网络数据 默认值
  • WebSettings.LOAD_CACHE_ELSE_NETWORK 优先加载本地缓存数据,无论缓存是否过期
  • WebSettings.LOAD_NO_CACHE 只加载网络数据,不加载本地缓存
  • WebSettings.LOAD_CACHE_ONLY 只加载缓存数据,不加载网络数据

缓存

与原生交互

WebView wv = findViewById(R.id.web_view);
wv.addJavascriptInterface(hsBridge, "HSBridge");

你可能感兴趣的:(Android精华,android,webview)