The easiest way of doing that is storing the javascript calls in a plugin or a JavascriptInterface and have repeating method in your javascript side executing it. That way loadUrl
is never called.
Something like:
StringBuilder sb = null;
private final Object LOCK = new Object();
public void sendJavascript(String js){
synchronized(LOCK) {
if (sb == null){
sb = new StringBuilder();
sb.append("javascript:");
}
sb.append(js):
}
}
Then using a javascriptInterface:
private class JsInterface {
@JavascriptInterface
public String getJavascript(){
synchronized(LOCK) {
String javascriptToRun = sb.toString();
sb = new StringBuilder();
sb.append("javascript:");
return javascriptToRun;
}
}
}
Add your javascript interface to your webview:
JsInterface jsInterface = new JavascriptInterface();
webview.addJavascriptInterface(jsInterface, "JsInterface");
And on your javascript code set a timed interval for retrieving the stored function.
EDIT
I just found this answer, it shows mine has a big problem, the methods are not synchronized. Meaning when you call getJavascript()
our variable sb
might not be ready to be read yet. I've added the relevant bit to the source above.
USE THE LOCKS or your code will be unreliable