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, implement the startup method, and you’re good to go.  Here is the required code:

  1. package net.kgmoney.ServiceTemplate;
  2. import;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. public class ServiceTemplate extends Service {
  6.    @Override
  7.    public IBinder onBind(Intent intent) {
  8.       return null;
  9.    }
  10.    @Override
  11.    public void onCreate() {
  12.       //code to execute when the service is first created
  13.    }
  14.    @Override
  15.    public void onDestroy() {
  16.       //code to execute when the service is shutting down
  17.    }
  18.    @Override
  19.    public void onStart(Intent intent, int startid) {
  20.       //code to execute when the service is starting up
  21.    }
  22. }

And in your application’s manifest file, in the application section…

  1. <service
  2.    android:enabled=“true”
  3.    android:name=“.ServiceTemplate”
  4. />

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. TheonCreate 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 aBroadcastReceiver. Here are examples of each:

  1. //this is a button handler for a button that is on an Activity
  2. btnStartService.setOnClickListener(new View.OnClickListener() {
  3.    public void onClick(View v) {
  4.       Intent myIntent = new Intent(getApplicationContext(), ServiceTemplate.class);
  5.       myIntent.putExtra(“extraData”, “somedata”);
  6.       startService(myIntent);
  7.    }
  8. });


  1. //this is starting the service from a BroadcastReceiver
  2. Intent myIntent = new Intent(context, ServiceTemplate.class);
  3. myIntent.putExtra(“extraData”, “somedata”);
  4. startService(myIntent);

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.

Leave a Reply

Your email address will not be published. Required fields are marked *