I inject JavaScript
interface to my WebView
, and once I get to my native methods that I defined in the JavaScript
, if freezes the WebView
until I return a value.
For example:
In this example the WebView
is stuck for the 8 seconds. I also double-checked that it is not the main thread, and it is indeed NOT the main thread, so I just don't see a reason why would it freeze the WebView
.
myWebView.addJavascriptInterface( new WebViewHandler.JavaScriptInterface(), "webCallHandler" );
public class JavaScriptInterface
{
@JavascriptInterface
public String jsToAndroidNativeFunc(String Data)
{
Callable<String> task = new Callable<String>() {
@Override
public String call() throws Exception {
try {
if( Looper.getMainLooper() == Looper.myLooper() )
Log.d("TAG","Main Thread");
else
Log.d("TAG","Not Main Thread");
TimeUnit.SECONDS.sleep(8); // the webview is now stuck during these 8 seconds
return "123";
}
catch (InterruptedException e) {
throw new IllegalStateException("task interrupted", e);
}
}
};
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<String> future = executor.submit(task);
try {
return future.get();
}
catch (InterruptedException e) {
e.printStackTrace();
return "111";
} catch (ExecutionException e) {
e.printStackTrace();
return "111";
}
}
}