This blog is a continuation of the Service Tutorial.
In Part 1 we created a simple application using Phonegap. In this part we will add the code for our skeleton service.
As per Part 1 of this tutorial, I am using Cordova 1.8.1. Any changes for Cordova 2.x.x have been annotated.
Download the Plugin files and add the following to the project:
Instructions on downloading these file (and where to put them in you project) can be found in this blog.
Add a new class within the src\com.redfolder.sample folder. Set the Name of the class to "TwitterService" and the superclass to "com.redfolder.phonegap.plugin.backgroundservice.BackgroundService":
If you have the Inherited abstract methods, your class should look like this:
If Eclipse hasn't automatically created the abstract methods, then copy them in now.
Let's go over the abstract methods:
We are going to add two lines to this class (which is all we will need for this part):
This just gives us a tag to use in Log calls.
Now lets log a message, update doWork() with a log message:
This completes the work on the class for this part of the tutorial. The rest of this blog will describe plumbing the class in to your Phonegap application.
Add the following to your AndroidManifest.xml. It should be placed within the Application node.
For a Background Service to be available to an Android Application it must be registered within the manifest. If you are creating an Application for multiple Background Service, you need to create a Service entry for each one.
Add the following line to res/xml/plugins.xml (for Cordova 2.x.x, add to res/xml/config.xml):
This registers the Plugin for use by Cordova/ Phonegap. Note that we are registering the Plugin, which is generic regardless of how many Background Services your application may have.
Create a new text file of "twitterService.js" within assets\www, and copy in the following code:
And now we plumb our service into our index.html. Copy the following code into the head section of index.html:
Phonegap plugins have a fairly standard mechanism of providing callback functions for success and error. In this example, we waterfall through the functions to start the service (if not already started) and enable the timer (if not already enabled).
The Background Service must be started before you can perform many functions. Once the service is started, you need to explicitly enable the timer. The enableTimer method takes an extra parameter which is the timer interval in milliseconds - in this case we are setting it to 60 seconds.
You should now be able to finally re-compile and run the application. If all works, you should see the logcat message every 60 seconds:
If you close the Phonegap Application, you will continue to see the logcat message every minute. You should be able to start and stop the Application and the logcat will continue.
The only time that logcat will stop is if you either stop the service (via Settings) or if you reboot the device (or emulator).## Handing device reboot Most of the times we will want our Background Service to automatically restart when our device is rebooted.
To do this we need to add the following to the AndroidManifest.xml:
Add this within the Application node. This block only needs to be added once regardless of the number of Background Services in your application. This code tells Android that on boot, to call the special plugin BootReceiver class which will automatically start any Background Service registered for boot start.
The logcat messages should now survive device reboot.
This concludes this part of the tutorial. In the third and final part we take the skeleton service we created and build some meaningful functionality.