在PC端,咱们能够经过一个URL连接,点击后启动QQ,这是很好的用户跳转体验。很方便。android
使用的连接以下:web
<a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=8888888&site=qq&menu=yes"
>click</a>浏览器
其中把8888888 换成你的QQ号。ide
那么在android里能够这么使用么? 实际上在 默认浏览器 (或者 UC) 里能够作到。而使用自定义的webView时,体验就不那么好了。ui
咱们常常在WebView里 进行 下面的重载 setWebViewClient的对象url
webView1.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } });
若是这样作了,就没法跳转到QQ了。为何呢?spa
经捕获发送的请求包,我发现了在http://wpa.qq.com的请求中,实际还发送了这么一个请求:mqqwpa://im/chat开头的。code
mqqwpa:// 这部分URL的部分,叫作URL的sechme部分,他是http:// ,https://相似,而http:// ,https://是网页,能够打开的。交由webView处理,而其余的则交由默认处理。这样就呢过解决咱们的 在URL连接点击后调用QQ。对象
因此你须要重载 shouldInterceptRequest 方法,来截获其余的sechme处理。blog
像下面这样:
webView1.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) { if (url.startsWith("http") || url.startsWith("https")) { return super.shouldInterceptRequest(view, url); } else { Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(in); return null; } } });