Dual life of a service

Tutorials with advanced 'difficulty' and more Lines of Code.

Dual life of a service

Postby paller » Sun Feb 03, 2008 11:11 am

Dual life of a service

Click here for the cross-linked version of the entry.

Earlier (with much less experience in Android services) I wrote about the two kinds of services behind the android.app.Service abstraction: a long-running service whose lifecycle is independent of the activity that started it (Context.startService) and a service bound by other services and activities by its AIDL interface (Context.bindService). The lifecycle of this second type of service depends on the lifecycles of the entities that bind it, if the service has no more bound client, the application manager is free to destroy it. Meanwhile, the first type of service exists until the clients that started the service stop it or until the service stops itself. In extremely low resource conditions the application manager may shut down the first type of service too but this is rare.

I don't know how about you but the difference between the two types always seemed to be unnatural for me. For starter, I can pretty much imagine a number of useful and practical services that are long-running and clients want to communicate with them at the same time. For example a P2P module would be such a service; it would run in the background doing discovery-related or other long-running tasks and occassionally client applications would connect to it to accomplish P2P operations.

It turns out that this difference is artificial. Android services can exhibit both properties: they can be long-running and can be bound and unbound with AIDL interface operations. This is possible because there is only one service instance, independently of the method the service was started with. I discovered it recently and as there was a topic about it in the Android groups, I decided to quickly put together an example program about it.

You can download the example program from here.

Our example is simple. There is a service that holds a counter and can return that counter by means of an AIDL interface. The service need to be bound by Context.bindService() to obtain client-side interface stub. This is not very useful, however, because the counter does not change, it needs to count forward. In order to do that, we start the same service with Context.startService(). It turns out that the onStart() that follows is invoked on the same service instance. Hence we have a service that conforms to both types: it is long-running and serves interface invocations at the same time.

Simple? I thought so. I implemented a version of the example program and it worked flawlessy except that the service could not be stopped. It turns out that a dual service like that can be stopped with Context.stopService() call only if both lifecycles models allow it. This means that stopService() has no effect on a bound service!

Let's take an example. The client activity allows binding/unbinding, starting/stopping the service manually.


Now let's see what happens if the service is bound, unbound then started and stopped.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. D/DUALSERVICECLIENT( 568): bindService()
  2. D/ActivityThread( 568): Creating service aexp.dualservice.DualService
  3. D/DUALSERVICE( 568): onCreate
  4. D/DUALSERVICECLIENT( 568): onServiceConnected
  5. I/ActivityManager( 465): Stopping service: {aexp.dualservice/aexp.dualservice.DualService}
  6. D/DUALSERVICECLIENT( 568): unbindService()
  7. D/ActivityThread( 568): Stopping service aexp.dualservice.DualService@40044928
  8. D/DUALSERVICE( 568): onDestroy
  9. D/DUALSERVICECLIENT( 568): startService()
  10. D/ActivityThread( 568): Creating service aexp.dualservice.DualService
  11. D/DUALSERVICE( 568): onCreate
  12. D/DUALSERVICE( 568): onStart
  13. I/ActivityManager( 465): Stopping service: {aexp.dualservice/aexp.dualservice.DualService}
  14. D/DUALSERVICECLIENT( 568): stopService()
  15. D/ActivityThread( 568): Stopping service aexp.dualservice.DualService@40048cd0
  16. D/DUALSERVICE( 568): onDestroy
Parsed in 0.013 seconds, using GeSHi

The service lifecycles work nicely: when the service is bound, its onCreate is called, then it is destroyed (onDestroy()) when it is unbound. The same goes for startService-stopService except that onStart() is called after onCreate(), as it should happen.

Now let's see what happens if the sequence is bind-start-stop-unbind!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. D/DUALSERVICECLIENT( 568): bindService()
  2. D/ActivityThread( 568): Creating service aexp.dualservice.DualService
  3. D/DUALSERVICE( 568): onCreate
  4. D/DUALSERVICECLIENT( 568): onServiceConnected
  5. D/DUALSERVICECLIENT( 568): startService()
  6. D/DUALSERVICE( 568): onStart
  7. D/DUALSERVICECLIENT( 568): stopService()
  8. I/ActivityManager( 465): Stopping service: {aexp.dualservice/aexp.dualservice.DualService}
  9. D/DUALSERVICECLIENT( 568): unbindService()
  10. D/ActivityThread( 568): Stopping service aexp.dualservice.DualService@4004a430
  11. D/DUALSERVICE( 568): onDestroy
Parsed in 0.011 seconds, using GeSHi

ActivityManager reports twice that the service was stopped. Of these two, only the second one is real (leads to the invocation of onDestroy()). As the service abstraction has no onStop() method (that would be counterpart of onStart()), the service is not notified that it was stopped. You can examine that with getting the counter: it continues counting after the service was stopped.

This is annoying enough but a method like stopCounting() on the AIDL interface would solve the problem. My opinion, however, is that the lack of service stop notification will cause problems with less tricky long-running services too. For example a voice recorder service will not be able to correctly free the resources (e.g. close the output file correctly) if it is just stopped by one of its clients or if the ActivityManager shuts it down due to low resource condition. I don't know how hard it would be ti implement stop notification callback (finalizers are the toughest kind of callbacks) but this enhancement was hacked into every application model that missed it originally (like Java applications, for example).
Posts: 29
Joined: Mon Dec 31, 2007 2:33 am
Location: London


Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests