简体   繁体   中英

evaluateJavascript only wok in main thread?

I tried to use evaluateJavascript method in my main thread and it works fine. But if I move the function to my other thread (I have a service running in backgroud waiting for message). It does not work at all. So I am assuming evaluateJavascript only works in main thread, which is UI thread. Is this the fact or I am doing anything wrong?

`myWebView.evaluateJavascript("test()", null);`

test() is the method in HTML application

Is there any way to call evaluateJavascript in other thread?

According to the docs:

Asynchronously evaluates JavaScript in the context of the currently displayed page. If non-null, |resultCallback| will be invoked with any result returned from that execution. This method must be called on the UI thread and the callback will be made on the UI thread.

So it runs asynchronously by default and must be used from the UI thread, ie no need to create a separate thread by yourself.

Finally I got the answer. Use runOnUiThread() method in other thread to invoke evaluateJavascript() will work, if anyone is interested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM