The Core Plugin are files and configuration that will be needed for any implementation that uses the Plugin.
7) Ensure that following are in the AndroidManifest.xml. This line sets permission so that the Background Service can be restarted on device boot.
8) Add the BootReceiver detail to the AndroidManifest.xml. This should be added within the Application node.
9) Your manifest file should look similar to the below:
This completes the steps to install the core Plugin. The next steps are for specific Background Service.
Install the Background Service
The files and setup for a Background Service will be unique to the service. In this instance we are putting in MyService which will output a hello message to the LogCat.
1) Create a Package called "com.yournamespace.yourappname" (if you don't already have)
2) Copy the MyService.java file into src\com\yournamespace\yourappname folder (and refresh the project):
3) Cordova 1.81 > Copy the myService.js and index.html files into assets\www folder:
or
Cordova 2.x.x > Copy the myService-2.0.0.js and index-2.0.0.js into assets\www folder:
4) Add the following lines to the AndroidManifest.xml. This should be added within the Application node:
5) Your manifest file should look similar to the below:
6) Cordova 2.0.0 only > Update the MainActivity to load index-2.0.0.html rather than index.html.
The application is now good to go.
Running the application
From here you can:
- Start & Stop the Background Service.
- Start & Stop the Timer. Note that the MyService is set to run every minute, but will only write to the LogCat when the Timer is enabled.
- Register/ De-register for Boot. If registered for boot, the service will automatically restart when the device is restarted, If not registered, then you will need to run the app again to start the background service.
- Update the Config. In this simple example, you can set the "Hello To" property of the service.
- Refresh the latest result - displays the latest result to screen.
When the service is running (and the timer is enabled), you should get messages similar to the below in the LogCat:
Next Steps
In coming blogs I shall explain the Background Service Plugin in more detail as well a take you through the steps of extending the Plugin yourself.
I really hope this helps. I look forward to comments.
Spread the love
If you find the Background Service Plugin useful then spread the love.
All the work I do on the Plugin is done in my spare time - time I would otherwise be spending taking my wife out for a nice meal or helping my lad find vinyl records (he's currently very much into The Smiths, Fleetwood Mac and Kate Bush).
The Plugin is free and will always remain free. I will continue to develop, maintain and distribute the Plugin under the MIT License.