With services there are 2 ways to work with :

  1. One is to use startService(Intent) to start the service and stopService(Intent) to stop the service from within your activity. With this solution you cannot communicate directly with the service, but you can use SQLITE as a layer between activities and services.
  2. The second is to use bindService() to bind the service with the activity. With this the service is alive all the time the activity that is bind to is alive. With this method you can communicate with the service directly.

I will write a simple tutorial about the second option.

I use Eclipse and ADT Plugin for Eclipse . This will do some background work.

So, let’s begin :

  1. Create a new file called : IMyService.aidl ( note the aidl extension, AIDL stand for Android Interface Definition Language ). This will containt a interface with a list of methods that you can use later to communicate with the server.
    1. package com.devicefms.android.example;
    2.  
    3. interface IMyService
    4. {
    5.         void download(String url);
    6.         int state();
    7.         String errors();
    8. }
  2. The ADT plugin will generate all the background code that you will need to use this methods.
  3. Create the service class :
    1. public class MyService extends Service {
    2.  
    3.         public void downloda(String url) {
    4.         // do something
    5. }
    6.  
    7.         public int state() {
    8.         // return service state : Started, Stopped, Buffering
    9. }
    10.  
    11.        public String errors() {
    12.        // return errors if there are any
    13. }
    14.         @Override
    15.         public IBinder onBind(Intent intent) {
    16.  
    17.                 return mBinder;
    18.         }
    19.  
    20.         private final IMyService.Stub mBinder = new IMyService.Stub() {
    21.  
    22.                 @Override
    23.                 public void download(String url) {
    24.                         MyService .this.download(url);
    25.                 }
    26.  
    27.                 @Override
    28.                 public int state(){
    29.                         return MyService.this.state();
    30.                 }
    31.  
    32.                  @Override
    33.                 public String errors(){
    34.                         return MyService.this.errors();
    35.                 }
    36.         };
    37. }
  4. The ADT plugin generated the Stub class ( see it under /gen folder )
  5. Now it’s done. Let’s go back to screen activity. Here you will have two members : the service and connection to the service
    1. private IMyService mService = null;
    2. private ServiceConnection mServiceConnection;
  6. First we create the ServiceConnection member :
    1. mServiceConnection = new ServiceConnection() {
    2.         public void onServiceConnected(ComponentName classname, IBinder service) {
    3.                        mService = IMyService.Stub.asInterface(service);
    4.  
    5.                        // only after this method is called our service is started and ready to             answer to our calls.
    6.                 }
    7.  
    8.                 public void onServiceDisconnected(ComponentName name)
    9.                 {
    10.                         mTrafficService = null;
    11.                 }
    12.         };
  7. Now let’s bind the service :
    1. boolean bound = bindService(new Intent(mContext, MyService.class),
    2.                                 mServiceConnection, Context.BIND_AUTO_CREATE);

    The last flag means that the service will be automatically created and started for us
    This method return true if the service was bound to the current activity.

  8. We are done. We can call our methods. Be careful to call them after service was successful connected.
    1. private Runnable mStartService = new Runnable(){
    2.                 @Override
    3.                 public void run() {
    4.                         if(mService  != null ) {
    5.                                if(mService.state() != Started )
    6.                                      mService.download("http://devicefms.ro");
    7.                                else
    8.                                      Toast.makeText(getBaseContext(), "Errors : " + mService.errors(), Toast.LENGTH_SHORT).show();
    9.                         }
    10.                          mHandler.postDelayed(this, DateUtils.MINUTE_IN_MILLIS * 1);
    11.                 }
    12. }
    13.  
    14. private final Handler mHandler = new Handler();
  9. Done
  10. More info on : http://developer.android.com/guide/developing/tools/aidl.html