You are on page 1of 5

[BLANK_AUDIO]

Hi, I'm Adam Porter, and this is


Programming Mobile Applications for
Android Handheld Systems.
Much of our discussion of Android so far
has focused around the
activity class and things that users see
and interact with through that class.
Things like fragments, user interface
classes, maps, and touchable views.
But handheld applications also include
other things
that are less visible, but nonetheless
very important.
For example, we've talked about broadcast
receivers.
A fundamental Android component that sits
in
the background, listening for and
responding to events.
Are there are also content providers.
Another fundamental Android component that
allows inter application data sharing.
The fourth fundamental Android component
is the service class.
Services are designed to support longer
running operations
that are usually not visible to the user.
Things like downloading large data files
from the network.
Or synchronizing on-device information
with the network server.
In this lesson I'll begin by giving an
overview of the service class.
Next I'll talk about how you implement
simpler services that can be started by
clients.
And after that I'll finish up by talking
about how you implement
more complex services that clients can
bind to and then interact with.
As I said, services do not interact
directly
with users, so they have no user
interfaces.
Services have two main uses, first, they
allow you to
perform work in the background even if the
services application terminates.
Second, services allow code in one process
to interact with code in another process.
And as I mentioned earlier components that
want to use a service but don't need
to directly interact with it, can start
that
service by calling the Context classes
startService method.
Passing in an intent associated with that
specific service.
Once started, the service can run in the
background indefinitely.
Although, like any component, Android can
kill
a service if it needs that service's
resources.
Started services are usually designed to
perform a single operation, after which
they
terminate themselves without transferring
any results
back to the component that started them.
And by default, services run in the main
thread of their hosting application.
So, depending on how you implement and use
this service, you may need
to create a separate thread on which the
service can do its work.
In contrast, components that want to use a
service, but do need to directly
interact with it, can bind to that
service by calling the Context classes
bindService method.
Passing in an Intent associated with a
specific service.
A ServiceConnection object which
implements callback methods once the
client is connected to and disconnected
from the service, and any flags
that control the behavior of the binding
operation.
Binding to a service allows a component to
send request to and receive responses
from a service running in the same
process, or in a different process.
At binding time, if the service is not
already started, it will be started and
once
bound, the service will continue running
as long
as at least one client remains bound to
it.
[BLANK_AUDIO]
So here's my device, now I'll start the
LocalLoggingService application.
As you can see, this application displays
a single button labeled Generate Log
Message.
When I click this button, a service will
be started,
and that service will write a message to
the log.
Let me hit that button now.
Let's take a look at the log console to
see the message.
So
here's my IDE.
Let me open the LogCat view.
Here, you can see a log message
that was written by the
LocalLoggingService application.
Let's look at the source code for this
application.
Here's the application, opened in the IDE.
Let me start by opening up
LoggingServiceClient class.
In onCreate, this code first creates an
Intent that
will be used to start the LoggingService
service object.
Next, the code adds the message to
be logged as an extra to the
startServiceIntent.
And lastly, the code calls the
startService method, passing in that
Intent.
Now, I'll open up a LoggingService class,
this class extends IntentService.
The IntentService is started by the
system, and thereafter it queues incoming
intents, and then hands them off, one at a
time, to the onHandleIntent method.
In this case, that method simply writes
the incoming message to the log.
[BLANK_AUDIO]
Our next example application is called
MusicPlayerForegroundService, with
this application, a client activity starts
a service that plays a music file.
The service plays the music in what's
called the Foreground state.
And that means the that the service will
be doing something that the user will,
will be
aware of and therefore, Android should
refrain from killing
this service if the system gets low on
memory.
In addition, like any service, the music
playing service will
keep playing even if the client activity
pauses or terminates.
Let's watch this application run.
[BLANK_AUDIO]
So here's my device.
Now, I'll start the music player
foreground service application.
As you can see, there are now two buttons
shown on the display.
The top one is labeled Start Service, and
the bottom one is labeled Stop Service.
Pressing these buttons starts or stops a
service that is hosted by this
application.
Let me start by pressing the start button,
and as you
can hear, when I press the start button,
music started playing.
You also notice that there's also a
notification up in the status bar.
Now, I'll back out of this application,
and even though I've killed the
application, you can
see that the service is still playing the
music.
Now, I'll pull down on the notification
drawer.
And click on the Notification View which
restarts the application.
And now I'll press the Stop Service button
which, as you can
hear, stops the service and stops the
music that it was playing.
Let's look at the source code for this
application.
Here's the application opened in the IDE.
Let me start by opening up Service Music
ServiceClient, in onCreate.
This code first creates an Intent, that
will be used to start the MusicService.
Next the code sets up listeners for the
startButton and for the stopButton.
The startButton and listener calls the
startService method, passing in the
musicServiceIntent.
And the stopButton calls the stopService
method, also passing in the same
musicServiceIntent.
So let's switch over and take a look now
at the MusicService class.
Here in onCreate this code begins by
setting up a new MediaPlayer.
It also attaches an on CompletionListener
to the MediaPlayer,
which will stop the service when the music
finishes playing.
Next, the code creates a notification area
notification, so
that the user can exit the music player
service client,
but still have a way to get back to
the client in order to shut down the music
player.
Continuing down the code calls the
startForeground method.
Which puts this service in a foreground
state so that it
is less likely to be killed if the system
needs more resources.
Next the code overrides the onStartCommand
method.
This method gets called when a client
calls the start service command.
The code in this method checks to see
whether the media player
exists, and if so, it then saves the
current start
command ID, and then makes the media
player play the song from the beginning.
This method ends by returning a value that
tells the system
what to do if the service is killed by the
system.
In this case, that value is
START_NOT_STICKY.
which means that the system should not
automatically restart the service if it
gets killed.
[BLANK_AUDIO]

You might also like