Communication from Javascript to Java Android

suggest change

Android Activity

package com.example.myapp;

import android.os.Bundle;
import android.webkit.WebView;

public class WebViewActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        WebView webView = new WebView(this);

        *   Note the label Android, this is used in the Javascript side of things
        *   You can of course change this.
        webView.addJavascriptInterface(new JavascriptHandler(), "Android");

Java Javascript Handler

import android.webkit.JavascriptInterface;

public class JavascriptHandler {

     *  Key point here is the annotation @JavascriptInterface
    public void jsCallback() {
       // Do something

    public void jsCallbackTwo(String dummyData) {
       // Do something

Web Page, Javascript call

Android.jsCallback('hello test');

Extra Tip

Passing in a complex data structure, a possible solution is use JSON.

Android.jsCallback('{ "fake-var" : "fake-value", "fake-array" : [0,1,2] }');

On the Android side use your favorite JSON parser ie: JSONObject

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents