Sunday, March 13, 2011

Android Services and Broadcast Receiver Tutorial

Developing services with Android Gingerbread and Eclipse
This tutorial describes how to create and consume Android services. It is based on Eclipse 3.6, Java 1.6 and Android 2.3 (Gingerbread).
1. Android Services
1.1. Service

A service is a component which runs in the background, without interacting with the user.
The Android platform provides a lot of pre-defined services, usually exposed via a Manager class. In your activity you access services via the method getSystemService().
Own Services must be declared via the "AndroidManifest.xml". They run the main thread of their hosting process. Therefore you should run performance intensive tasks in the background .

1.2. Own Services

You can declare your own service to perform long running operations without user interaction or to supply functionality to other applications. A activity can start a service via the startService() method and stop the service via the stopService() method. If the activity want to interact with the service it can use the bindService() method of the service. This requires an "ServiceConnection" object which allows to connect to the service and which return a IBinder object. This IBinder object can be used by the activity to communicate with the service.

Once a service is started its onCreate() method is called. Afterwards the onStartCommand() is called with the Intent data provided by the activity. startService also allows to provide a flag with determines the lifecycle behavior of the services. START_STICKY is used for services which are explicit started or stopped. Services started with START_NOT_STICKY will end automatically after the onStartCommand() method is done. A service is started within the main thread of the application therefore all long running tasks should be performed in the background .

A Services needs to be declared in the "AndroidManifest.xml" via a <service android:name="yourclasss"> </service> and the implementing class must extend the class "Service" or one of its subclasses.

One common case is that the service and the activity are very closely related. In this case class just case the IBinder to the class the service provides. See Local Service Example for an example.
Another example would be the usage of Remote Messenger Service. In this case activities can send messages to the service and receive messages as well if they register for them.

1.3. Broadcast Receiver

A broadcast receiver is a class which extends "BroadcastReceiver" and which is registered as a receiver in an Android Application via the AndroidManifest.xml (or via code). This class will be able to receive intents via the sendBroadcast() method. "BroadCastReceiver" defines the method "onReceive()". Only during this method your broadcast receiver object will be valid, afterwards the Android system will consider your object as no longer active. Therefore you cannot perform any asynchronous operation.

1.4. Pending Intent

This tutorial will also use a PendingIntent. A PendingIntent is a token that you give to another application (e.g. Notification Manager, Alarm Manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code.

Read more: vogella.de