Android Notes: Services

ShahRukh
4 min readJul 6, 2018

The primary role of a service is as a flag to the operating system, letting it know that your process is still doing work, despite the fact that it is in the background.

  • A service runs in your application’s main thread so any sort of blocking operations should be done by making and managing new threads within the service.
  • They don’t have any user interface and can perform a wide range of tasks like downloading big chunk of data, maintaining a constant connection regardless of the user is using the app (telephony or chat), play music or doing periodic updates all while running the background.
  • A component can bind itself to a service (using onBind()) to interact with it and even perform inter-process communication (IPC).
  • Services are created when manually started with startService()or when some activity tries connecting to the service IPC.
  • You have to subclass Service class or should use one of its existing subclasses like IntentService.
  • Will live as long as they are not specifically shut down or the system is not desperate for RAM.
  • The call to startService() will be asynchronous so the calling component won’t be blocked. The service will be created if it’s not already running and the onStartCommand() method will receive an intent (while running on the main thread).
  • onStartCommand() will return one of several predefined constants, indicating what will happen should the service be killed by the system. The START_STICKY means the service will be restarted (as if onStartCommand() is called) but without the original intent. The START_REDELIVER_INTENT means the service will be restarted (as if onStartCommand() is called) with the original intent. The START_NOT_STICKY means the service will remain in the stopped state until started again with startService().
  • stopService() doesn’t employ any sort of reference counting. So three calls to startService() will result in one running service which will be stopped on one call to stopService().

Categorization of Services

  1. Foreground: The services which are used to do some work in response to the user’s interaction falls into this category. These services can stay running even when the user isn’t interacting with the application. For example, a music app uses a service to play music and won’t stop even when the user switches to some other application.
  2. Background: A background services perform its work which is not exactly noticeable by the user in the foreground. For example, a service can be used to perform some maintenance or synchronization work in the app’s database.
  3. Bound: A service which is bounded by some other Android component is called a bound service. Any number of components can bound themselves to a single service. These type of services will live as long as they are bounded to some component. Examples include a music player or a chatting app.

Important methods

If you are going to subclass Service class, the most important methods that you should override:

  • onCreate(): This method will be called just one-time to set up this service and before the onStartCommand() and onBind() methods.
  • onStartCommand(): System will call this method as a result of a component calling startService() method. When this method executes, the service will keep on running in the background and it is your responsibility to stop the service when its work is complete by calling stopSelf() or stopService(). If you only want to provide binding, you don't need to implement this method.
  • onBind(): This method will be called when a component is bound to this service by calling bindService(). If a component call bindService() to create a service, the onStartCommand() will not be called.
  • onDestroy(): Called when the system is about to destroy the service. You should do the cleanup here.

Service declaration in the manifest

Add a <service> element as a child of <application> element in your manifest. You can also set android:exported as false in service element to keep it usable only by your app.

The lifecycle of a Service

Just like an activity, a service also has a lifecycle but unlike activity, you don’t require to call the superclass method for each overridden method.

(On left) Lifecycle when the service is created using startService() and (On right) lifecycle when the service is created using bindService().

No matter how a service is started, it can still allow a client to bind to it. A service that’s started with startService() can still receive a call to onBind() .

Please let me know if there’s any correction or improvement needed in these notes. Your feedback will be highly appreciated.

--

--

ShahRukh

The Android guy @ Jodel | Android Apps | SDK | Shutterbug | Dreamer | Thinker www.shahrukhamd.com