Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are valid return values for a Javascript Interface on an Android WebView?

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?

like image 815
browep Avatar asked Feb 01 '12 23:02

browep


People also ask

How do I enable Javascript on Android WebView?

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);

Can WebView run Javascript?

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.

How does WebView work in Android?

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.


1 Answers

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

like image 149
slay Avatar answered Sep 21 '22 20:09

slay