GeekHades
GeekHades

Reputation: 3016

Android and JavaScript, JS how to get webview JS return data?

We know Android studio can get invoke the html javascript since Android API17,

ERPWebViewJsInterface erpWebViewJsInterface = new ERPWebViewJsInterface(this);
erpWebViewJsInterface.setmWebViewJSCallBack(this);
webView.addJavascriptInterface(erpWebViewJsInterface, WebViewParams.WEBVIEWW_JS_FLAG);

Sometimes, we may need return data to html ,But the Javascript how to get the data?

I think, maybe we can in javascript define resultCallBack, when we get the result, we can invoke the Javascript method!

Do you have the better method! get the Android method return data?

Upvotes: 0

Views: 460

Answers (1)

Mister Smith
Mister Smith

Reputation: 28168

The method addJavaScriptInterface is meant to call Java functions from JavaScript.

To call JavaScript code from Java you can't use that. You might want to use loadUrl instead.

For example:

webView.loadUrl("javascript:alert('Hey there!')"); 

You can replace the alert call with whatever code you need.

You could also do something similar using Apache Cordova. It allows you to call Java Plugins from JavaScript. You can pass a callback to a Cordova plugin, have the plugin cache the callback, and then run it from Java whenever you need it.

Upvotes: 1

Related Questions