Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding JavaScript Interface to a Browser

Tags:

android

Is it possible to add a JavaScript interface to the Android Browser the same way one can be added to the WebView Component as illustrated in this demo. My particular use case only needs JavaScript -> android so that I can send it back to the previous activity.

like image 505
James A Wilson Avatar asked Sep 22 '10 02:09

James A Wilson


2 Answers

You can invoke methods and functions in your webview by using javascript url's, e.g.

webview.loadUrl("javascript:somemethod()");

You will, of course, need to enable javascript on your webview:

webview.getSettings().setJavaScriptEnabled(true);

This is from java to javascript. If you want to invoke java code / android API's from javascript, use addJavascriptInterface()

webview.addJavascriptInterface(new MyJSJavaBridge(), "api");

All of this is shown in the example url you posted as well.

like image 81
Ivo van der Wijk Avatar answered Oct 13 '22 01:10

Ivo van der Wijk


You can do it using jsinterface. First you need to make the browser jsinterface enabled and then you may call to Android method from the HTML of your browser.

You may , get a fair example and idea here ...

http://android-puremvc-ormlite.blogspot.com/2011/07/using-java-script-in-android-webview.html

like image 30
Surojit Avatar answered Oct 13 '22 01:10

Surojit