4

I am writing an Android "web" app (I will not upload the APP to the web, but set the HTML+JS inside the application resources). That means, the GUI will be HTML5, and I will use another "native" thread to read from the microphone and hopefully send the "parsed text" to the HTML5/JS code.

Is this possible? In the Android code, I see how can I call native code from JS (and I want the opposite).

http://developer.android.com/guide/webapps/webview.html

2
  • 4
    OMG, I am calling Java native... what happened to the world.... Commented Feb 9, 2012 at 20:17
  • DUP: stackoverflow.com/questions/4325639/… Commented Feb 9, 2012 at 20:51

1 Answer 1

9

Yes, Android rocks at this actually.

I'm posting example code from a random example app that I made but this should get you going.

Lets start by supposing you have to global variable to your webview controlling class:

String name = "John";
String lastName = "Doe";

In the Android class controlling the webview (during onCreate() or when you setup your webview):

webView.addJavascriptInterface(new JavaScriptInterface(), "android")

Inner class to your webview controlling class:

/**
* Sets up the interface for getting access to Latitude and Longitude data
* from device
**/
private class JavaScriptInterface {
    public String getName() {
        return name;
    }
    public String getLastName() {
        return lastName;
    }
    public void onJavascriptButtonPushed(String text) {
        Toast.makeText(WebViewControllingClass.this, "The text of the pressed button is: "+text,Toast.LENGTH_SHORT).show();
    }
}

Thats it for the Android side, now to use it through Javascript:

To get the name and lastname through JavaScript:

if (window.android){
    name = window.android.getName();
    lastName = window.android.getLastName();
}

If you want to raise the toast, set a javascript whose function would be:

if (window.android){
    window.android.onJavascriptButtonPushed("Hello");
}

Edit:

I havent tried this, but a search yielded this, try it out, it should work.

If you want to use it in the other direction:

JS:

function testEcho(message){
     window.JSInterface.doEchoTest(message);
}

Android:

myWebView.loadUrl("javascript:testEcho('Hello World!')");
Sign up to request clarification or add additional context in comments.

3 Comments

No, I want the other way. I want to push events from Java to Javascript.
Oh, sorry, one second. I missunderstood.
You can push events from Java to JS using the same JS bridge, however you must write your JS functions to receive this (or JS vars), than via webView.loadUtl("<load your JS code>"); ,actually you can do this on both ways, setting and getting values from the HTML5 page, since the page is running inside the Webview component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.