Service Tutorial in android Part-I

Service:

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:  http://developer.android.com/reference/android/app/Service.html#RemoteMessengerServiceSample

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);
startService(intent);
step 2:
Intent intent=new Intent(“com.ramesh.myservice.ACTION”);
startService(intent);

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


<service android:name="BindingService" >
 <intent-filter>
 <action android:name="com.ramesh.myservice.ACTION" >
 </action>
 </intent-filter>
 </service>

SimpleService.java:

 public class SimpleService extends Service
 {
 @Override
 public IBinder onBind(Intent arg0)
 {
 return null;
 }
 @Override
 public void onCreate()
 {
 super.onCreate();
 Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
 }
 @Override
 public void onDestroy() {
 super.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,
Service.BIND_AUTO_CREATE);

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

  unbindService(conn);

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()
 {
 @Override
 public void onServiceDisconnected(ComponentName name)
 {
 //service is disconnected!
 }

@Override
 public void onServiceConnected(ComponentName name, IBinder service)
 {
 //Service is connected.
 mService = (MyBinder) service;
 ((Button)findViewById(R.id.button1)).setText(mService.getName());
 mService.setName("Joy!");
 }
 };

/***BindingService.java:***/

public class BindingService extends Service {

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

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

@Override
 public int onStartCommand(Intent intent, int flags, int startId) {
 return START_STICKY;
 }

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();
 }

@Override
 public String getName() {

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

@Override
 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…..

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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