js调安卓:
//js
/**
* 调用移动端方法
*/
function jsCallMobileFinish() {
let device = getDevice();
let data = null;
if (device === "android") {
if (window.Android) {
data = window.Android.jsCallMobileFinish();
}
} else if (device === "ios") {
if (window.webkit) { //调用IOS的jumpjumpNext()方法
data = window.webkit.messageHandlers.jsCallMobileFinish.postMessage("");
}
}
return data;
}
/**
* 获取移动设备
* @returns {*}
*/
function getDevice() {
let u = navigator.userAgent,
app = navigator.appVersion;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if (isAndroid) {
return "android"
} else if (isiOS) {
return "ios"
} else {
return null
}
}
//安卓
mWebView?.addJavascriptInterface(InJavaScript(), "Android")
val webSettings = mWebView!!.settings
webSettings.javaScriptEnabled = true
class InJavaScript {
@JavascriptInterface
fun get(data: String?) {
if (data != null ) {
try {
} catch (e: Exception) {
e.printStackTrace()
}
}else{
}
}
}
安卓调用js:
//安卓
fun callJsMethod(methodName:String,vararg params:String?){
var paramsStr = ""
params.let {
paramsStr = "\'${it.joinToString("\',\'")}\'"
}
val url = "javascript:$methodName($paramsStr)"
LogUtils.i("huanghai","url",url)
webview.loadUrl(url)
// webview.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)
}
//js
window.androidCallJs = function() {
}
网友评论