How to call a function in WebView Javascript from Android Java without delay?

Issue

When using this method to call javascript functions there is a few millisecond delay (10ms – 50ms) in callback

 webView.evaluateJavascript("javascript: updateARObject()", callback);

How to call a function instantaneously in javascript, all files are in asset folder of Android.

Is it possible with some custom WebView like Xwalkview? Or socket connection between both? ARCore is communicating with Three.js in real time, How to achieve that?

Solution

The reason the callback is present is because it cannot be done instantaneously from android app.

The main reason for that is that Android uses WebView to run javascript. Modern Android WebView is just an instance of Google Chrome wrapped in android view. As you understand this instance needs to be run on separate process as it does. If one chooses to dig deep he will find out that the communication between android app and WebView is done via AIDL service as all the interprocess communications should be done in android.

Taking into consideration the time needed to write an AIDL call into stack allocate memory and form message(main AIDL method of communication), send it, unwrap in Chrome, parse, execute javascript and do all this operations one more in order to answer – 10ms – 50ms is very low latency.

Maybe ArCore has its own its own js processor to handle js files fast. Or Google Chrome has its own method to communicate with ArCore(or inner instance). I don’t know that – but it is Google developed products – I think they know their shortcuts.

Custom WebView may work – the only thing is that basically it will be a new version of browser you will need to support your js for it separately from all the other browsers due to a whole bunch of possible reasons.

Hope it helps you somehow.

Answered By – Pavlo Ostasha

Leave a Comment