Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling android native APIs from javascript functions of embedded WebView

I am blackberry developer and I don't have any experience in Android programming.

I am trying to create an android application which will have webview/embedded browser. I will load some web page with some javascripts in embedded browser. There will be some buttons on the page which will call javascript functions. Some of these functions require access to native APIs (eg Camera API). Is it possible to call native api's from javascript functions or access to native code of the app ?

Right now this is possible in blackberry platform using Javascript extension. I'm expecting something similar in Android.

Thank you.

Anyone who is looking for the answer, I've found solution based on @dtanders answer. Check this out: http://developer.android.com/guide/webapps/webview.html#BindingJavaScript

like image 573
indusBull Avatar asked Jul 20 '11 19:07

indusBull


People also ask

How to create a native interface for a WebView?

First, in your native code, you need to define the “native interface” that will be exposed to JavaScript code inside the WebView: The fact that this interface requires Context is just an example, but @JavascriptInterface annotation is mandatory. It basically designates the methods that JavaScript code will be able to “see”.

How do I add JavaScript to Android WebView?

Binding JavaScript to Android WebView allows you to bind JavaScript code to Android code through an interface. To do this, we must use the addJavaScriptInterface () method, which is passed the class that provides the interface for JS, and the name that will be used to display the instance in JS (for example, “ AndroidFunction “).

How do I enable JavaScript on a webviewclient?

To use JavaScript, you need to enable it by calling the setJavaScriptEnabled () method on the WebSettings object. WebViewClient is called when page content is being rendered. You can also intercept the URL loading here (using the shouldOverrideUrlLoading () method).

How to enable JavaScript on a destroyed WebView?

If the WebView has been destroyed, any call to the WebSettings method will raise an IllegalStateException. To use JavaScript, you need to enable it by calling the setJavaScriptEnabled () method on the WebSettings object. WebViewClient is called when page content is being rendered.


2 Answers

Use http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String) to expose Java interfaces to the JavaScript running in the WebView

like image 188
dtanders Avatar answered Oct 30 '22 06:10

dtanders


Well, if you specifically want the Camera API, it can be done. From your JS code, you can call a piece of Java code which will in turn trigger the Camera App, take a picture, return the image's URL, which your Java code can forward it to a JS method.

like image 39
Kumar Bibek Avatar answered Oct 30 '22 06:10

Kumar Bibek