This project demonstrates how to build and use a simple PhoneGap plugin for Android.
The plugin simply returns the phone’s number as a string.
Start by creating a PhoneGap project (we assume PhonGap version >= 1.0) in Eclipse, or using the PhoneGap Eclipse wizard to create one.
To implement your own plugin, follow these 4 steps:
- Create the plugin – add a class to your Java project (see MyPhoneGapPlugin.java)
- The class extends the PhoneGap Plugin class
- The function you need to implement is public PluginResult execute(String action, JSONArray data, String callbackId)
- Add to plugins file – add a line that includes the full path of your class to the plugins file (see plugins.xml)
- Create a JavaScript interface – implement a JavaScript interface to the plugin (see myphonenumber.js)
- Notice that it expects an oSuccess and onError callback functions
- No need for it to be too elaborate – contain your actual logic in the Java code, and any data preparation to the calling function
- Call your plugin – (see index.html)
- include the js file in the HTML head section
- I find it easier to create a single function that contains both oSuccess and onError callback functions, and the actual call
And that’s it. Again, if you keep the logic in the actual plugin Java code, you can more or less reuse this framework as is, while doing more in Java.