Until now we have been exploring various aspects of the Android
component
Activity. The next important Android
component to
explore is Service.
A Service is an Android component
that is used to perform some long-running task in the
background
without the need for any user interface.
The Android Alarm Clock application is an example of a
Service component.
To implement a custom Service, one needs
to sub-class the Android framework class
android.app.Service.
In the custom Service class will override
the
following callback methods:
onCreate() :: callback method used
to perform
any one-time setup and is called by the Android system only once when
the Service is first created
onBind() :: callback method used
to bind
an Activity to the Service.
This method will return an instance of the Android framework class android.os.IBinder which is the
interface
through which the bound component interacts with the Service
onStartCommand() :: callback
method that is invoked by the Android system
when an Activity calls the startService() method. Once this
method
executes, the Service is started in the
background. We need to return an integer constant of START_STICKY
or START_NOT_STICKY or START_REDELIVER_INTENT
onDestroy() :: callback
method that is invoked by the Android system
when an Activity calls the stopService() method. Once this
method
executes, the Service is stopped. This
method is used to clean-up any resources used by the Service
Let us create a new Android application named
DroidAlarmService to illustrate the concept of using
Android Service component to build
our own custom alarm clock application.
This application will display a screen with a Time picker and two
Buttons - one
to set the alarm and other to cancel the alarm.
We will not go step-by-step to show the various
screens since we already did that in Part-2 for the DroidTipCalculator
application.
Modify the contents of the dimens.xml
file to look like the one shown in the
listing 10.1 below:
Next, modify the contents of the strings.xml
file to look like the one shown in the
listing 10.2 below:
The contents of the activity_time_pick.xml
layout file will look like the one shown in the listing 10.3 below:
Previewing the activity_time_pick.xml
layout file in the Graphical Mode in Eclipse will look like the one
shown in the figure 10.1 below:
Figure-10.1
The contents of the java source file TimePickActivity.java
will look like the one shown in the listing 10.4 below:
The contents of the java source file MyAlarmService.java
will look like the one shown in the listing 10.5 below:
Here is how our custom alarm clock will work:
In the activity named TimePickActivity,
the user will pick an appropriate time (hour and minute) for the alarm
and press
the Set Alarm button
On pressing the Set Alarm button
in the activity TimePickActivity, an Intent is created to start the service
named MyAlarmService with the associated
data (selected hour and minute). Also, the Set Alarm
button is disabled and the Cancel Alarm
button is enabled
On pressing the Cancel Alarm button
in the activity TimePickActivity will
stop the service MyAlarmService. Also,
the Cancel Alarm button is disabled and
the Set Alarm button is enabled
Calling the startService() method
by passing the created Intent will start
the service MyAlarmService in the
background
A very important thing to keep in mind - when we start a Service from an Activity,
it runs in the same UI Thread
When the service MyAlarmService
starts,
the onStartCommand() method is called. We
return the constant START_STICKY to
indicate that we will explicitly start and stop the service
Since the MyAlarmService runs on the
main
thread as the TimePickActivity, we start an AsyncTask in the background to trigger an
alarm at the selected hour and minute
Notice we start a Handler in the OnCreate() method of TimePickActivity.
It will display a Toast alert when it
receives a Message. Also, the Cancel Alarm button is disabled and the Set Alarm button is enabled
At the selected hour and minute, the background AsyncTask
from MyAlarmService
triggers a Message to the Handler
in TimePickActivity
Finally, modify the contents of the AndroidManifest.xml
file to look like the one shown in the listing 10.6 below:
The important change to notice is the additional tag named
<Service>. We use this
tag to define our service MyAlarmService.
We are now ready to test our DroidAlarmService
application on the virtual Android device we created in
Part-1.
We will create a Run Configuration for DroidAlarmService
as we did in Part-2
for DroidTipCalculator.
Once the run configuration for DroidAlarmService
is ready, we will Run
the application and the application will come to life as shown in the
following figure 10.2 below:
Figure-10.2
Pick the desired alarm time (hour and minute) and click on the
Set Alarm button. This will launch
the background service MyAlarmService
resulting in the following figure 10.3 below:
Figure-10.3
When the alarm goes off at the desired time (hour and minute),
it will display a Toast message
as shown in the following figure 10.4 below: