Categories
Android

The services in Android

A service is a task running in the background to perform actions without a user interface and they are used to perform long running operations with a significant use of resources by avoiding delays in the user experience.
The services belong to one or both of the following categories:

  • started: the service is launched using the the method startService()
  • bound: the service is launched using the the method bindService() and the components of the application that has launched the service can communicate with the same service


Here I show a simple app to compare these two types of service:

  1. create an Android project without starting activity
  2. edit the file AndroidManifest.xml

    note the presence of two lines with service tag, you need them to enable the services to be performed and, for the service .MyStartedService, I set the attribute android:process=”:started” to specify a process associated to the service, this will allow you in the test phase to stop the service without stopping the main activity
  3. edit the files in the folder res/values/, nothing of important here
    • dimens.xml
    • strings.xml
    • styles.xml
  4. create the file res/layout/activity_main.xml

    this resource defines a layout with the following controls:

    • Started service:
      • option onDestroy: if selected the line StopSelf() is executed ant then the method onDestroy of the class MyStartedService too
      • oprions START_STICKY and START_NOT_STICK: to set the value returned from the method onStartCommand of the class MyStartedService
      • a button to launch the service “started”
    • Bound service:
      • a button to launch the service “bound”
      • a text box to display the output of the service “bound”
  5. create file eu/lucazanini/service/MainActivity.java

    this is the main activity, you see:

    • the onClick event to manage the buttons, btn1 for the “started” service and btn2 for the “bound” service
    • the code for MyHandler, ServiceConnection and separateThread is used for “bound” service only
  6. create the file eu/lucazanini/service/MyStartedService.java

    this class is the “started” service and it is called from the main activity using the StartService() method; extending the Service class you must implement the onBind method but in the case of “started” service put only the statement “return null;” because this method is never called; the job of the service is in the onStartCommand() method, the code line “Utils.test(TAG);”
  7. create the file eu/lucazanini/service/MyBoundService.java

    this class is the “bound” service and it is called from the main activity using the StartService() method; the onBind method has been overridden to return the object mBinder of the inner class LocalBinder that extends Binder
  8. create the file eu/lucazanini/service/Utils.java

    this is a class with static methods doing the job of the services

service

And now let’s test the app:

  1. launch the app
  2. leaving the defalt options launch the service “started” with th first button “Launch”
  3. see the output in the LogCat view that shows the time for 10 seconds and the call to onDestroy
    the service is started using the code startService() in the main Activity, the argument in startService is an Intent for the class MyStartedService that extends the class Service, the work done by the service is contained in the method onStartCommand()
  4. unselect onDestroy and launch again the service “started”, the call to onDestroy doesn’t happen, this means that, even if the service has completed to display the time for 10 seconds, the service is not destroyed
  5. stop the service, you can do it in two ways:
    • from shell: with the comand “adb shell kill [PID]” where [PID] is the PID associated to the service and you can read in the view LogCat in Eclipse
    • dalla view Devices in Eclipse: selecting the process eu.lucazanini.service:started and stopping it with the icon “Stop Process”
  6. wait some seconds and verify that the service restarts
  7. select START_NOT_STICKY and restart the service
  8. stop the service and verify that the service doesn’t restart

This test should make clear the difference between START_STICKY and START_NOT_STICKY in the case in which the service is stopped:

  • START_STICKY: the service restarts if it is stopped before the call to onDestroy
  • START_NOT_STICKY: the service doesn’t restart if it is stopped before the call to onDestroy but it can restart with the code startService()

For more information about the differences between the two parameters you can read Service API changes starting with Android 2.0.

Now let’s test the app for the service “bound”:

  1. launch the app
  2. push the second button to launch the service “bound”
  3. see the output in the text view; the text box is updated every second for 10 seconds displaying the current time without locking the device

You can download the project here.

References:
Android Developers: Service
Android Developers: Services
Android Developers: Bound Services

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.