Skip to content

Using the MyService Sample

Mark Taylor edited this page Jan 4, 2014 · 9 revisions

To demonstrate the use of the background service, I provide the MyService plugin sample.

The sample can be installed using the following command (this assumes you are familiar with the [Cordova Android Getting Started] (http://docs.phonegap.com/en/3.3.0/guide_platforms_android_index.md.html#Android%20Platform%20Guide)):

You should be able to run this example.

Java part explained

The src\com\red_folder\phonegap\plugin\backgroundservice\sample\MyService.java provides the background service.

This class extends the com.red_folder.phonegap.plugin.backgroundservice.BackgroundService class.

The class holds a property to store who it is we are saying hello to (note that in production code, this should be stored in a persistent store):

private String mHelloTo = "World";

The class allows the mHelloTo be set via the setConfig:

@Override
protected void setConfig(JSONObject config) {
   try {
      if (config.has("HelloTo"))
         this.mHelloTo = config.getString("HelloTo");
      } catch (JSONException e) {
      }
   }
} 

The class allows the mHelloTo value to be provided to the HTML/ Javascript Front-end:

@Override
protected JSONObject getConfig() {
   JSONObject result = new JSONObject();
	
   try {
      result.put("HelloTo", this.mHelloTo);
   } catch (JSONException e) {
   }
	
   return result;
}

The class provides the background service logic in the doWork:

@Override
protected JSONObject doWork() {
   JSONObject result = new JSONObject();
		
   try {
      SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
      String now = df.format(new Date(System.currentTimeMillis())); 

      String msg = "Hello " + this.mHelloTo + " - its currently " + now;
      result.put("Message", msg);

      Log.d(TAG, msg);
   } catch (JSONException e) {
   }
		
   return result;	
}

The Plugman logic automatically adds the service into the Android Manifest (AndroidManifest.xml):

<service android:name="com.red_folder.phonegap.plugin.backgroundservice.sample.MyService">
   <intent-filter>
      <action android:name="com.red_folder.phonegap.plugin.backgroundservice.sample.MyService" />
   </intent-filter>
</service>

Javascript part explained

The assets/www/plugins/com.red_folder.phonegap.plugin.backgroundservice.sample/www/myService.js provide the Javascript interface for the MyService background service.

The core Javascript logic is provided by the background service core logic. This file extends that for the specific background service.

var serviceName = 'com.red_folder.phonegap.plugin.backgroundservice.sample.MyService';

var factory = require('com.red_folder.phonegap.plugin.backgroundservice.BackgroundService')
module.exports = factory.create(serviceName);

The Plugman logic automatically adds the plugin into the Cordova config (res/xml/config.xml):

<feature name="BackgroundServicePlugin">
   <param name="android-package" value="com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePlugin" />
</feature>

HTML part explained

Clone this wiki locally