1.20. Extending PhoneGap with Plug-ins

Problem

You want to add additional functionality to your application for a specific device that the PhoneGap library currently doesn’t support.

Solution

Create a custom plug-in that is added to your application and invoke it with JavaScript via the window.plugin command.

Discussion

In this example, I am going to extend the PhoneGap library by creating and using an Android plug-in with the application. The goal of this plug-in will be to read an RSS feed and parse the XML into an array of JSONObject objects that will be outputted in the existing PhoneGap application. As I mentioned in the preface, I am developing on Windows using Eclipse, so the plug-in will be for the Android only, but implementing the plug-in is platform independent because it is done via JavaScript. In other words, if you were to download an iPhone plug-in, it could be implemented in a similar fashion.

To create a plug-in, a new Android Project must be created. In Eclipse, select File New Android Project. I am going to name the project XMLParser. For the Build Target, select the latest Android API. The “Application name” will be XMLParserPlugin. I’ve created the package as com.webistrate.phonegap.plugin.xmlparser (update as needed for your package). And finally, I unchecked Create Activity.

Once the new project is added, you also need to add the PhoneGap library to your project. Just like during the initial setup, you can copy the phonegap.jar file to a new libs directory and ...

Get 20 Recipes for Programming PhoneGap now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.