webView.getSettings().setAllowFileAccess(true); //如果访问的页面中有Javascript,则webview必须设置支持Javascript webView.getSettings().setJavaScriptEnabled(true); if (url.endsWith(".html")) { webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); } else { webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); } webView.getSettings().setAllowFileAccess(true); webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setDomStorageEnabled(true); webView.getSettings().setDatabaseEnabled(true); webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { error_layout.setErrorType(EmptyLayout.NETWORK_LOADING); } @Override public void onPageFinished(WebView view, String url) { error_layout.setErrorType(EmptyLayout.HIDE_LAYOUT); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); error_layout.setErrorType(EmptyLayout.NETWORK_ERROR); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Logger.e(url); // 如下方案可在非微信内部WebView的H5页面中调出微信支付 if (url.startsWith("weixin://wap/pay?")) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); startActivity(intent); return true; } else if (parseScheme(url)) { try { Intent intent; intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); intent.addCategory("android.intent.category.BROWSABLE"); intent.setComponent(null); // intent.setSelector(null); startActivity(intent); // return true; } catch (Exception e) { e.printStackTrace(); } } return false; } });
public boolean parseScheme(String url) { if (url.contains("platformapi/startapp")) { return true; } else if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.M) && (url.contains("platformapi") && url.contains("startapp"))) { return true; } else { return false; } }