WebView点击链接的时候出现ERR_UNKNOWN_URL_SCHEME

阿酷tony
阿酷tony This guy is lazy,Introduction has not been set

1 People liked this article · 3346 views

我是阿酷tony,从事视频直播行业近十年,方向:视频直播、微信直播、视频加密技术、流媒体环境、网页视频播放器,组建了一个交流群,点击加入一起交流

发现问题描述中图片展示的链接是一个百度APP的自定义协议,而由于WebView只支持http和https这样的协议,所以当遇到这种自定义协议的时候WebView就不知道该怎么解析了。

设置webViewClient并在shouldOverrideUrlLoading方法中对拦截的url进行过滤处理:

mWebView.webViewClient = object: WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
return if(url!!.startsWith("http:")||url.startsWith("https:")){
//对http或者https协议的链接进行加载
view!!.loadUrl(url)
true
}else{
//这里需要捕捉异常,因为如果没有安装相关的APP会有类找不到的异常
try {
//启动对应协议的APP
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
}catch (e:Exception){
}
true
}
}
}

Published on 2021-04-28 23:19

Disclaimers:

This document is written by 阿酷tony Original published on SaaS点评网_SaaS软件超市 ,The copyright belongs to the author。

Log in,More exciting content waiting for you to find,Contribute wonderful answers,Participate in comment interaction

go Sign in! No accountgoregister

帮选软件
All Rights Reserved Powered BY SaaS点评网 © 2025 浙ICP备2021004744号