PhoneGap – Getting Started Events and Plugin

In this blog post we will look into phonegap plugins and events.

PhoneGap Plugins

Phonegap’s entire framework is divided into phonegap core and phonegap plugins. Phonegap is a very small section which mainly provides API to create phonegap plugins. Plugin are the main ecosystem of phonegap which provide all the native functionality and the javascript bridge. So when ever you use phonegap, you add a number of plugins which your app needs. So for every native feature like GPS, Network, Push Notifications, Facebook, etc there are plugins which you need to add.

Here are few popular plugin registries where you will find all phonegap plugins
http://plugins.cordova.io/#/viewAll
http://plugreg.com/plugins#

To add a plugin to your phonegap application, go to your phonegap project and then

phonegap plugin add org.apache.cordova.device

to remove a plugin

phonegap plugin remove org.apache.cordova.device

to list all existing plugins

phonegap plugin list

So to summarize, phonegap’s main power lies in its plugin ecosystem and there are many plugin’s which you can use to access phone’s native features.

PhoneGap Events

Phonegap exposes certain important events relating to device state. Lets look into some of these events
deviceready
Device ready is an event fired by cordova, when the cordova api ready and can be accessed. It equivalent to document.ready in jquery. More details can be here
To use it

document.addEventListener("deviceready", function(){
   console.log('device ready');
   // place your phonegap related code in here
}, false); 

So it’s important to note that all phonegap related javascript function should be called inside deviceready.
There are many such events which can be seen here http://docs.phonegap.com/en/edge/cordova_events_events.md.html#Events, important ones being pause, resume, backbutton, online, offline.