You are on page 1of 67

Android Services & Security:

Programming Started Services


(Part 1)
Douglas C. Schmidt

d.schmidt@vanderbilt.edu
www.dre.vanderbilt.edu/~schmidt
Professor of Computer Science
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA

Android Services & Security: Programming Started Services (Part 1)

Learning Objectives in this Part of the Module


Understand how to program a Started Service
e.g., a Download Application that uses a Started Service to retrieve &
display an image from a remote server

Download
Activity
Socket

Download
Service

Socket

Send HTTP
GET request

Android Services & Security: Programming Started Services (Part 1)

Download Application Overview

Android Services & Security: Programming Started Services (Part 1)

Download Application Overview


1. DownloadActivity uses startService() to launch a DownloadService

Download
Activity

Android Services & Security: Programming Started Services (Part 1)

Download Application Overview


1. DownloadActivity uses startService() to launch a DownloadService

Download
Activity

Android Services & Security: Programming Started Services (Part 1)

Download Application Overview


1. DownloadActivity uses startService() to launch a DownloadService

Download
Activity

startService()

Download
Service

Android Services & Security: Programming Started Services (Part 1)

Download Application Overview


1. DownloadActivity uses startService() to launch a DownloadService

Download
Activity

startService()

Download
Service

Android Services & Security: Programming Started Services (Part 1)

Download Application Overview


1. DownloadActivity uses startService() to launch a DownloadService
2. The DownloadService retrieves the image & stores it in a file on the device

Download
Activity

Download
Service

Android Services & Security: Programming Started Services (Part 1)

Download Application Overview


1. DownloadActivity uses startService() to launch a DownloadService
2. The DownloadService retrieves the image & stores it in a file on the device

Download
Activity
Socket

Download
Service

Socket

Send HTTP
GET request

Android Services & Security: Programming Started Services (Part 1)

Download Application Overview


1. DownloadActivity uses startService() to launch a DownloadService
2. The DownloadService retrieves the image & stores it in a file on the device
3. The DownloadService returns the pathname of the file back to the

DownloadActivity, which then displays the image

Download
Activity

Send reply

Download
Service

10

Android Services & Security: Programming Started Services (Part 1)

Download Application Overview


1. DownloadActivity uses startService() to launch a DownloadService
2. The DownloadService retrieves the image & stores it in a file on the device
3. The DownloadService returns the pathname of the file back to the

DownloadActivity, which then displays the image

Download
Activity

Send reply

Download
Service

11 1 & 2 in this part


Well just cover steps

Android Services & Security: Programming Started Services (Part 1)

Download Application Overview


1. DownloadActivity uses startService() to launch a DownloadService
2. The DownloadService retrieves the image & stores it in a file on the device
3. The DownloadService returns the pathname of the file back to the

DownloadActivity, which then displays the image

Download
Activity

Send reply

Download
Service

12 & Service Communication


See upcoming parts on Activity

Android Services & Security: Programming Started Services (Part 1)

Tips to Understand the Download Application


Run/read the code & watch the video carefully to understand how it works

13
github.com/douglascraigschmidt/POSA-14/tree/master/ex/DownloadApplication

Android Services & Security: Programming Started Services (Part 1)

Tips to Understand the Download Application


Run/read the code & watch the video carefully to understand how it works
This example is complex since many classes & Android mechanisms are used

14

Android Services & Security: Programming Started Services (Part 1)

Tips to Understand the Download Application


Run/read the code & watch the video carefully to understand how it works
This example is complex since many classes & Android mechanisms are used
We therefore analyze it from various perspectives

15

Android Services & Security: Programming Started Services (Part 1)

Programming
Started Services
(Part 1)

16

Android Services & Security: Programming Started Services (Part 1)

Programming a Started Service

Download
Activity
Socket

Download
Service

Socket

17

Send HTTP
GET request

Android Services & Security: Programming Started Services (Part 1)

Launching a Started Service


A client launches a Started Service by calling startService()

Intent intent =
DownloadService.makeIntent
(this, Uri.parse(url), downloadHandler);
startService(intent);

Download
Activity
18
developer.android.com/guide/components/services.html#CreatingStartedService

Android Services & Security: Programming Started Services (Part 1)

Launching a Started Service


A client launches a Started Service by calling startService()
e.g., Download Activity creates an Intent that identifies
the DownloadService & supplies a URL parameter via
Intent data that tells Service what image to retrieve
Intent intent =
DownloadService.makeIntent
(this, Uri.parse(url), downloadHandler);
startService(intent);

Download
Activity
19

Android Services & Security: Programming Started Services (Part 1)

Launching a Started Service


A client launches a Started Service by calling startService()
e.g., Download Activity creates an Intent that identifies
the DownloadService & supplies a URL parameter via
Intent data that tells Service what image to retrieve
Intent intent =
DownloadService.makeIntent
(this, Uri.parse(url), downloadHandler);
startService(intent);

Download
Activity
20

Android Services & Security: Programming Started Services (Part 1)

Launching a Started Service


A client launches a Started Service by calling startService()
e.g., Download Activity creates an Intent that identifies
the DownloadService & supplies a URL parameter via
Intent data that tells Service what image to retrieve
Intent intent =
DownloadService.makeIntent
(this, Uri.parse(url), downloadHandler);
startService(intent);

Download
Activity

This call doesnt block


the client while the
DownloadService runs

21

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


In response to startService(), Android will launch the
Service if its not already running

Androids Activity Manager


launches the DownloadService

Download
Activity

Download
Service

22
www.dre.vanderbilt.edu/~schmidt/PDF/Activator.pdf
has more info

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


In response to startService(), Android will launch the
Service if its not already running
Android then invokes the Services onCreate() &
onStartCommand() hook methods

public class DownloadService extends Service {


public void onCreate() { ... }
public int onStartCommand(Intent intent,
int flags, int startId) { ... }
...

Download
Activity

Download
Service
23

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


In response to startService(), Android will launch the
Service if its not already running
Android then invokes the Services onCreate() &
onStartCommand() hook methods

public class DownloadService extends Service {


public void onCreate() { ... }
public int onStartCommand(Intent intent,
int flags, int startId) { ... }
...

Download
Activity

Download
Service
24

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


In response to startService(), Android will launch the
Service if its not already running
Android then invokes the Services onCreate() &
onStartCommand() hook methods
onCreate() starts a HandlerThread
public class DownloadService extends Service {
public void onCreate() {
...
}

Download
Activity

Download
Service
25

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


In response to startService(), Android will launch the
Service if its not already running
Android then invokes the Services onCreate() &
onStartCommand() hook methods

HandlerThread works with a ServiceHandler to download


the image in the background & return pathname to client
public class DownloadService extends Service {
public void onCreate() {
...
}

Download
Activity

Download
Service
26

Android Services & Security: Programming Started Services (Part 1)

Programming
Started Services
(Part 2)

27

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


In response to startService(), Android will launch the
Service if its not already running
Android then invokes the Services onCreate() &
onStartCommand() hook methods

onStartCommand() sends the Intent


to the background HandlerThread
public class DownloadService extends Service {
public int onStartCommand(Intent intent,
int flags,
int startId)
{ ... }

Download
Activity

Download
Service
28

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


In response to startService(), Android will launch the
Service if its not already running
Android then invokes the Services onCreate() &
onStartCommand() hook methods

onStartCommand() returns a result


to Android, but not to the client
public class DownloadService extends Service {
public int onStartCommand(Intent intent,
int flags,
int startId)
{ return ...; }

Download
Activity

Download
Service
29

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


Return value tells Android what it should do with the Service if its process is killed
while
After a client calls startService() Android invokes the
it is running

Services onCreate() & onStartCommand() hook methods


If Service not already running, it will be started & will
receive the Intent via onStartCommand()
onStartCommand() returns a result to Android, but not
to the client

public class DownloadService extends Service {


public int onStartCommand(Intent intent,
int flags,
int startId)
{ return ...; }

Download
Activity

Download
Service
30

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


Return value tells Android what it should do with the Service if its process is killed
while
After a client calls startService() Android invokes the
it is running

Services onCreate() & onStartCommand() hook methods


If Service not already running, it will be started & will
receive the Intent via onStartCommand()
onStartCommand() returns a result to Android, but not
to the client

START_STICKY Dont redeliver Intent to onStartCommand() (pass null intent)

public class DownloadService extends Service {


public int onStartCommand(Intent intent,
int flags,
int startId)
{ return ...; }

Download
Activity

Download
Service
31

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


Return value tells Android what it should do with the Service if its process is killed
while
After a client calls startService() Android invokes the
it is running

Services onCreate() & onStartCommand() hook methods


If Service not already running, it will be started & will
START_NOT_STICKY Service should remain stopped until explicitly started by
receive the Intent via onStartCommand()
some client code
onStartCommand() returns a result to Android, but not
to the client

START_STICKY Dont redeliver Intent to onStartCommand() (pass null intent)

public class DownloadService extends Service {


public int onStartCommand(Intent intent,
int flags,
int startId)
{ return ...; }

Download
Activity

Download
Service
32

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


Return value tells Android what it should do with the Service if its process is killed
while
After a client calls startService() Android invokes the
it is running

Services onCreate() & onStartCommand() hook methods


If Service not already running, it will be started & will
START_NOT_STICKY Service should remain stopped until explicitly started by
receive the Intent via onStartCommand()
some client code
onStartCommand() returns
a result
to Android,
but not
START_REDELIVER_INTENT
Restart
Service
via onStartCommand(),
supplying the
to the
client
same
Intent
as was delivered this time
START_STICKY Dont redeliver Intent to onStartCommand() (pass null intent)

public class DownloadService extends Service {


public int onStartCommand(Intent intent,
int flags,
int startId)
{ return ...; }

Download
Activity

Download
Service
33

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


In response to startService(), Android will launch the
Service if its not already running
Android then invokes the Services onCreate() &
onStartCommand() hook methods

public class DownloadService extends Service {


public int onStartCommand(Intent intent,
int flags,
int startId)
{ return ...; }

Download
Activity

Download
Service

34
android-developers.blogspot.com.au/2010/02/service-api-changes-starting-with.html

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


In response to startService(), Android will launch the

Service if its not already running


Android then invokes the Services onCreate() &
onStartCommand() hook methods
A started service typically performs a single operation &
often doesnt return a result to the client
public class DownloadService extends Service {
...
public void handleMessage(Message msg) {
downloadImage((Intent) msg.obj);
...

Download
Activity

Download
Service
35

Android Services & Security: Programming Started Services (Part 1)

Processing a Started Service


In response to startService(), Android will launch the

Service if its not already running


Android then invokes the Services onCreate() &
onStartCommand() hook methods
A started service typically performs a single operation &
often doesnt return a result to the client
public class DownloadService extends Service {
...
public void handleMessage(Message msg) {
downloadImageAndReply((Intent) msg.obj);
...

Download
Activity

Download
Service
36

Retrieves an image from


remote server & returns
pathname to client

Android Services & Security: Programming Started Services (Part 1)

Stopping a Started Service


When the operation is done, the Service can be stopped

public class DownloadService extends Service {


...
public void handleMessage(Message msg) {
...
stopSelf(msg.arg1);

Download
Activity

Download
Service
37

Android Services & Security: Programming Started Services (Part 1)

Stopping a Started Service


When the operation is done, the Service can be stopped
e.g., the DownloadService call stopSelf() to shut itself

down when its done retrieving & processing an image


A Service that stops itself must be careful there arent
concurrent operations processing other Intents!
public class DownloadService extends Service {
...
public void handleMessage(Message msg) {
...
stopSelf(msg.arg1);

Download
Activity

Download
Service
38

Android Services & Security: Programming Started Services (Part 1)

Stopping a Started Service


When the operation is done, the Service can be stopped
e.g., the DownloadService call stopSelf() to shut itself

down when its done


Conversely, a Service can be shut down if stopService()
is called by another component
public class DownloadActivity ... {
...
stopService(intent);
...

Download
Activity

Download
Service
39

Android Services & Security: Programming Started Services (Part 1)

Design of the
Download Application

40

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application

41

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application

42

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application

43

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()

Intent

Download
Activity

startService()

44

send
intent

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
Based on the Activator pattern

45

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions

46

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions
1. Creates a ServiceHandler
Associated with a single
HandlerThread

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

3 Create a
Service
Handler
sendMessage()
handleMessage()
downloadImageAndReply()

47

worker
thread &
Handler

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions
1. Creates a ServiceHandler
Associated with a single
HandlerThread

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

3 Create a
Service
Handler
sendMessage()
handleMessage()
downloadImageAndReply()

48

worker
thread &
Handler

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions
1. Creates a ServiceHandler
Associated with a single
HandlerThread

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

3 Create a
Service
Handler
sendMessage()
handleMessage()
downloadImageAndReply()

49

worker
thread &
Handler

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions
1. Creates a ServiceHandler
Associated with a single
HandlerThread

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

3 Create a
Service
Handler
sendMessage()
handleMessage()
downloadImageAndReply()

50

worker
thread &
Handler

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions
1. Creates a ServiceHandler
Associated with a single
HandlerThread

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

3 Create a
Service
Handler

worker
thread &
Handler

sendMessage()
handleMessage()
downloadImageAndReply()

51 Messages with Android Handler


See earlier part on Sending & Handling

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions
1. Creates a ServiceHandler
2. Receives & queues an Intent
in the ServiceHandler

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

4
Service
Handler
sendMessage()
handleMessage()
downloadImageAndReply()

52

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions
1. Creates a ServiceHandler
2. Receives & queues an Intent
in the ServiceHandler

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

4
Service
Handler
sendMessage()
handleMessage()
downloadImageAndReply()

53

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions
1. Creates a ServiceHandler
2. Receives & queues an Intent
in the ServiceHandler
3. ServiceHandler processes
Intent in the background
Downloads image designated
by the URL in the Intent

54

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

Service
Handler
sendMessage()

Dequeue
Intent
& get file

handleMessage()

5
downloadImageAndReply()

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions
1. Creates a ServiceHandler
2. Receives & queues an Intent
in the ServiceHandler
3. ServiceHandler processes
Intent in the background
Downloads image designated
by the URL in the Intent

55

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

Service
Handler
sendMessage()

Dequeue
Intent
& get file

handleMessage()

5
downloadImageAndReply()

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions
1. Creates a ServiceHandler
2. Receives & queues an Intent
in the ServiceHandler
3. ServiceHandler processes
Intent in the background
Downloads image designated
by the URL in the Intent

Intent

Download
Service
onCreate()

send
intent

onStartCommand()

Service
Handler
sendMessage()

Dequeue
Intent
& get file

handleMessage()

5
downloadImageAndReply()

56 & Service Communication


See upcoming parts on Activity

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
Download
The DownloadService is
Activity
1
launched on-demand
startService()
DownloadService performs
four main actions
1. Creates a ServiceHandler
2. Receives & queues an Intent
in the ServiceHandler
3. ServiceHandler processes
Intent in the background
4. Stops itself when there are
no more Intents to handle

57

Intent

Download
Service
onCreate()

send
intent
4

Service
Handler
sendMessage()

Dequeue
Intent
& get file

handleMessage()

5
downloadImageAndReply()

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
The DownloadService is
launched on-demand
DownloadService performs
four main actions
This design is guided by the
Command Processor pattern

Client

Queue request

58
www.dre.vanderbilt.edu/~schmidt/PDF/CommandRevisited.pdf
has more info

Android Services & Security: Programming Started Services (Part 1)

Design of the Download Application


DownloadActivity sends an Intent
via a call to startService()
The DownloadService is
launched on-demand
DownloadService performs
four main actions
This design is guided by the
Command Processor pattern
Offload tasks from applications
main Thread to a single
backgroundThread

Client

Queue request

59handle multiple requests concurrently


This pattern works if a Service neednt

Android Services & Security: Programming Started Services (Part 1)

Summary

60

Android Services & Security: Programming Started Services (Part 1)

Summary
The Download Application uses a
Started Service to retrieve & display
an image from a remote server

Download
Activity
Socket

Download
Service

Socket

61

Send HTTP
GET request

Android Services & Security: Programming Started Services (Part 1)

Summary
The Download Application uses a
Started Service to retrieve & display
an image from a remote server
When a Started Service is launched,
it has a lifecycle that's independent
of the component that started it

Download
Activity

Download
Service

62

Android Services & Security: Programming Started Services (Part 1)

Summary
The Download Application uses a
Started Service to retrieve & display
an image from a remote server
When a Started Service is launched,
it has a lifecycle that's independent
of the component that started it
A Service can run in the background
indefinitely, even if the component
that started it is destroyed

Download
Activity

Download
Service

63

Android Services & Security: Programming Started Services (Part 1)

Summary
The Download Application uses a
Started Service to retrieve & display
an image from a remote server
When a Started Service is launched,
it has a lifecycle that's independent
of the component that started it
Started Services are driven by
inversion of control

64

Android Services & Security: Programming Started Services (Part 1)

Summary
The Download Application uses a
Started Service to retrieve & display
an image from a remote server
When a Started Service is launched,
it has a lifecycle that's independent
of the component that started it
Started Services are driven by
inversion of control

65

Android Services & Security: Programming Started Services (Part 1)

Summary
The Download Application uses a
Started Service to retrieve & display
an image from a remote server
When a Started Service is launched,
it has a lifecycle that's independent
of the component that started it
Started Services are driven by
inversion of control
The Download Application
implementation is guided by
a common Android idiom for
concurrent Service processing

Intent
Download
Activity

startService()

Download
Service
onCreate()

send
intent

onStartCommand()

Service
Handler
sendMessage()
handleMessage()

5
downloadImageAndReply()

dequeue Intent
& download file

66

Android Services & Security: Programming Started Services (Part 1)

Summary
The Download Application uses a
Started Service to retrieve & display
an image from a remote server
When a Started Service is launched,
it has a lifecycle that's independent
of the component that started it
Started Services are driven by
inversion of control
The Download Application
implementation is guided by
a common Android idiom for
concurrent Service processing
This idiom is based on the
Command Processor pattern

Intent
Client
Activity

onCreate()

startService()

Service
Handler

Intent
Service

send
intent

onStartCommand()

queue
intent
3

Download
Service

sendMessage()
handleMessage()

onHandleIntent()

dequeue
intent

See upcoming parts on The67


Command Processor Pattern

process intent

You might also like