Saturday, December 3, 2011

How javascript call java in Phonegap android

Phonegap is a great mobile solution, since I am working in Grapple mobile now, our platform use quite similar solution with Phonegap, so I really want to learn Phonegap to see if we can steal some ideas from it.
What troubled me a long time is in its android platform: I wonder how does its javascript code calls java native code? Before I tried to read its android source code, but did not find the answer. This week I spent some time on its source code, finally I find the answer. I would like to share this result with you, this might be valuable especially if you want to dig more to phonegap, since I seldom find the answer from google or books about phonegaps, none of them talks about this.

The phonegap source code I checked out is from github, the current version is 1.2:
1. phonegap android source code:
2. phonegap platform sample code:

Digging the source code
From phonegap-1.2.0.js

For example looking at accelerometer:  in javascript layer, the accelerometer is implemented by calling Phonegap.exec() method:
The PhoneGap.exec() method will be like this: So the exec method will invoke a javascript prompt()- which is popping up an dialog, huh, it sounds weird.

Let's look at the java code:
Inside, find the inner class GapClient, which is a subclass of WebChromeClient, it override the following 3 methods: onJsAlert(), onJsConfirm() and onJsPrompt()
Here we only look at the onJsPrompt(), look at the android API for onJsPrompt():
Tell the client to display a prompt dialog to the user. If the client returns true, WebView will assume that the client will handle the prompt dialog and call the appropriate JsPromptResult method. If the client returns false, a default value of false will be returned to to javascript. The default behavior is to return false.

So it is quite clear that: if prompt() is called in javascript layer, then onJsPrompt() method will get invoked in java layer. Phonegap just uses this way to handle the javascript to java communication.

Let's look at the onJsPrompt() method in details:  So here the phoneGap PluginManager will delegate the the javascript call to different native plugins.

From the article of
and phonegap release note:, it mentioned that
Worked around JavaScript bridge exception for Android 2.3. Use “prompt” instead of calling objects directly.
So it seems that this is a workaround for an issue in Android 2.3.

To recap, this is the flow of javascript call java in phonegap android is like this:
1. in javascript layer, all the javascript method will finally delegate to Phonegap.exec() method
2. PhoneGap.exec() will call prompt()
3. In java layer, DriodGap.GapClient override onJsPrompt(), which will capture the prompt() method call in java, which then delegate to PluginManager to invoke the native java method call.


  1. There is a post from PhoneGap that helps to solve similar problem as yours. Please take a look at

  2. Heya¡­my very first comment on your site. ,I have been reading your blog for a while and thought I would completely pop in and drop a friendly note. . It is great stuff indeed. I also wanted to there a way to subscribe to your site via email?

    Agile Coach Training

    1. Hi, Ayshwariya,
      Thanks for your interest. I think all you need is just click the "Join this site" button,
      or usually I use google reader, you can add my blog url in your google reader list.

  3. Very nice post!!! Thank You for Sharing.

  4. More and more companies are trying to get nimble to enable them to respond to change with agility. Over the years, there has been a clear shift in momentum about the ways how companies manage projects. So, the project manager should be a PMP certified, who can better handle the planning, execution, and closing of any project. To get yourself prepared for PMP Certification , is the best source.

  5. The SBOK guide of will give you a clear understanding of how to run effective daily standup meetings. It also provides you detailed information on Agile Project Management methodologies such as planning/review/retrospective meeting, and how to take advantages of related tools and so on.