Creating a Simple Android Service for Background Processing

So here’s one that I’ve been looking forward to trying out, creating a service that can run in the background and do things while the user is doing something else.  There are actually two ways of using a service in Android. One involves binding the service to an activity, and the other is more of a traditional service that just runs in the background independently. I am going to focus on the latter for this post. Basically, you have to create a Java class that extends android.app.Service, implement the startup method, and you’re good to go.  Here is the required code:

[crayon lang="java"]
package net.kgmoney.ServiceTemplate;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class ServiceTemplate extends Service {

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
//code to execute when the service is first created
}

@Override
public void onDestroy() {
//code to execute when the service is shutting down
}

@Override
public void onStart(Intent intent, int startid) {
//code to execute when the service is starting up
}
}
[/crayon]

And in your application’s manifest file, in the application section…
[crayon lang="xml"]

[/crayon]
You’ll notice the onBind method. This is actually the only required, but it is used for binding the service to an activity, which we aren’t going to do. The methods we care about are onCreate, onDestroy, and onStart. These are really pretty self explanatory, but I’ll say a bit anyway. The onCreate method executes only on the initial creation of the service which happens on the first call to Context.startService(). Once the service has been created, onCreate will not fire again regardless of how many times Context.startService() is called. The method onStart, however, is called each time Context.startService() is called. Finally, onDestroy is called when the service is stopped.

NOTE: onStart is actually deprecated and you should use onStartCommand when using API levels 5 and above.

So, now you’ve taken the template service and added code to make it do something useful and you want to call it. This requires the creation of an Intent. There are two situation I’ve had so far that I wanted to start a service. 1) from a button in an Activity and 2) from a BroadcastReceiver. Here are examples of each:

[crayon lang="java"]
//this is a button handler for a button that is on an Activity
btnStartService.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(), ServiceTemplate.class);
myIntent.putExtra(“extraData”, “somedata”);
startService(myIntent);
}
});
[/crayon]
…or…
[crayon lang="java"]
//this is starting the service from a BroadcastReceiver
Intent myIntent = new Intent(context, ServiceTemplate.class);
myIntent.putExtra(“extraData”, “somedata”);
startService(myIntent);
[/crayon]
Basically identical, except for the context argument when creating the Intent. The reason I’m showing both is because when I first was trying this, I was specifically looking for how to start a service from a BroadcastReceiver. Also, take note that you can use Intent.putExtra to pass arbitrary data to the service.

Tags: , ,

16 Responses to "Creating a Simple Android Service for Background Processing"

Leave a Comment