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
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”.
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 “).
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).
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With