WebSettings mWebSettings = getSettings();
mWebSettings.setDefaultTextEncodingName("UTF-8");//设置默认的显示编码
mWebSettings.setJavaScriptEnabled(true);//调用JS方法.安卓版本大于17,加上注解 @JavascriptInterface
addJavascriptInterface(new Object() {
@JavascriptInterface
public void toastMessage(final String url, final int type, final int dir) {
L.e("url = " + url + " type = " + type + " dir = " + dir);
APIWrapper.getInstance()
.getLenovoWord(url)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new RxSubscriber<ResponseBody>() {
@Override
public void _onNext(ResponseBody responseBody) {
try {
String data = responseBody.string();
L.e("data = " + data);
loadUrl("javascript:ResCompleted(" + data + "," + type + "," + dir + ")");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void _onError(String msg) {
loadUrl("javascript:ResCompleted(" + msg + ")");
}
});
}
}, "Android");
<html>
<meta name="viewport"
content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">
<head>
<title>Js调用Android代码</title>
<style>
#button-call{
width: 100%;
padding: 20px;
font-size: 20px;
}
#div_bg{
background: #cccccc;
margin-top: 50px;
}
</style>
<script type="text/javascript">
window.onload=function() {
document.getElementById('button_call').onclick=function(){
window.Android.toastMessage("http://api.sina.cn/sinago/list.json?channel=news_toutiao",1, 0);
}
}
function ResCompleted(result,type,dir) {
document.getElementById('div_bg').innerHTML='Android调用JS代码-成功!!!'+JSON.stringify(result);
}
</script>
</head>
<body>
<button id="button_call">Js调用Android代码</button>
<div id="div_bg"></div>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有