For API level <19, there are only workarounds for using JavascriptInterface (my preferred method below) or grabbing the OnJsAlert method and using the alert() dialog. This means that you cannot use the alert() function for its intended purpose.
View:
WebView.addJavascriptInterface(new JsInterface(), "AndroidApp"); WebView.loadUrl("javascript:doStringToMyAndroid('This string from android')")
JsInterface:
public class JsInterface() { @JavascriptInterface void receiveString(String value) {
JavaScript:
function doStringToMyAndroid(stringFromAndroid){ var myJsString = "Hello World" + ;
But at API level 19+, we now have a methodJavascript method:
WebView.evaluateJavascript("(function() { return getStringToMyAndroid('" + myJsString + "'); })();", new ValueCallback<String>() { @Override public void onReceiveValue(String s) { Log.d("LogName", s);
source share