Friday, 18 March 2011

Android service - The one who is hidden

Unlike activities in Android, services run silently in background.

Because they have higher priority than inactive activities, it is perfect bind them to the application's component and make the application continue runs and responses the user's action in the background.

Note: One thing that should be take account is that the service runs in the main thread. If you are running a cpu-intense process or blocking operation, it could cause the application cannot response correctly the user and Application Not Responding (ANR) errors. That's why the services should run in background

Starting a service
There are two ways to start a service
  • Bounded: In this case, the bindService() method can be used to "bound" a service. The service could be run as long as the component which has bind it is still living.
    • The method onBind() should be implemented.
  • Started: the service is "started" using startService() method. In this case, the service will runs almost indefinitely, even when the component which has started it has been destroyed (see note 1). It is useful when not result is required. The unique case when the service will stop is when the service calls to stopSelf() method to finish itself.
    • The method onStartCommand(Intent, int, int) should be implemented.
 It is also possible combining bounded services and started services. You can start a service first and then bind it or you can bind a service and later call it with startService() method. While both method onStartCommand (Intent, int, int) and onBind() are present in the service, it allows you to do both things

Services Lifecycle
This is the lifecycle for "pure" services. On the left there is the lifecycle of "Started" service and on the right there is the lifecycle of "Bunded" service.


From this point, this post will be split into two parts:
1. Bounded service

2. Started service

Regarding the persistence of a service
  • The order of importance is
    • On screen process > service > not on screen process
  • In the first bounded case, the importance of a services is never less important than the importance of the most important component which is bounded.
    • For example, if there are two clients bounded to the same service
      • Component 1: visible to the user (on the screen)
      • Component 2: not visible to the user (paused or stopped)
    • The service will have the same importance as the component 1
  • A service can call the method startForeground(int, Notification) to put the service in a foreground state and thus it won't be a candidate for killing.
Note 1
So, the service will never be killed?

In the extremely low memory case, the service could be killed. But the system will try to restart it again as soon as it has enough memory. In this case, if the service should work asynchronously or in another thread, then START_FLAG_REDELIVERY should be used as the value returned by onStartCommand(Intent, int, int) method.
With this flag, you make sure that the intents of onStartCommand method will be re-delivered.

For more info:
Official guide reference:
Official dev guide:

    No comments:

    Post a Comment