I have an Android WebView that has JavaScript that is calling Android methods through the addJavascriptInterface method:
myWebview.addJavascriptInterface(new JavascriptBridge(), "Android"); public class JavascriptBridge { public String getAString() { return "my_str"; } }
This works fine. I want to return a list of ints to the WebView. Tried this:
public class JavascriptBridge { public int[] getMyInts() { return new int[]{1,2,3}; } }
but calling this function in JS returns undefined:
var myInts = Android.getMyInts();
Is there a list of valid return types for an Android Javascript Interface? Is it only primitives?
JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView .You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.id.webview);
WebView is a special component in Android which serves as kind of built-in browser inside Android applications. If you want to execute HTML, CSS or JavaScript code in your Android app, or you need to allow users visit a URL without leaving your application, WebView is the way to go.
The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.
I have not seen a list of valid types (for passing values to Java functions and to return), but only primitives and string seem to work.
You can use JSON (e.g. stringify and parse in Javascript, check various Java options at json.org
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With