简体   繁体   中英

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

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?

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.

From kitkat onwards use evaluateJavascript method instead loadUrl to call the javascript functions like below:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
    webView.evaluateJavascript("enable();", null);
} else {
    webView.loadUrl("javascript:enable();");
}

you can Use function call delay by setting:

window.setTimeout(function () { ... })

But,It won't necessarily run right away, neither will explicitly setting the delay. If you will use setTimeout, it removes the function from the execution queue and it will only be invoked after JavaScript has finished with the current execution queue.

console.log(1);
setTimeout(function() {console.log(2)});
console.log(3);
console.log(4);
console.log(5);
//console logs 1,3,4,5,2

for more details see http://javascriptweblog.wordpress.com/2010/06/28/understanding-javascript-timers/

Hope, you'll find your answer. Cheers!

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