缓存及一些设定
我在做一些项目时,h5做的项目手机浏览器能使用,但是在搬到webview时候不能用,这个时候通过查阅资料,原来是webview没有设定好,包括缓存、缓存大小及路径等等
mWebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); mWebview.getSettings().setJavaScriptEnabled(true); mWebview.getSettings().setDomStorageEnabled(true); mWebview.getSettings().setAppCacheMaxSize(1024 * 1024 * 8); String appCachePath = mContext.getApplicationContext().getCacheDir() .getAbsolutePath(); mWebview.getSettings().setAppCachePath(appCachePath); mWebview.getSettings().setAllowFileAccess(true); mWebview.getSettings().setAppCacheEnabled(true);截取跳转地址、监听页面变化
一定要设定WebViewClient,有些时候shouldOverrideUrlLoading会拦截不到url的跳转,后来在onLoadResource中成功拦截,感觉拦截还是onLoadResource好使,onPageStarted、onPageFinished、onReceivedError分别监听页面载入数据的开始、结束、与错误。
mWebview.setWebViewClient(new WebViewClient() {@Override public boolean shouldOverrideUrlLoading(WebView view, String url) { System.out.println("跳转Sort:" + url); }@Override public void onLoadResource(WebView view, String url) { // 分享 if (url.contains("?platformType=0")) { url = url.substring(0, url.length() - 1) + "1"; view.stopLoading(); view.loadUrl(url); return; } if (url.contains("/webapp/shareCoupon_") || url.contains("/webapp/shareRedpacket_")) { view.stopLoading(); ShareUtils.shareDialog(mContext, url); return; } super.onLoadResource(view, url); } });@Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { mWebview.setVisibility(View.INVISIBLE); } @Override public void onPageFinished(WebView view, String url) { // TODO Auto-generated method stub super.onPageFinished(view, url); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // TODO Auto-generated method stub super.onPageStarted(view, url, favicon); }}