Android webview使用大全
来源:互联网 发布:广州婚纱摄影排名 知乎 编辑:程序博客网 时间:2024/06/13 06:32
package com.zy.tour.util.widget;import java.lang.reflect.Method;import android.Manifest.permission;import android.annotation.SuppressLint;import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.net.Uri;import android.util.AttributeSet;import android.view.KeyEvent;import android.view.View;import android.webkit.WebSettings;import android.webkit.WebSettings.PluginState;import android.webkit.WebSettings.RenderPriority;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Toast;import com.zy.tour.util.http.ProgressUtil;/*** * WebView客户端: * <p> * 使用权限 {@link permission#INTERNET} * <p> * 提供以下功能: * <ol> * <li>设置浏览器是否可执行缩放 {@link LWebView#setZoomEnable(boolean)} * <li>设置浏览器支持多窗口 {@link LWebView#setMultipleWindowsEnable(boolean)} * <li>设置是否支持插件 {@link LWebView#setPluginState(PluginState)} * <li>设置是否启动地理定位 {@link LWebView#setGeolocationEnabled(boolean)} * <li>设置缓存模式 {@link LWebView#setCacheMode(int)} * </ol> * <p> * 加载资源: * <ol> * <li>加载网络资源 : {@link WebView#loadUrl("http://www.google.com")} * <li>加载本地assets文件夹下的资源 {@link * WebView#loadUrl("file:///android_asset/index.html")} * </ol> * * @author li'mingqi */public class LWebView extends WebView {/** 上下文对象 **/private Context mContext;/** WebView管理 */private WebSettings mWebSetting;/** 是否支持缩放--默认支持缩放 */private boolean zoomEnable = true;/** 是否支持多窗口--默认支持多窗口 */private boolean multipleWindowsEnable = true;/** 设置插件的支持与否状态 --默认不支持插件 */private PluginState pluginState = PluginState.OFF;/** 是否启用地理定位 --默认开启 **/private boolean GeolocationEnabled = true;/** 是否弹出正在加载的progressDialog,默认弹出 **/private boolean show = true;/** * 设置缓存模式 --默认正常缓存模式-- * <ol> * <li>{@link WebSettings#LOAD_NORMAL} ,正常使用缓存 * <li>{@link WebSettings#LOAD_NO_CACHE} ,不使用缓存 * <li>{@link WebSettings#LOAD_CACHE_ELSE_NETWORK} ,高速缓存模式,若高速缓存中无数据,则执行网络加载 * <li>{@link WebSettings#LOAD_CACHE_ONLY} ,不使用网络加载,只从缓存中取 * <li>{@link WebSettings#LOAD_DEFAULT} ,默认的缓存模式,即当缓存资源可用时,使用缓存,不可用时从网络加载. * </ol> **/private int cacheMode = 0;private ProgressUtil progress = null;public LWebView(Context context) {super(context);init(context);}public LWebView(Context context, AttributeSet attrs) {super(context, attrs);init(context);}public LWebView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init(context);}@SuppressWarnings("deprecation")@SuppressLint("SetJavaScriptEnabled")private void init(Context context) {this.mContext = context;progress = new ProgressUtil(mContext);/*** * 触摸焦点起作用 */requestFocusFromTouch();/*** * 滚动条样式在视图的边缘显示滚动条, */setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);mWebSetting = getSettings();/*** * 设置浏览器执行jsp */mWebSetting.setJavaScriptEnabled(true);/*** * 设置webView是否支持缩放,默认支持; */mWebSetting.setSupportZoom(zoomEnable);/*** * 设置支持多窗口,默认支持 */mWebSetting.setSupportMultipleWindows(multipleWindowsEnable);/*** * 设置浏览器是否支持插件 */mWebSetting.setPluginState(pluginState);/*** * 设置是否支持地理定位 */mWebSetting.setGeolocationEnabled(GeolocationEnabled);/*** * 设置编码解码HTML时的编码方式 */mWebSetting.setDefaultTextEncodingName("UTF-8");/** * 设置缓存模式 */mWebSetting.setCacheMode(cacheMode);/*** * 设置渲染优先级--过时,高版本不再给予支持 */mWebSetting.setRenderPriority(RenderPriority.HIGH);/*** * 设置可以加载本地的文件,资源文件可是使用 "///android_asset","///android_res" */mWebSetting.setAllowFileAccess(true);/*** * 加载网页中的图片---默认为true */mWebSetting.setLoadsImagesAutomatically(true);/** * 开启应用程序缓存 --必须制定应用缓存目录才能生效 即 * {@link WebSettings#setAppCachePath(String)} */mWebSetting.setAppCacheEnabled(true);/*** * 设置可以使用DOM存储 */mWebSetting.setDomStorageEnabled(true);/** * 获取应用缓存路径 */String appCachePath = context.getApplicationContext().getCacheDir().getAbsolutePath();/*** * 设置webView的缓存路径 */mWebSetting.setAppCachePath(appCachePath);/*** * 加载url监听事件 */setWebViewClientListener();}/** * 设置是否弹出正在加载的progressDialog * * @param show * the show to set */public void setShow(boolean show) {this.show = show;}/*** * 加载监听 */public void setWebViewClientListener() {setWebViewClient(new WebViewClient() {/*** * 拦截url,并使用该浏览器处理该url */@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {if (url.startsWith("tel:")) {// 打电话Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse(url));mContext.startActivity(intent);} else if (url.startsWith("sms:")) {// 发短息Uri uri = Uri.parse("smsto:" + url.split(":")[1]);Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);sendIntent.putExtra("sms_body", "");mContext.startActivity(sendIntent);} else {// 正常处理urlview.loadUrl(url);}return true;}/*** * 异常 */@Overridepublic void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {super.onReceivedError(view, errorCode, description, failingUrl);Toast.makeText(mContext, description + "\n" + failingUrl,Toast.LENGTH_LONG).show();}/*** * 开始加载 */@Overridepublic void onPageStarted(WebView view, String url, Bitmap favicon) {super.onPageStarted(view, url, favicon);if(show){progress.show("正在加载数据...");}}/*** * 加载 结束 */@Overridepublic void onPageFinished(WebView view, String url) {super.onPageFinished(view, url); if(progress.isShowing()){ progress.dismiss(); }}});}/*** * 监听系统手机的返回键 */@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (canGoBack() && keyCode == KeyEvent.KEYCODE_BACK) {goBack();// 返回上一个页面,而不是finish调该页面return true;}return false;}@Overridepublic void destroy() {try {Class<?> localClass = super.getClass();Method localMethod = localClass.getMethod("freeMemory",new Class[0]);localMethod.invoke(this, new Object[0]);super.destroy();return;} catch (Exception e) {e.printStackTrace();}}/*** * 设置webView是否支持缩放 * * @param zoomEnable * 是否支持缩放 */public void setZoomEnable(boolean zoomEnable) {this.zoomEnable = zoomEnable;}/*** * 设置是否支持多窗口 * * @param multipleWindowsEnable * 是否支持 */public void setMultipleWindowsEnable(boolean multipleWindowsEnable) {this.multipleWindowsEnable = multipleWindowsEnable;}/*** * 设置插件的支持状态 * * @param pliginState * 插件状态打开与否 */public void setPluginState(PluginState pliginState) {this.pluginState = pliginState;}/*** * 设置是否开启地理定位 * * @param geolocationEnabled * 是否定位 */public void setGeolocationEnabled(boolean geolocationEnabled) {GeolocationEnabled = geolocationEnabled;}/*** * 设置缓存模式 * * @param cacheMode * 缓存模式 */public void setCacheMode(int cacheMode) {this.cacheMode = cacheMode;}interface WebLoadJavaScriptListener {void service(String... action);}}
0 0
- Android webview使用大全
- webview使用大全
- WebView使用大全
- WebView使用大全
- webView使用大全
- webview的使用大全
- WebView--- Android WebView使用总结
- Android WebView的使用
- android WebView使用
- Android WebView的使用
- Android WebView使用
- Android-----WebView使用
- Android-----WebView使用
- Android中的WebView使用
- Android-----WebView使用
- Android Webview使用
- Android-----WebView使用
- Android WebView使用
- 常喝酵素 不易得病
- ContentProvider笔记
- 手机游戏开发工程师
- 用Python遍历(Linux)的子目录并且查找出指定的字符串带(扩展名)黑白名单功能
- Hadoop Hive与Hbase整合
- Android webview使用大全
- IntelliJ Idea 常用快捷键
- Mac中使用mysql数据库基础(上)
- 覆盖,重写,重载,隐藏,多态的区别(一下子清晰了)
- cocos2d-x +lua
- Foreman配置bug
- C++ std::pair 与 std::make_pair
- Storm – Kafka – Cassandra for Big Data System
- OpenGL环境设置