webview使用

public class CacheWebView {
	private Context context;
	private WebView web;
	private WebSettings settings;
	private boolean cache = true;// 是否使用cache
	private boolean useCache = true;// 使用cache总开关
	private boolean firstAnotherIsUseCache = true;
	private String titles;
	private onRefreshTitleListener Listener;
	
	private boolean Backing = false;
	private interfaceMyWebView inteMyWebView=null;
	private int statu = 100;
	private SwipeRefreshLayout swipeRefreshLayout = null;
	


	@SuppressLint("SetJavaScriptEnabled")
	public CacheWebView(WebView web, Context context) {

		this.context = context;
		
		this.web = web;
		settings = web.getSettings();
		settings.setJavaScriptEnabled(true);//设置支持JS代码
		settings.setAllowFileAccess(true);
		settings.setLoadsImagesAutomatically(true);//图片自动加载
		settings.setGeolocationEnabled(true);//支持LBS定位
		settings.setGeolocationDatabasePath(context.getFilesDir().getPath());
		web.requestFocusFromTouch();
		web.setWebViewClient(new myWebViewClient());
		web.setWebChromeClient(new myWebChromeClient());

	}

	private void testGeolocationOK() {
		LocationManager manager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		boolean gpsProviderOK = manager
				.isProviderEnabled(LocationManager.GPS_PROVIDER);
		boolean networkProviderOK = manager
				.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		boolean geolocationOK = gpsProviderOK && networkProviderOK;
		Log.i("loc", "gpsProviderOK = " + gpsProviderOK
				+ "; networkProviderOK = " + networkProviderOK
				+ "; geoLocationOK=" + geolocationOK);
	}



	public void goBack() {
		Backing = true;
		web.goBack();
	}

	public void loadUrl(String url, boolean useCache) {
		setIsUseCache(useCache).loadUrl(url);
	}

	// 打开网页是否使用缓存
	public void loadUrl(String url) {

		web.stopLoading();
		cache = true;
		if (useCache) {
			settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		} else {
			settings.setCacheMode(WebSettings.LOAD_DEFAULT);

		}
		web.loadUrl(url);

		testGeolocationOK();
	}

	// 是否使用缓存
	public CacheWebView setIsUseCache(boolean useCache) {
		this.useCache = useCache;
		this.firstAnotherIsUseCache = useCache;
		return this;
	}

	public CacheWebView OverrideUrlLoading(interfaceMyWebView inteMyWebView) {
		this.inteMyWebView = inteMyWebView;
		return this;

	}

	// 第一次加载是否使用缓存,之后是否使用缓存
	public void loadUrl(String url, boolean useCache, boolean firstIsUseCache) {
		
		if (firstIsUseCache) {
	
			web.stopLoading();
			web.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
			web.loadUrl(url);
			useCache = false;
		} else {
			loadUrl(url, false);
		}
		firstAnotherIsUseCache = useCache;

	}


	
	private class myWebViewClient extends WebViewClient {
		
		//每次加载网页都会调用
		@Override
		public void onLoadResource(WebView view, String url) {
			
			super.onLoadResource(view, url);
		
			view.loadUrl(Config.js.closeHFder);
			if (inteMyWebView != null) {
				inteMyWebView.onPageStart(view, url, settings);
			}

		}

		//加载网页会有时会自动跳转,此方法可以控制url
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (firstAnotherIsUseCache) {
				useCache = firstAnotherIsUseCache;
				cache = true;
				view.getSettings().setCacheMode(
						WebSettings.LOAD_CACHE_ELSE_NETWORK);//默认使用缓存
			}

			view.loadUrl(url);
			return true;
		}

		//网页加载完成调用此方法
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			view.loadUrl(Config.js.closeHFder);
			if (Backing) {
				Backing = false;
				return;
			}
			
			if (useCache) {
				if (cache) {
					view.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//网络加载
					view.loadUrl(view.getUrl());
					cache = false;
				}
			}
			if (Tools.isNetworkAvailable(context)) {

			} else {
				view.getSettings().setCacheMode(
						WebSettings.LOAD_CACHE_ELSE_NETWORK);
				Toast.makeText(context, "网络无连接...", 2).show();
			}

		}

	}

	private class myWebChromeClient extends WebChromeClient {

		public void onGeolocationPermissionsHidePrompt() {
			super.onGeolocationPermissionsHidePrompt();
		}

		//监听网页加载进度
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			
			super.onProgressChanged(view, newProgress);
			statu = newProgress;
			if (newProgress < 100) {
				if (swipeRefreshLayout != null)
					swipeRefreshLayout.setRefreshing(true);
				
			} else {
				if (inteMyWebView!=null) {
					inteMyWebView.OverrideUrlLoading(view, view.getUrl(), settings);	
				}

				view.loadUrl(Config.js.closeHFder);
				
				if (Listener != null) {
				
					titles = view.getTitle();//获取该url的title
					
				}
				if (swipeRefreshLayout != null)
					swipeRefreshLayout.setRefreshing(false);
				
			}
		}

		@Override
		public void onGeolocationPermissionsShowPrompt(final String origin,
				final Callback callback) {

			callback.invoke(origin, true, true);

			super.onGeolocationPermissionsShowPrompt(origin, callback);
			Log.i("loc", "onGeolocationPermissionsShowPrompt");
		}

	}

	public interface interfaceMyWebView {

		public String OverrideUrlLoading(WebView view, String url,
				WebSettings settings);

		public void onPageStart(WebView view, String url, WebSettings settings);
	}

	public interface onRefreshTitleListener {
		public void onRefreshTitle(String title);
	}

	public void setonRefreshTitleListener(onRefreshTitleListener listener) {
		this.Listener = listener;
	}

	public WebView getWeb() {
		return web;
	}

	public void setWeb(WebView web) {
		this.web = web;
	}

	public SwipeRefreshLayout getSwipeRefreshLayout() {
		return swipeRefreshLayout;
	}

	public void setSwipeRefreshLayout(SwipeRefreshLayout swipeRefreshLayout) {
		this.swipeRefreshLayout = swipeRefreshLayout;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值