Services



What are services? 

A service is a component which runs in the background, without direct interaction with the user. As the service has no user interface it is not bound to the lifecycle of an activity.

Services are used for repetitive and potential long running operations, like Internet downloads, checking for new data, data processing, updating content providers and the like.

Services run with a higher priority than inactive or invisible activities and therefore it is less likely that the Android system terminates them. Services can also be configured to be restarted, if they get terminated by the Android system once sufficient system resources are available again.

It is possible to assign services the same priority as foreground activities. In this case it is required to have a visible notification active for the related service. It is frequently used for services which play videos or music.

A service is a component that runs in the background to perform long-running operations without needing to interact with the user. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity.

A service has lifecycle callback methods that you can implement to monitor changes in the service's state and you can perform work at the appropriate stage. The following diagram on the left shows the lifecycle when the service is created with startService() and the diagram on the right shows the lifecycle when the service is created with bindService(): 


 
 




Callback
Description
onStartCommand()
The system calls this method when another component, such as an activity, requests that the service be started, by calling startService(). If you implement this method, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService() methods.
onBind()
The system calls this method when another component wants to bind with the service by calling bindService(). If you implement this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder object. You must always implement this method, but if you don't want to allow binding, then you should return null.
onUnbind()
The system calls this method when all clients have disconnected from a particular interface published by the service.
onRebind()
The system calls this method when new clients have connected to the service, after it had previously been notified that all had disconnected in itsonUnbind(Intent).
onCreate()
The system calls this method when the service is first created usingonStartCommand() or onBind(). This call is required to perform one-time setup.
onDestroy()
The system calls this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc.



Services and background processing :

A service runs by default in the same process in the main thread as the application.

Therefore you need to use asynchronous processing in the service to perform resource intensive tasks in the background. A common used pattern for a service implementation is to create and run a new Thread in the service to perform the processing in the background and then to terminate the service once it has been finished the processing.

Services which run in the process of the application are sometimes called local services.

 
Platform service and custom services :

The Android platform provides and runs predefined system services and every Android application can use them, given the right permissions. These system services are , usually exposed via a specific Manager class. Access to them can be gained via the getSystemService() method. The Context class defines several constants for accessing these services.


An Android application can, in addition to consuming the existing Android platform services, define and use new services. Defining your custom services allows you to design responsive applications. You can fetch the application data via it and once the application is started by the user, it can present fresh data to the user.


Defining custom services :

A service needs to be declared in the AndroidManifest.xml file and the implementing class must extend theService class or one of its subclasses.


<service
  android:name="MyService"
  android:icon="@drawable/icon"
  android:label="@string/service_name"  >
</service>


 
public class MyService extends Service {

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful
    return Service.START_NOT_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
  //TODO for communication return IBinder implementation
    return null;
  }



Start a service :

An Android component (service, receiver, activity) can trigger the execution of a service via thestartService(intent) method.


// use this to start and trigger a service
Intent i= new Intent(context, MyService.class);

// potentially add data to the intent
i.putExtra("KEY1", "Value to be used by the service");
context.startService(i); 


Service start process and execution :

If the startService(intent) method is called and the service is not yet running, the service object is created and the onCreate() method of the service is called.

Once the service is started the startService(intent) method in the service is called. It passes in the Intentobject from the startService(intent) call.

If startService(intent) is called while the service is running, its onStartCommand() is also called. 

Therefore your service needs to be prepared that onStartCommand() can be called several times.


Service restart behavior :

In its onStartCommand() method call, the service returns and int which define its restart behavior in case the service gets terminated by the Android platform.

 
Stopping a service :

You stop a service via the stopService() method. No matter how frequently you called thestartService(intent) method, one call to the stopService() method stops the service.
A service can terminate itself by calling the stopSelf() method.

No comments:

Post a Comment