我写了一个webview调用android方法,这个回调方法可以更新UI,但是这个方法不是UI线程,为什么呢?谢谢!
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test HTML</title>
</head>
<body>
<button onclick="callNativeMethod()">Call Android Method</button>
<script>
function callNativeMethod(){
window.android.toast("invock android method")
}
</script>
</body>
</html>
android
webView.loadUrl("file:///android_asset/test.html");
webView.addJavascriptInterface(new JSBridge(), "android");
JSBridge
class JSBridge {
@JavascriptInterface
public void toast(String msg) {
Log.e(TAG, "toast: " + Thread.currentThread().getName());
Log.e(TAG, "toast: " + (Looper.getMainLooper() == Looper.myLooper()));
textView.setText(msg);
Toast.makeText(WebViewActivity.this, msg, Toast.LENGTH_SHORT).show();
}
}
LOG
12-10 10:35:50.194 21207-21275/lsl.test.cheyixiaotest E/WebViewActivity: toast: JavaBridge
toast: false