“/media/image/123”代表H5要调用Native查看id为123的图片; “/webapp/close/webview”表示H5需要Native关闭当前页面; “/webapp/patient_card/?patient_id=345”表示H5要调用Native显示用户345的详情页
protected WebViewClient mWebClient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 添加tel链接响应
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
// 添加mp4播放相应
if (url.endsWith(".mp4")) {
viewVideo(url);
return true;
}
// 添加图片链接响应
if (Pattern.compile("/media/image").matcher(url).find()) {
viewImage(url);
return true;
}
// 关闭webview
if (Pattern.compile("/webapp/close/webview").matcher(url).find()) {
onBackPressed();
return true;
}
// 添加某个特殊url的响应
if(Pattern.compile("/webapp/patient_card/").matcher(url).find()) {
Uri uri = Uri.parse(url);
String patientId = uri.getQueryParameter("patient_id");
viewPatientInfo(patientId);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
};
WebSettings settings = mWebView.getSettings(); settings.setJavaScriptEnabled(true);
public class WebAppInterface {
@JavascriptInterface
public boolean method1(String param1, String param2) {
// do something
}
@JavascriptInterface
public boolean method2(String param1, String param2) {
// do something
}
}
WebView webView = (WebView) findViewById(R.id.webview); webView.addJavascriptInterface(new WebAppInterface(), "InterfaceName");
<script type="text/javascript">
function method1(param1, param2) {
InterfaceName.method1(param1, param2);
}
</script>
// 无参数调用
contentWebView.loadUrl("javascript:javacalljs()");
// 有参数调用
mWebView.loadUrl("javascript:test('" + param+ "')"); // param是js的函数test()的参数
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有