Chapter 10. Creating Android Apps with PhoneGap

In the previous chapter, we developed a mobile-friendly web application using jQuery Mobile. In this chapter, we’ll convert the very same mobile application into a native Android mobile application using PhoneGap. At first glance, converting a humble JavaScript application into a native Android application seems like a miraculous metamorphosis: native Android apps are usually written in Java, Java is not JavaScript, and you can’t convert from one to the other. How does PhoneGap manage this? In fact, PhoneGap’s apparent magical abilities lie within the Android system itself. Android provides a facility called WebView that allows native applications to display regular web content. This includes the ability to execute JavaScript within the web content as normal.

One exciting feature of WebView is that it also allows interaction between a native Android application and the web content within the WebView. This is extremely useful, both for Android developers wanting to display and interact with web content within their applications and for web developers wanting to take advantage of Android device features like cameras and accelerometers. Essentially, PhoneGap is an Android application that uses a WebView for your web content, and also provides a JavaScript library for accessing some of the facilities of the Android device itself.

Other flavors of PhoneGap work in a similar way. For example, the Apple iPhone version of PhoneGap uses ...

Get Supercharged JavaScript Graphics now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.