HTML5 HYBIRD混合APP需要在H5页面中打开第三方网站(例如:百度),android默认不在当前WebView中打开,反而会调用系统或外部浏览器,解决办法是自己重写WebViewClient,覆盖shouldOverrideUrlLoading并让其返回True。

实现代码

mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// webview自己加载URL,让后通知系统不需要HandleURL
view.loadUrl(url);
return true;
}
});




原因可以从Android源代码中可知,True if the host application wants to leave the current WebView and handle the url itself, otherwise return false。



/** Give the host application a chance to take over the control when a new url is about to be loaded
* in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity
* Manager to choose the proper handler for the url. If WebViewClient is provided, return true means
* the host application handles the url, while return false means the current WebView handles the url.
* This method is not called for requests using the POST "method".
@param view The WebView that is initiating the callback.
@param url The url to be loaded.
@return True if the host application wants to leave the current WebView and handle the url itself, otherwise return false.
*/

public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}


需要注意一点问题,如果你的代码中有拨打电话(tel:),发送邮件(mailto:)的话,需要在实现代码时对URL简单筛选一下才好



--------------------------------------------------------------------------------------

- 版权声明:

- 如在本页面内无特别说明,本文内容均为[李大仁博客]原创,本文版权归[李大仁博客]所有。

- 欢迎转载,转载请务必在文章页面明显位置提供原文链接并注明出处。欢迎您在转载本文时保留本段声明。

- 文章标题: Android强制使用WebView不调用系统或外部浏览器

- 独立博客: 李大仁博客

- 永久链接: http://www.lidaren.com/archives/1583

--------------------------------------------------------------------------------------

以上内容由博客自动发布工具自动发布,最终显示内容和效果会与原文内容有所偏差,敬请谅解。




更多相关文章

  1. zjujoe的专栏都是月亮惹的祸--linux android 博客汇总
  2. Android心得3.2--用SAX解析器解析xml文件内容
  3. trinea博客地址
  4. android开发新浪微博客户端 完整攻略 [新手必读]
  5. 在Android中把内容写到XML文件中
  6. Android 字体相关内容

随机推荐

  1. android 连续点击三次跳转到下一面
  2. Android .so和.a的makefile
  3. android datepicker只显示年和日期
  4. android 视频图片混合轮播实现
  5. android应用常见错误
  6. 日常记录
  7. 缓存统一管理工具类--android
  8. Android修改默认浏览器为其他浏览器
  9. 判断当前是电脑还是手机打开网页
  10. Android使用adb命令对手机进行截屏保存到