webview在加載網頁的時候會默認調起手機自帶的瀏覽器加載網頁,用戶體驗很差。但當用戶設置瀏覽器客戶端(setWebViewClient)設置這樣的監聽事件以後,當請求url的時候就不會打開手機自帶的瀏覽器。android
webview.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); LoadingDialog.closeDialog(); } });
在此方法中能夠監聽瀏覽器開始加載網頁和加載網頁結束。git
今天着重講的並非上邊的兩個方法而是下面的這位github
putinmoney_webview.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
} });
shouldOverrideUrlLoading並非每次都在onPageStarted以前開始調用的,只有在調用webview.loadURL的時候纔會調用。web
webview.loadUrl("https://qr.alipay.com/bax05351pgjhc4yegd2y2084");
但發起請求的時候後,webview的連續動做是前後請求兩個urlapi
1.https://mobilecodec.alipay.com/client_download.htm?qrcode=bax05351pgjhc4yegd2y2084瀏覽器
2.https://ds.alipay.com/from=mobilecodec&scheme=alipayqr%3A%2F%2Fplatformapi%2Fstartapp%3FsaId%3D10000007%26clientVersion%3D3.7.0.0718%26qrcode%3Dhttps%253A%252F%252Fqr.alipay.com%252Fbax05351pgjhc4yegd2y2084%253F_s%253Dweb-otherapp
以後返回一個意圖,也是用這個意圖來打開支付寶appide
intent://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode=https%3A%2F%2Fqr.alipay.com%2Fbax05351pgjhc4yegd2y2084%3F_s%3Dwebother&_t=1474448799004#Intent;scheme=alipayqr;package=com.eg.android.AlipayGphone;endpost
webview.setWebViewClient(new WebViewClient(){ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // TODO Auto-generated method stub super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { // TODO Auto-generated method stub super.onPageFinished(view, url); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.e(TAG, "訪問的url地址:" + url); if (parseScheme(url)) { try { Uri uri = Uri.parse(url); Intent intent; intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); intent.addCategory("android.intent.category.BROWSABLE"); intent.setComponent(null); // intent.setSelector(null); startActivity(intent); } catch (Exception e) { } } else { view.loadUrl(url); } return true; } });
關於"android.intent.category.BROWSABLE"參考:
http://blog.csdn.net/gf771115/article/details/7827833ui
此外要對webview設置下:
WebSettings webSettings = webview.getSettings(); webSettings.setJavaScriptEnabled(true); // 設置能夠訪問文件 webSettings.setAllowFileAccess(true); // 設置支持縮放 webSettings.setBuiltInZoomControls(true); webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); // webSettings.setDatabaseEnabled(true); // 使用localStorage則必須打開 webSettings.setDomStorageEnabled(true); webSettings.setGeolocationEnabled(true);
實例下載地址:https://github.com/reachchen/loadurltoali.git
在項目中發現的問題在此修正:
發現一些手機(例如一些升級了7.0的手機)在請求後並無返回intent,因此就會出現調不起支付寶的狀況。可是用瀏覽器直接打開是能夠的
兼容的思路以下:
新建一個線程,在用戶發出請求後,若是10秒的時間沒有收到調起的intent,就用瀏覽器直接調起。
代碼以下:
public boolean parseScheme(String url) { if (url.contains("platformapi/startapp")){ myHandler.removeCallbacks(runable); return true; } else if(url.contains("web-other")){ myHandler.postDelayed(runable, 10000); return false; }else { return false; } }
新建的線程
myHandler = new Handler(); runable = new Runnable() { @Override public void run() { // TODO Auto-generated method stub LoadingDialog.closeDialog(); webview_another.loadUrl(myaliWebUrl.get("code_url")); } }; }