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

2

Send HTTP
GET request

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

Download Application Overview

3

Android Services & Security: Programming Started Services (Part 1) Download Application Overview 1. DownloadActivity uses startService() to launch a DownloadService Download Activity 4 .

Android Services & Security: Programming Started Services (Part 1) Download Application Overview 1. DownloadActivity uses startService() to launch a DownloadService Download Activity 5 .

Android Services & Security: Programming Started Services (Part 1) Download Application Overview 1. DownloadActivity uses startService() to launch a DownloadService Download Activity startService() Download Service 6 .

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

Download Application Overview
1. DownloadActivity uses startService() to launch a DownloadService

Download
Activity

startService()

Download
Service

7

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

8

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

9

Send HTTP
GET request

Android Services & Security: Programming Started Services (Part 1) Download Application Overview 1. 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. DownloadActivity uses startService() to launch a DownloadService 2. which then displays the image Download Activity Send reply Download Service 10 .

The DownloadService returns the pathname of the file back to the DownloadActivity. DownloadActivity uses startService() to launch a DownloadService 2.Android Services & Security: Programming Started Services (Part 1) Download Application Overview 1. The DownloadService retrieves the image & stores it in a file on the device 3. which then displays the image Download Activity Send reply Download Service 11 1 & 2 in this part We’ll just cover steps .

DownloadActivity uses startService() to launch a DownloadService 2. The DownloadService returns the pathname of the file back to the DownloadActivity.Android Services & Security: Programming Started Services (Part 1) Download Application Overview 1. The DownloadService retrieves the image & stores it in a file on the device 3. which then displays the image Download Activity Send reply Download Service 12 & Service Communication” See upcoming parts on “Activity .

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 13 github.

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 .

startService(intent).html#CreatingStartedService .makeIntent (this.android. Uri. downloadHandler).parse(url).com/guide/components/services.Android Services & Security: Programming Started Services (Part 1) Launching a Started Service • A client launches a Started Service by calling startService() Intent intent = DownloadService. Download Activity 18 developer.

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. Uri. startService(intent).Android Services & Security: Programming Started Services (Part 1) Launching a Started Service • A client launches a Started Service by calling startService() • e. downloadHandler).parse(url).. Download Activity 19 .makeIntent (this.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. 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..makeIntent (this.g. Uri.parse(url). downloadHandler). startService(intent).

parse(url). downloadHandler). Download Activity This call doesn’t block the client while the DownloadService runs 21 .g..makeIntent (this. Uri.Android Services & Security: Programming Started Services (Part 1) Launching a Started Service • A client launches a Started Service by calling startService() • e. 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. startService(intent).

Android Services & Security: Programming Started Services (Part 1) Processing a Started Service • In response to startService().vanderbilt. Android will launch the Service if it’s not already running Android’s Activity Manager launches the DownloadService Download Activity Download Service 22 www.edu/~schmidt/PDF/Activator.dre.pdf has more info .

int startId) { . Android will launch the Service if it’s not already running • Android then invokes the Service’s onCreate() & onStartCommand() hook methods public class DownloadService extends Service { public void onCreate() { .... } .. int flags.Android Services & Security: Programming Started Services (Part 1) Processing a Started Service • In response to startService(). } public int onStartCommand(Intent intent.. Download Activity Download Service 23 ..

.. int flags. Download Activity Download Service 24 ... Android will launch the Service if it’s not already running • Android then invokes the Service’s onCreate() & onStartCommand() hook methods public class DownloadService extends Service { public void onCreate() { . int startId) { .. } .. } public int onStartCommand(Intent intent.Android Services & Security: Programming Started Services (Part 1) Processing a Started Service • In response to startService().

} 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 it’s not already running • Android then invokes the Service’s onCreate() & onStartCommand() hook methods onCreate() starts a HandlerThread public class DownloadService extends Service { public void onCreate() { ..

.. } Download Activity Download Service 26 .Android Services & Security: Programming Started Services (Part 1) Processing a Started Service • In response to startService(). Android will launch the Service if it’s not already running • Android then invokes the Service’s 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() { .

Android Services & Security: Programming Started Services (Part 1) Programming Started Services (Part 2) 27 .

} Download Activity Download Service 28 .. int flags.Android Services & Security: Programming Started Services (Part 1) Processing a Started Service • In response to startService(). int startId) { .. Android will launch the Service if it’s not already running • Android then invokes the Service’s onCreate() & onStartCommand() hook methods onStartCommand() sends the Intent to the background HandlerThread public class DownloadService extends Service { public int onStartCommand(Intent intent.

Android will launch the Service if it’s not already running • Android then invokes the Service’s onCreate() & onStartCommand() hook methods onStartCommand() returns a result to Android...Android Services & Security: Programming Started Services (Part 1) Processing a Started Service • In response to startService(). 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 ..

.. } 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 Service’s onCreate() & onStartCommand() hook methods • If Service not already running. int flags. 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 startId) { return ..

.. int flags.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 Service’s onCreate() & onStartCommand() hook methods • If Service not already running. int startId) { return .. but not to the client • START_STICKY – Don’t redeliver Intent to onStartCommand() (pass null intent) public class DownloadService extends Service { public int onStartCommand(Intent intent. } Download Activity Download Service 31 . it will be started & will receive the Intent via onStartCommand() • onStartCommand() returns a result to Android.

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... int flags. int startId) { return ..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 Service’s onCreate() & onStartCommand() hook methods • If Service not already running. } Download Activity Download Service 32 . but not to the client • START_STICKY – Don’t redeliver Intent to onStartCommand() (pass null intent) public class DownloadService extends Service { public int onStartCommand(Intent intent.

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. supplying the to the client same Intent as was delivered this time • START_STICKY – Don’t redeliver Intent to onStartCommand() (pass null intent) public class DownloadService extends Service { public int onStartCommand(Intent intent. but not • • START_REDELIVER_INTENT – Restart Service via onStartCommand()...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 Service’s onCreate() & onStartCommand() hook methods • If Service not already running. int flags. } Download Activity Download Service 33 . int startId) { return ..

.. int flags. } Download Activity Download Service 34 android-developers.blogspot.Android Services & Security: Programming Started Services (Part 1) Processing a Started Service • In response to startService().html . int startId) { return .au/2010/02/service-api-changes-starting-with.com.. Android will launch the Service if it’s not already running • Android then invokes the Service’s onCreate() & onStartCommand() hook methods public class DownloadService extends Service { public int onStartCommand(Intent intent.

.... Download Activity Download Service 35 .obj).. public void handleMessage(Message msg) { downloadImage((Intent) msg. Android will launch the Service if it’s not already running • Android then invokes the Service’s onCreate() & onStartCommand() hook methods • A started service typically performs a single operation & often doesn’t return a result to the client public class DownloadService extends Service { .Android Services & Security: Programming Started Services (Part 1) Processing a Started Service • In response to startService().

public void handleMessage(Message msg) { downloadImageAndReply((Intent) msg. Download Activity Download Service 36 Retrieves an image from remote server & returns pathname to client ...Android Services & Security: Programming Started Services (Part 1) Processing a Started Service • In response to startService(). .obj)... Android will launch the Service if it’s not already running • Android then invokes the Service’s onCreate() & onStartCommand() hook methods • A started service typically performs a single operation & often doesn’t return a result to the client public class DownloadService extends Service { .

the Service can be stopped public class DownloadService extends Service { .. Download Activity Download Service 37 ...arg1).Android Services & Security: Programming Started Services (Part 1) Stopping a Started Service • When the operation is done. stopSelf(msg. public void handleMessage(Message msg) { ..

Android Services & Security: Programming Started Services (Part 1) Stopping a Started Service • When the operation is done.arg1).. Download Activity Download Service 38 . the DownloadService call stopSelf() to shut itself down when it’s done retrieving & processing an image A Service that stops itself must be careful there aren’t concurrent operations processing other Intents! public class DownloadService extends Service { .. stopSelf(msg.... public void handleMessage(Message msg) { . the Service can be stopped • e.g.

. Download Activity Download Service 39 .g. . the DownloadService call stopSelf() to shut itself down when it’s done • Conversely. { .. a Service can be shut down if stopService() is called by another component public class DownloadActivity ..Android Services & Security: Programming Started Services (Part 1) Stopping a Started Service • When the operation is done..... stopService(intent). the Service can be stopped • e.

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 1 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 2 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 2 onStartCommand() .

Creates a ServiceHandler • Associated with a single HandlerThread Intent Download Service onCreate() send intent 2 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.

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 2 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 2 onStartCommand() 3 Create a Service Handler sendMessage() handleMessage() downloadImageAndReply() 49 worker thread & Handler .

Creates a ServiceHandler • Associated with a single HandlerThread Intent Download Service onCreate() send intent 2 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 2 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.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. Receives & queues an Intent in the ServiceHandler Intent Download Service onCreate() send intent 2 onStartCommand() 4 Service Handler sendMessage() handleMessage() downloadImageAndReply() 52 3 .

Creates a ServiceHandler 2. Receives & queues an Intent in the ServiceHandler Intent Download Service onCreate() send intent 2 onStartCommand() 4 Service Handler sendMessage() handleMessage() downloadImageAndReply() 53 3 .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.

ServiceHandler processes Intent “in the background” • Downloads image designated by the URL in the Intent 54 Intent Download Service onCreate() send intent 2 onStartCommand() 4 3 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. Receives & queues an Intent in the ServiceHandler 3. Creates a ServiceHandler 2.

ServiceHandler processes Intent “in the background” • Downloads image designated by the URL in the Intent 55 Intent Download Service onCreate() send intent 2 onStartCommand() 4 3 Service Handler sendMessage() Dequeue Intent & get file handleMessage() 5 downloadImageAndReply() . Receives & queues an Intent in the ServiceHandler 3.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.

ServiceHandler processes Intent “in the background” • Downloads image designated by the URL in the Intent Intent Download Service onCreate() send intent 2 onStartCommand() 4 3 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.

Receives & queues an Intent in the ServiceHandler 3. Stops itself when there are no more Intents to handle 57 Intent Download Service onCreate() send intent 4 3 Service Handler sendMessage() Dequeue Intent & get file handleMessage() 5 downloadImageAndReply() . ServiceHandler processes Intent “in the background” 4.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.

pdf has more info .dre.edu/~schmidt/PDF/CommandRevisited.vanderbilt.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 2 3 58 www.

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 application’s main Thread to a single backgroundThread Client Queue request 2 3 59handle multiple requests concurrently This pattern works if a Service needn’t .

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 .

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.

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 • A Service can run in the background indefinitely.

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 1 startService() Download Service onCreate() send intent 2 onStartCommand() 4 3 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.

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() 1 startService() Service Handler Intent Service send intent onStartCommand() queue intent 3 Download Service sendMessage() handleMessage() … onHandleIntent() 4 2 dequeue intent See upcoming parts on “The67 Command Processor Pattern” 5 process intent .