Im trying to evaluateJavascript query with blocking function in my webview. Meaning function thread awaits result. However, evaluateJavascript and ValueCallback are both called on main thread, and main thread is paused awaiting for result, meaning result can never be caught with await. Here is my example,
private String getFirstUser(){
String evS = "document.getElementsByClassName(\"hm-user\")[0].innerHTML";
final CountDownLatch cdl = new CountDownLatch(1);
final StringBuilder sb = new StringBuilder();
try{
evaluateJavascript(evS, new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
if(value != null && value.length() != 0 && !value.equals("null")){
sb.append(value);
}
cdl.countDown();
}
});
cdl.await(200, TimeUnit.MILLISECONDS);
}catch (Exception e){};
return sb.length() == 0 ? null : sb.toString();
}
What happens is, callback does not happen until await is unblocked, meaning function always return null?
What am I doing wrong? I have used this for http requests and it worked.
Edit: I am aware of similiar threads like Android main thread blocking WebView thread however, there are no solutions available.