201010-Android A WebView method was called on thread JavaBridge

Android开发过程中,偶遇一个webview与android通信的问题,提示A WebView method was called on thread 'JavaBridge'. All webview methods must be called...

这个问题发现Android 9以上会出现,解决方法是@JavascriptInterface注解的方法必须指定在UI线程执行,将原来方法里的代码放入run里面执行即可

如下

1
2
3
4
5
6
// 注意这个activity就是webview所在的activity
H5DetailActivity.this.runOnUiThread(new Runnable() {
public void run() {
webView.loadUrl("javascript:initBookList('" + ans + "')");
}
});
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×