Skip to content

services

I-Love-Github edited this page Feb 20, 2015 · 11 revisions

Android Services

Android services are background processes. They are designed to handle long-term or continuous operations that would cause the user interface to hang (the primary activity thread is also the UI thread). They can be

Lifecycle

Services can run persistently or as one-shot functions. They can interact with no activities/classes, or they can talk to several at a time. This makes their lifecycle and implementation more complicated. Here are some flowcharts that represent three possible service lifecycles:

1: Activity calls startService()

2: Activity calls bindService()

3: Activity calls startService(), then binds to the service with bindService()

Communication

Two ways of communicating with services are through Intents and Messages -- this wiki will not cover these methods, but they are well documented in the Android API.

For our purposes, the most convenient way of interacting with a service is by binding our activity to it using bindService(). This gives our activity access to that service's public methods. If we pass a reference of our activity to the service, we get direct two-way communication between the service and our activity.

Always call onUnbind() and stopService() in the onStop() method of your activity. This ensures that services do not run persistently and waste user resources. Eventually, the Android system will remove the service to free up resources, but we're responsible adults and should not rely on the system to handle our garbage collection.

Persistence

Services created with onBind() only persist as long as activities are bound to it. Once all activities are unbound, it will self-destruct.

Calling stopService() from an activity or stopSelf() from within the service will also cause it to self-destruct.

Services created with onStart() will persist until the system destroys it to free up resources. If this is not desired, there are three levels of persistence that can be used during the creation process:

START_NOT_STICKY: Once the service is destroyed, it will not restart automatically. It is up to the activity to recreate it.

START_STICKY: Once the service is destroyed, the system will automatically restart it and call onStartcommand(). This will allow the service to effectively run forever.

START_REDELIVER_INTENT: This wiki is not covering intent-driven services, but this persistence option redelivers the last intent in addition to restarting it whenever it is destroyed.

Android Manifest

Just like activities, services must be declared in the AndroidManifest.xml file:

<manifest ... >
  ...
  <application ... >
      <service android:name=".ExampleService" />
      ...
   </application>
</manifest>

Sample Code

If you're just here for some sample code. Refer to this example project to see a working example of how services work.

Helpful Links

Android API: Services Android API: Bound Services Android API: Messages Stackoverflow: Getting an Android Service to Communicate With an Activity Stack Overflow: How to Get an Android Bound Service to Survive a Configuration Restart

Clone this wiki locally