-1

I'm trying to use javascript interface along with google maps javascript library.
I searched and i found a way almost all similar to this JavascriptInterface .

This is my code :

 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>

 <script type="text/javascript">
    function testAPI() {
        JSInterface.test();
    }
 </script>
 <script>
  // gooogle maps code 
  // 
 </script>
 </head>
 <body>
 <div id="map-canvas"></div>
 <div><input type="button" value="Make Toast" onClick="testAPI()" /></div>

This is my interface :

public class JavaScriptInterface {
private Activity activity;

public JavaScriptInterface(Activity activiy) {
    this.activity = activiy;
}

public void test(){
    Toast.makeText(activity.getApplicationContext(),
            "Javascript interface test.", 0).show();
}
}

This is where i create all :

myBrowser = (WebView) findViewById(R.id.mybrowser);

    myBrowser.setWebChromeClient(new WebChromeClient() {
        public void onGeolocationPermissionsShowPrompt(String origin,
                GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
        }
    });
    myBrowser.getSettings().setJavaScriptEnabled(true);
    String javascrips = null;
    // myBrowser.loadUrl("file:///android_asset/geolocation.html");
    try {
        AssetManager am = getAssets();
        InputStream input = am.open("geolocation.html");
        int size;
        size = input.available();
        byte[] buffer = new byte[size];
        input.read(buffer);
        input.close();
        javascrips = new String(buffer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    jsInterface = new JavaScriptInterface(this);
    myBrowser.addJavascriptInterface(jsInterface, "JSInterface");

    myBrowser.loadDataWithBaseURL("file:///android_res/raw/", javascrips,
            "text/html", "UTF-8", null);

When clicking on the button (the javascript input with the testAPI() function ) , nothing happens , The Toast doesn't not show up why ?

Community
  • 1
  • 1
saeed hardan
  • 485
  • 2
  • 11
  • 24

1 Answers1

1

Try adding @JavascriptInterface to your test() method

http://developer.android.com/reference/android/webkit/JavascriptInterface.html

Need the annotation starting with JellyBean

mjstam
  • 1,049
  • 1
  • 6
  • 5
  • Welcome, stumped my for the better part of a day when we changed our default build platform. – mjstam Dec 19 '14 at 18:09