12

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

indusBull
  • 1,834
  • 5
  • 27
  • 39
  • I know little about javascript so I couldn't say for sure. But I've never come across a way that you could do that on Android. Do you already have the web app that you are loading into the WebView built? – FoamyGuy Jul 20 '11 at 20:16
  • @Tim yes we already have a webapp. But I haven't created any sample native app with WebView. I want to first know if it's possible. – indusBull Jul 20 '11 at 20:23

4 Answers4

7

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

dtanders
  • 1,835
  • 11
  • 13
  • Be very careful while using the addJavascriptInterface method. It enables javascript use the injectes objects' public fields. – crazy horse Sep 09 '13 at 17:15
1

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.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
  • I've found the solution. But still I'm interested in that "piece of javacode" that can do all camera functions for me. Or are you also talking about addJavaScriptInterface? – indusBull Jul 20 '11 at 21:12
0

See:

http://developer.android.com/guide/webapps/index.html http://developer.android.com/guide/webapps/webview.html

tamsler
  • 1,275
  • 1
  • 18
  • 26
-1

Its a complete shot in the dark on my part but these might have some useful information for you.

Webview not responding to touch events

Does Android support click events in its WebView?

Also don't forget that WebViews have javascript disabled by default you'll need to explicitly set it to true in order to make it work.

Community
  • 1
  • 1
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156