我在作一些項目時,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);web
必定要設定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); } }