a semantic approach to english grammar 2nd edition
DESCRIPTION
GrammerTRANSCRIPT
-
Douglas C. Schmidt [email protected]
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)
-
Android Services & Security: Programming Started Services (Part 1)
2
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 Service
Download Activity
Socket
Socket
Send HTTP GET request
-
Android Services & Security: Programming Started Services (Part 1)
3
Download Application Overview
-
Android Services & Security: Programming Started Services (Part 1)
4
1. DownloadActivity uses startService() to launch a DownloadService
Download Application Overview
Download Activity
-
Android Services & Security: Programming Started Services (Part 1)
5
1. DownloadActivity uses startService() to launch a DownloadService
Download Application Overview
Download Activity
-
Android Services & Security: Programming Started Services (Part 1)
6
1. DownloadActivity uses startService() to launch a DownloadService
Download Application Overview
Download Service
Download Activity
startService()
-
Android Services & Security: Programming Started Services (Part 1)
7
1. DownloadActivity uses startService() to launch a DownloadService
Download Application Overview
Download Service
Download Activity
startService()
-
Android Services & Security: Programming Started Services (Part 1)
8
1. DownloadActivity uses startService() to launch a DownloadService 2. The DownloadService retrieves the image & stores it in a file on the device
Download Application Overview
Download Service
Download Activity
-
Android Services & Security: Programming Started Services (Part 1)
9
1. DownloadActivity uses startService() to launch a DownloadService 2. The DownloadService retrieves the image & stores it in a file on the device
Download Application Overview
Download Service
Download Activity
Socket
Socket
Send HTTP GET request
-
Android Services & Security: Programming Started Services (Part 1)
10
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 Application Overview
Download Service
Download Activity
Send reply
-
Android Services & Security: Programming Started Services (Part 1)
11
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 Application Overview
Download Service
Download Activity
Send reply
Well just cover steps 1 & 2 in this part
-
Android Services & Security: Programming Started Services (Part 1)
12
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 Application Overview
Download Service
Download Activity
Send reply
See upcoming parts on Activity & Service Communication
-
Android Services & Security: Programming Started Services (Part 1)
13 github.com/douglascraigschmidt/POSA-14/tree/master/ex/DownloadApplication
Tips to Understand the Download Application Run/read the code & watch the video carefully to understand how it works
-
Android Services & Security: Programming Started Services (Part 1)
14
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
-
Android Services & Security: Programming Started Services (Part 1)
15
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
-
Android Services & Security: Programming Started Services (Part 1)
16
Programming Started Services
(Part 1)
-
Android Services & Security: Programming Started Services (Part 1)
17
Programming a Started Service
Download Service
Socket
Socket
Send HTTP GET request
Download Activity
-
Android Services & Security: Programming Started Services (Part 1)
18
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
developer.android.com/guide/components/services.html#CreatingStartedService
-
Android Services & Security: Programming Started Services (Part 1)
19
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
Download Activity
Intent intent = DownloadService.makeIntent (this, Uri.parse(url), downloadHandler); startService(intent);
-
Android Services & Security: Programming Started Services (Part 1)
20
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
Download Activity
Intent intent = DownloadService.makeIntent (this, Uri.parse(url), downloadHandler); startService(intent);
-
Android Services & Security: Programming Started Services (Part 1)
21
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
Download Activity
Intent intent = DownloadService.makeIntent (this, Uri.parse(url), downloadHandler); startService(intent);
This call doesnt block the client while the
DownloadService runs
-
Android Services & Security: Programming Started Services (Part 1)
22
Processing a Started Service In response to startService(), Android will launch the
Service if its not already running
Download Service
Download Activity
Androids Activity Manager launches the DownloadService
www.dre.vanderbilt.edu/~schmidt/PDF/Activator.pdf has more info
-
Android Services & Security: Programming Started Services (Part 1)
23
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
Download Service
Download Activity
public class DownloadService extends Service { public void onCreate() { ... } public int onStartCommand(Intent intent, int flags, int startId) { ... } ...
-
Android Services & Security: Programming Started Services (Part 1)
24
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
Download Service
Download Activity
public class DownloadService extends Service { public void onCreate() { ... } public int onStartCommand(Intent intent, int flags, int startId) { ... } ...
-
Android Services & Security: Programming Started Services (Part 1)
25
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() { ... }
Download Service
Download Activity
onCreate() starts a HandlerThread
-
Android Services & Security: Programming Started Services (Part 1)
26
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() { ... }
Download Service
Download Activity
HandlerThread works with a ServiceHandler to download the image in the background & return pathname to client
-
Android Services & Security: Programming Started Services (Part 1)
27
Programming Started Services
(Part 2)
-
Android Services & Security: Programming Started Services (Part 1)
28
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) { ... }
Download Service
Download Activity
onStartCommand() sends the Intent to the background HandlerThread
-
Android Services & Security: Programming Started Services (Part 1)
29
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
Download Service
Download Activity
public class DownloadService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { return ...; }
onStartCommand() returns a result to Android, but not to the client
-
Android Services & Security: Programming Started Services (Part 1)
30
Processing a Started Service After a client calls startService() Android invokes the
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
Download Service
Download Activity
public class DownloadService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { return ...; }
Return value tells Android what it should do with the Service if its process is killed while it is running
-
Android Services & Security: Programming Started Services (Part 1)
31
Processing a Started Service After a client calls startService() Android invokes the
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
Download Service
Download Activity
public class DownloadService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { return ...; }
Return value tells Android what it should do with the Service if its process is killed while it is running START_STICKY Dont redeliver Intent to onStartCommand() (pass null intent)
-
Android Services & Security: Programming Started Services (Part 1)
32
Processing a Started Service After a client calls startService() Android invokes the
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
Download Service
Download Activity
public class DownloadService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { return ...; }
Return value tells Android what it should do with the Service if its process is killed while it is running START_STICKY Dont redeliver Intent to onStartCommand() (pass null intent) START_NOT_STICKY Service should remain stopped until explicitly started by
some client code
-
Android Services & Security: Programming Started Services (Part 1)
33
Processing a Started Service After a client calls startService() Android invokes the
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
Download Service
Download Activity
public class DownloadService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { return ...; }
Return value tells Android what it should do with the Service if its process is killed while it is running START_STICKY Dont redeliver Intent to onStartCommand() (pass null intent) START_NOT_STICKY Service should remain stopped until explicitly started by
some client code START_REDELIVER_INTENT Restart Service via onStartCommand(), supplying the
same Intent as was delivered this time
-
Android Services & Security: Programming Started Services (Part 1)
34
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
Download Service
Download Activity
public class DownloadService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { return ...; }
android-developers.blogspot.com.au/2010/02/service-api-changes-starting-with.html
-
Android Services & Security: Programming Started Services (Part 1)
35
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
Processing a Started Service
Download Service
Download Activity
public class DownloadService extends Service { ... public void handleMessage(Message msg) { downloadImage((Intent) msg.obj); ...
-
Android Services & Security: Programming Started Services (Part 1)
36
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
Processing a Started Service
Download Service
Download Activity
public class DownloadService extends Service { ... public void handleMessage(Message msg) { downloadImageAndReply((Intent) msg.obj); ...
Retrieves an image from remote server & returns
pathname to client
-
Android Services & Security: Programming Started Services (Part 1)
37
When the operation is done, the Service can be stopped
Stopping a Started Service
Download Service
Download Activity
public class DownloadService extends Service { ... public void handleMessage(Message msg) { ... stopSelf(msg.arg1);
-
Android Services & Security: Programming Started Services (Part 1)
38
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
Stopping a Started Service
Download Service
Download Activity
public class DownloadService extends Service { ... public void handleMessage(Message msg) { ... stopSelf(msg.arg1);
A Service that stops itself must be careful there arent concurrent operations processing other Intents!
-
Android Services & Security: Programming Started Services (Part 1)
39
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
Stopping a Started Service
Download Service
Download Activity
public class DownloadActivity ... { ... stopService(intent); ...
-
Android Services & Security: Programming Started Services (Part 1)
40
Design of the Download Application
-
Android Services & Security: Programming Started Services (Part 1)
41
Design of the Download Application
-
Android Services & Security: Programming Started Services (Part 1)
42
Design of the Download Application
-
Android Services & Security: Programming Started Services (Part 1)
43
Design of the Download Application
-
Android Services & Security: Programming Started Services (Part 1)
44
1 send intent
Design of the Download Application DownloadActivity sends an Intent
via a call to startService() Download
Activity
Intent
startService()
-
Android Services & Security: Programming Started Services (Part 1)
45
1 send intent
Design of the Download Application DownloadActivity sends an Intent
via a call to startService() The DownloadService is
launched on-demand Based on the Activator pattern
Intent
onCreate()
onStartCommand()
Download Service
startService()
Download Activity 2
-
Android Services & Security: Programming Started Services (Part 1)
46
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
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity 2
-
Android Services & Security: Programming Started Services (Part 1)
47
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 1. Creates a ServiceHandler
Associated with a single HandlerThread
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
Create a worker
thread & Handler
3
2
sendMessage()
handleMessage()
downloadImageAndReply()
-
Android Services & Security: Programming Started Services (Part 1)
48
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 1. Creates a ServiceHandler
Associated with a single HandlerThread
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
Create a worker
thread & Handler
3
2
sendMessage()
handleMessage()
downloadImageAndReply()
-
Android Services & Security: Programming Started Services (Part 1)
49
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 1. Creates a ServiceHandler
Associated with a single HandlerThread
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
Create a worker
thread & Handler
3
2
sendMessage()
handleMessage()
downloadImageAndReply()
-
Android Services & Security: Programming Started Services (Part 1)
50
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 1. Creates a ServiceHandler
Associated with a single HandlerThread
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
Create a worker
thread & Handler
3
2
sendMessage()
handleMessage()
downloadImageAndReply()
-
Android Services & Security: Programming Started Services (Part 1)
51
1
See earlier part on Sending & Handling Messages with Android Handler
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 1. Creates a ServiceHandler
Associated with a single HandlerThread
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
Create a worker
thread & Handler
3
2
sendMessage()
handleMessage()
downloadImageAndReply()
-
Android Services & Security: Programming Started Services (Part 1)
52
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 1. Creates a ServiceHandler 2. Receives & queues an Intent
in the ServiceHandler
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
sendMessage()
handleMessage()
downloadImageAndReply()
-
Android Services & Security: Programming Started Services (Part 1)
53
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 1. Creates a ServiceHandler 2. Receives & queues an Intent
in the ServiceHandler
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
sendMessage()
handleMessage()
downloadImageAndReply()
-
Android Services & Security: Programming Started Services (Part 1)
54
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 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
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
sendMessage()
handleMessage()
downloadImageAndReply()
Dequeue Intent
& get file
5
-
Android Services & Security: Programming Started Services (Part 1)
55
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 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
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
sendMessage()
handleMessage()
downloadImageAndReply()
Dequeue Intent
& get file
5
-
Android Services & Security: Programming Started Services (Part 1)
56
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 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
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
See upcoming parts on Activity & Service Communication
sendMessage()
handleMessage()
downloadImageAndReply()
Dequeue Intent
& get file
5
-
Android Services & Security: Programming Started Services (Part 1)
57
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 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
Intent
onCreate()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
sendMessage()
handleMessage()
Dequeue Intent
& get file
5 downloadImageAndReply()
-
Android Services & Security: Programming Started Services (Part 1)
58
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
www.dre.vanderbilt.edu/~schmidt/PDF/CommandRevisited.pdf has more info
Client
3
2 Queue request
-
Android Services & Security: Programming Started Services (Part 1)
59
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
This pattern works if a Service neednt handle multiple requests concurrently
Client
3
2 Queue request
-
Android Services & Security: Programming Started Services (Part 1)
60
Summary
-
Android Services & Security: Programming Started Services (Part 1)
61
The Download Application uses a Started Service to retrieve & display an image from a remote server
Summary
Download Service
Socket
Socket
Send HTTP GET request
Download Activity
-
Android Services & Security: Programming Started Services (Part 1)
62
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 Service
Download Activity
Summary
-
Android Services & Security: Programming Started Services (Part 1)
63
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 Service
Download Activity
Summary
-
Android Services & Security: Programming Started Services (Part 1)
64
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
Summary
-
Android Services & Security: Programming Started Services (Part 1)
65
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
Summary
-
Android Services & Security: Programming Started Services (Part 1)
66
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
Summary
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Service Handler
dequeue Intent & download file
4 3
2 1 Download
Activity
sendMessage()
downloadImageAndReply() 5
handleMessage()
-
Android Services & Security: Programming Started Services (Part 1)
67
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
Summary
onHandleIntent()
process intent
Download Service
3
queue intent
dequeue intent
Service Handler
sendMessage()
onCreate()
onStartCommand()
2
Intent Service
startService()
handleMessage()
4 5
Client Activity
Intent
send intent
1
See upcoming parts on The Command Processor Pattern
Slide Number 1Learning Objectives in this Part of the ModuleDownload Application OverviewDownload Application OverviewDownload Application OverviewDownload Application OverviewDownload Application OverviewDownload Application OverviewDownload Application OverviewDownload Application OverviewDownload Application OverviewDownload Application OverviewTips to Understand the Download ApplicationTips to Understand the Download ApplicationTips to Understand the Download ApplicationProgrammingStarted Services (Part 1)Programming a Started ServiceLaunching a Started ServiceLaunching a Started ServiceLaunching a Started ServiceLaunching a Started ServiceProcessing a Started ServiceProcessing a Started ServiceProcessing a Started ServiceProcessing a Started ServiceProcessing a Started ServiceProgrammingStarted Services (Part 2)Processing a Started ServiceProcessing a Started ServiceProcessing a Started ServiceProcessing a Started ServiceProcessing a Started ServiceProcessing a Started ServiceProcessing a Started ServiceProcessing a Started ServiceProcessing a Started ServiceStopping a Started ServiceStopping a Started ServiceStopping a Started ServiceDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationDesign of the Download ApplicationSlide Number 60SummarySummarySummarySummarySummarySummarySummary