Service Tutorial in android Part-I


A service is a task that runs in the background without the user’s direct interaction, similar to a Unix daemon. For example, consider a music player. The music may be started by an activity, but you want it to keep playing even when the user has moved on to a different program. So, the code that does the actual playing should be in a service. Later,another activity may bind to that service and tell it to switch tracks or stop playing. Android comes with many services built in, along with convenient APIs to access them.

      A Service is a component which runs in the background, without interacting with the user. Every developer can create new Services in his application. Services support true multitasking for Android, as they can run in their own process. If you use threads in Activities their are still connected to the life-cycle of Activities and the Android system may decide to terminate them at any point in point.

For information about services:

The following topics are covered in this topic.

  1. Local Services
  2. Service using Binder
  3. Service using Messanger
  4. Serivice using Remote AIDL files
  5. Intent Service

1. Local Services:

We can start the service in two ways .

step 1:
Intent intent=new Intent(context,ServiceClassName.class);
step 2:
Intent intent=new Intent(“com.ramesh.myservice.ACTION”);

When we start service using step 2, make sure that adding the action in manifest file.

<service android:name="BindingService" >
 <action android:name="com.ramesh.myservice.ACTION" >

 public class SimpleService extends Service
 public IBinder onBind(Intent arg0)
 return null;
 public void onCreate()
 Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
 public void onDestroy() {
 Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();

In Activity take two buttons for testing , start the service with one button and stop the service with another.

You can choose one of the two steps to start the service.

2. Services using binder:

In Activity we are going to the bind service to communicating with it.

1. In oncreate method, we are binding the service using following statement.

bindService(new Intent(this, BindingService.class), conn,

2.Make sure that unbind the service connection  in onDestroy method Otherwise it gives service connection leaked exception.


3.When create the IBinder in service, its better to use WeakReference.This bug is mentioned in this blog.

private MyBinder mService;

 private ServiceConnection conn = new ServiceConnection()
 public void onServiceDisconnected(ComponentName name)
 //service is disconnected!

 public void onServiceConnected(ComponentName name, IBinder service)
 //Service is connected.
 mService = (MyBinder) service;


public class BindingService extends Service {

public static final String MY_CUSTOM_ACTION = "com.ramesh.myservice.ACTION";
 private MyBinder<BindingService> binder;
 private String name = "Hi";

 public IBinder onBind(Intent intent) {
 binder = new MyBinder<BindingService>(this);
 return binder;

 public int onStartCommand(Intent intent, int flags, int startId) {

public class MyBinder<T> extends Binder implements IMybinder {
 private WeakReference<T> mService;

public MyBinder(T service) {
 mService = new WeakReference<T>(service);

public T getService() {
 return mService.get();

 public String getName() {

Random r = new Random();
 return name + "=> your lucky number is:" + r.nextInt(100);

 public void setName(String newname) {
 name = newname;

 /** Interface setting the name and getting the name.**/

 public interface IMybinder
 public String getName();

 public void setName(String name);

Continue to be service tutorial-II…..


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s