s2 m1 p4 android intentservice
DESCRIPTION
describes Android IntentServiceTRANSCRIPT
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: Android IntentService
Android Services & Security: Android IntentService
2
Learning Objectives in this Part of the Module • Understand how the Android IntentService provides a framework for
programming Started Services that concurrently process commands expressed as Intents
developer.android.com/reference/android/app/IntentService.html has more
Android Services & Security: Android IntentService
3
Dequeue Intent
& get file
Motivation for IntentService • IntentService codifies an
idiom used in Android 1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
downloadImageAndReply()
handleMessage()
See previous part on “Programming Started Services”
5
sendMessage()
Android Services & Security: Android IntentService
4
Dequeue Intent
& get file
Motivation for IntentService • IntentService codifies an
idiom used in Android 1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
downloadImageAndReply()
handleMessage() 5
sendMessage()
Android Services & Security: Android IntentService
5
Motivation for IntentService • IntentService codifies an
idiom used in Android • Service.onCreate()
1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
handleMessage()
void onCreate() { ... HandlerThread thread = new HandlerThread("DownloadService"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); ...
Dequeue Intent
& get file
downloadImageAndReply() 5
sendMessage()
Android Services & Security: Android IntentService
6
Motivation for IntentService • IntentService codifies an
idiom used in Android • Service.onCreate()
1. Create/start a HandlerThread
1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
handleMessage()
void onCreate() { ... HandlerThread thread = new HandlerThread("DownloadService"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); ...
Dequeue Intent
& get file
downloadImageAndReply() 5
sendMessage()
Android Services & Security: Android IntentService
7
Motivation for IntentService • IntentService codifies an
idiom used in Android • Service.onCreate()
1. Create/start a HandlerThread 2. Give the Thread-specific Looper
to an instance of a ServiceHandler
1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
handleMessage()
void onCreate() { ... HandlerThread thread = new HandlerThread("DownloadService"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); ...
Dequeue Intent
& get file
downloadImageAndReply() 5
sendMessage()
Android Services & Security: Android IntentService
8
Motivation for IntentService • IntentService codifies an
idiom used in Android • Service.onCreate() • Service.onStartCommand()
1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
handleMessage() int onStartCommand(Intent intent, int f, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); ...
Dequeue Intent
& get file
downloadImageAndReply() 5
sendMessage()
Android Services & Security: Android IntentService
9
Motivation for IntentService • IntentService codifies an
idiom used in Android • Service.onCreate() • Service.onStartCommand()
1.Create a Message encapsulating the Intent parameter
1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
handleMessage() int onStartCommand(Intent intent, int f, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); ...
Dequeue Intent
& get file
downloadImageAndReply() 5
sendMessage()
Android Services & Security: Android IntentService
10
Motivation for IntentService • IntentService codifies an
idiom used in Android • Service.onCreate() • Service.onStartCommand()
1.Create a Message encapsulating the Intent parameter
2.Send Message to ServiceHandler
1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
sendMessage()
handleMessage() int onStartCommand(Intent intent, int f, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); ...
Dequeue Intent
& get file
downloadImageAndReply() 5
Android Services & Security: Android IntentService
11
Motivation for IntentService • IntentService codifies an
idiom used in Android • Service.onCreate() • Service.onStartCommand() • ServiceHandler.handleMessage()
1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
handleMessage()
void handleMessage(Message msg) { downloadImageAndReply ((Intent) msg.obj); stopSelf(msg.arg1);
Dequeue Intent
& get file
downloadImageAndReply() 5
sendMessage()
Android Services & Security: Android IntentService
12
Motivation for IntentService • IntentService codifies an
idiom used in Android • Service.onCreate() • Service.onStartCommand() • ServiceHandler.handleMessage()
1. Process the Message containing the Intent
1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
handleMessage()
void handleMessage(Message msg) { downloadImageAndReply ((Intent) msg.obj); stopSelf(msg.arg1);
Dequeue Intent
& get file
downloadImageAndReply() 5
sendMessage()
Android Services & Security: Android IntentService
13
Motivation for IntentService • IntentService codifies an
idiom used in Android • Service.onCreate() • Service.onStartCommand() • ServiceHandler.handleMessage()
1. Process the Message containing the Intent 2. Have the Service stop itself
1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
handleMessage()
void handleMessage(Message msg) { downloadImageAndReply ((Intent) msg.obj); stopSelf(msg.arg1);
Dequeue Intent
& get file
downloadImageAndReply() 5
sendMessage()
Android Services & Security: Android IntentService
14
Motivation for IntentService • IntentService codifies an
idiom used in Android • This idioms appears in a number
of packaged applications, e.g. • packages/apps/Calendar/src/com/ android/calendar/alerts/ AlertService.java
• packages/apps/Mms/src/com/ android/mms/transaction/ SmsReceiverService.java
• packages/apps/Mms/src/com/ android/mms/transaction/ TransactionService.java
1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
sendMessage()
handleMessage()
Dequeue Intent
& get file
downloadImageAndReply() 5
Android Services & Security: Android IntentService
15
Motivation for IntentService • IntentService codifies an
idiom used in Android • This idioms appears in a number
of packaged applications, e.g. • packages/apps/Calendar/src/com/ android/calendar/alerts/ AlertService.java
• packages/apps/Mms/src/com/ android/mms/transaction/ SmsReceiverService.java
• packages/apps/Mms/src/com/ android/mms/transaction/ TransactionService.java
1
Intent
onCreate()
onStartCommand()
Download Service
send intent startService()
Download Activity
Service Handler
4 3
2
sendMessage()
handleMessage()
Dequeue Intent
& get file
downloadImageAndReply() 5
Android Services & Security: Android IntentService
16
Overview of the IntentService
(Part 1)
Android Services & Security: Android IntentService
17
Overview of IntentService • IntentService codifies the idiom
for concurrent processing of Intents into a framework
public class IntentService extends Service { ... protected abstract void onHandleIntent(Intent intent); }
Android Services & Security: Android IntentService
18
Overview of IntentService • IntentService codifies the idiom
for concurrent processing of Intents into a framework
developer.android.com/reference/android/app/IntentService.html has more
public class IntentService extends Service { ... protected abstract void onHandleIntent(Intent intent); }
Android Services & Security: Android IntentService
19
Overview of IntentService • IntentService codifies the idiom
for concurrent processing of Intents into a framework • It’s very easy to use
Subclasses simply override this hook method to process an Intent
in a single background Thread
public class IntentService extends Service { ... protected abstract void onHandleIntent(Intent intent); }
Android Services & Security: Android IntentService
20
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
Overview of IntentService
See previous parts on “Programming Started Services”
1 Client
Intent Service
onCreate()
onStartCommand()
…
send intent
process intent
4
startService()
Download Service
onHandleIntent()
2
3
queue intent
dequeue intent
Service Handler
sendMessage()
handleMessage()
Intent
Android Services & Security: Android IntentService
21
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
1 Client send intent startService()
Overview of IntentService Intent
Android Services & Security: Android IntentService
22
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService() • Data & objects can be
passed to the Service by putting “extras” into Intents
1 Client send intent startService()
Overview of IntentService
www.itcuties.com/android/intent-putextra has more info on extras
Intent
Android Services & Security: Android IntentService
23
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService() • Data & objects can be
passed to the Service by putting “extras” into Intents
• The IntentService is launched on-demand via the Activator pattern
Client
Intent Service
onCreate()
onStartCommand()
www.dre.vanderbilt.edu/~schmidt/PDF/Activator.pdf has more on Activator
send intent startService()
Overview of IntentService Intent
Android Services & Security: Android IntentService
24
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent() • This hook method processes
the Intent sent by the client in a background Thread
Client
Intent Service
onCreate()
onStartCommand()
Download Service
onHandleIntent()
…
send intent startService()
Overview of IntentService Intent
developer.android.com/guide/components/services.html#ExtendingIntentService
Android Services & Security: Android IntentService
25
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent() • This hook method processes
the Intent sent by the client in a background Thread
Client
Intent Service
onCreate()
onStartCommand()
Download Service
onHandleIntent()
…
send intent startService()
Overview of IntentService Intent
void onHandleIntent(Intent intent) { downloadImageAndReply(intent); }
Android Services & Security: Android IntentService
26
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent() • This hook method processes
the Intent sent by the client in a background Thread
Client
Intent Service
onCreate()
onStartCommand()
Download Service
onHandleIntent()
…
send intent startService()
Overview of IntentService Intent
void onHandleIntent(Intent intent) { downloadImageAndReply(intent); }
Android Services & Security: Android IntentService
27
Overview of the IntentService
(Part 2)
Android Services & Security: Android IntentService
28
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent()
• The IntentService does several things
Client
Intent Service
onCreate()
onStartCommand()
2
…
3
send intent
queue intent
dequeue intent process intent
4
startService()
Overview of IntentService
Download Service
onHandleIntent()
Service Handler
sendMessage()
handleMessage()
frameworks/base/core/java/android/os/IntentService.java has the code
Intent
Android Services & Security: Android IntentService
29
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent()
• The IntentService does several things • Creates a ServiceHandler
Client
Intent Service
onCreate()
onStartCommand()
2
…
3
send intent
queue intent
dequeue intent process intent
4
startService()
Overview of IntentService
Download Service
onHandleIntent()
Service Handler
sendMessage()
handleMessage()
Intent
Android Services & Security: Android IntentService
30
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent()
• The IntentService does several things • Creates a ServiceHandler
Client
Intent Service
onStartCommand()
2
…
3
send intent
queue intent
dequeue intent process intent
4
startService()
Overview of IntentService
Download Service
onHandleIntent()
Service Handler
sendMessage()
handleMessage()
Intent
onCreate()
Android Services & Security: Android IntentService
31
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent()
• The IntentService does several things • Creates a ServiceHandler
• Internally creates a single background thread
Client
Intent Service
onCreate()
onStartCommand()
2
…
3
send intent
queue intent
dequeue intent process intent
4
startService()
Overview of IntentService
Download Service
onHandleIntent()
Service Handler
sendMessage()
handleMessage()
Intent
Android Services & Security: Android IntentService
32
1
onStartCommand()
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent()
• The IntentService does several things • Creates a ServiceHandler • Receives & queues
Intents in ServiceHandler
Client
Intent Service
send intent startService()
Overview of IntentService
onCreate()
…
process intent
4 onHandleIntent()
Download Service
2
3
queue intent
dequeue intent
Service Handler
sendMessage()
handleMessage()
Intent
Android Services & Security: Android IntentService
33
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent()
• The IntentService does several things • Creates a ServiceHandler • Receives & queues
Intents in ServiceHandler
Client
Intent Service
send intent startService()
Overview of IntentService
onCreate()
…
process intent
4 onHandleIntent()
Download Service
2
3
queue intent
dequeue intent
Service Handler
sendMessage()
handleMessage()
Intent
onStartCommand()
Android Services & Security: Android IntentService
34
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent()
• The IntentService does several things • Creates a ServiceHandler • Receives & queues
Intents in ServiceHandler • Processes the Intents “in
the background”
Client
Intent Service
send intent startService()
Overview of IntentService
onCreate()
…
process intent
4 onHandleIntent()
Download Service
2
3
queue intent
dequeue intent
Service Handler
sendMessage()
handleMessage()
Intent
onStartCommand()
Android Services & Security: Android IntentService
35
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent()
• The IntentService does several things • Creates a ServiceHandler • Receives & queues
Intents in ServiceHandler • Processes the Intents “in
the background”
Client
Intent Service
send intent startService()
Overview of IntentService
onCreate()
…
process intent
4 onHandleIntent()
Download Service
2
3
queue intent
dequeue intent
Service Handler
sendMessage()
handleMessage()
Intent
Only one Intent processed at a time by the one & only Thread
onStartCommand()
Android Services & Security: Android IntentService
36
1
• IntentService codifies the idiom for concurrent processing of Intents into a framework
• Clients send Intents via calls to startService()
• A subclass of IntentService implements onHandleIntent()
• The IntentService does several things • Creates a ServiceHandler • Receives & queues
Intents in ServiceHandler • Processes the Intents “in
the background” • Stops Service when there are no more Intents to process
Client
Intent Service
onCreate()
onStartCommand()
…
send intent
process intent
4
startService()
Overview of IntentService
Download Service
onHandleIntent()
2
3
queue intent
dequeue intent
Service Handler
sendMessage()
handleMessage()
Intent
In contrast, a Service must stop itself manually via stopSelf()
Android Services & Security: Android IntentService
37
The IntentService
Implementation
Android Services & Security: Android IntentService
38
Implementation of IntentService
frameworks/base/core/java/android/os/IntentService.java has the code
Android Services & Security: Android IntentService
39
Implementation of IntentService public class IntentService extends Service { ...
Base class for Services that handle asynchronous requests (expressed as Intents) on demand
Android Services & Security: Android IntentService
40
Implementation of IntentService public class IntentService extends Service { ...
Base class for Services that handle asynchronous requests (expressed as Intents) on demand
Android Services & Security: Android IntentService
41
Implementation of IntentService public class IntentService extends Service { ... private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); }
Useful data members
The volatile keyword ensures data members are properly visible to Threads
Android Services & Security: Android IntentService
42
Implementation of IntentService public class IntentService extends Service { ... private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); }
Called when IntentService is first created
Android Services & Security: Android IntentService
43
Implementation of IntentService public class IntentService extends Service { ... private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); }
Create/start a separate HandlerThread to process the Intent concurrently in the background
Android Services & Security: Android IntentService
44
Implementation of IntentService public class IntentService extends Service { ... private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); }
Create/start a separate HandlerThread to process the Intent concurrently in the background
See earlier part on “Android Looper”
Android Services & Security: Android IntentService
45
Implementation of IntentService public class IntentService extends Service { ... private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread ("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); }
Get the HandlerThread's Looper & use it for our Handler
Android Services & Security: Android IntentService
46
Implementation of IntentService public class IntentService extends Service { ... private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread ("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); }
Get the HandlerThread's Looper & use it for our Handler
Android Services & Security: Android IntentService
47
Implementation of IntentService public class IntentService extends Service { ... public int onStartCommand(Intent intent, int f, int startId) { onStart(intent, startId); return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; } public void onStart(Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); } ...
Called each time a Started Service is sent an Intent via startService()
Android Services & Security: Android IntentService
48
Implementation of IntentService public class IntentService extends Service { ... public int onStartCommand(Intent intent, int f, int startId) { onStart(intent, startId); return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; } public void onStart(Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); } ...
Helper method
Android Services & Security: Android IntentService
49
Implementation of IntentService public class IntentService extends Service { ... public int onStartCommand(Intent intent, int f, int startId) { onStart(intent, startId); return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; } public void onStart(Intent intent, int startId) Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); } ...
Forwards Intent to the ServiceHandler
Android Services & Security: Android IntentService
50
Implementation of IntentService public class IntentService extends Service { ... public int onStartCommand(Intent intent, int f, int startId) { onStart(intent, startId); return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; } public void onStart(Intent intent, int startId) Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); } ...
Create a Message
Android Services & Security: Android IntentService
51
Implementation of IntentService public class IntentService extends Service { ... public int onStartCommand(Intent intent, int f, int startId) { onStart(intent, startId); return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; } public void onStart(Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); } ...
Include Intent & start ID in Message to guide subsequent processing & shutdown
Android Services & Security: Android IntentService
52
Implementation of IntentService public class IntentService extends Service { ... public int onStartCommand(Intent intent, int f, int startId) { onStart(intent, startId); return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; } public void onStart(Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); } ...
Send Message to ServiceHandler for processing in the background Thread
Android Services & Security: Android IntentService
53
Implementation of IntentService public class IntentService extends Service { ... private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } public void handleMessage(Message msg) { onHandleIntent((Intent) msg.obj); stopSelf(msg.arg1); } } ...
Receives Messages passed via sendMessage()
Android Services & Security: Android IntentService
54
Implementation of IntentService public class IntentService extends Service { ... private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } public void handleMessage(Message msg) { onHandleIntent((Intent) msg.obj); stopSelf(msg.arg1); } } ...
Receives Messages passed via sendMessage()
Android Services & Security: Android IntentService
55
Implementation of IntentService public class IntentService extends Service { ... private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } public void handleMessage(Message msg) { onHandleIntent((Intent) msg.obj); stopSelf(msg.arg1); } } ...
Dispatch a callback hook method to process the Intent concurrently
Android Services & Security: Android IntentService
56
Implementation of IntentService public class IntentService extends Service { ... private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } public void handleMessage(Message msg) { onHandleIntent((Intent) msg.obj); stopSelf(msg.arg1); } } ...
Dispatch a callback hook method to process the Intent concurrently
Android Services & Security: Android IntentService
57
Implementation of IntentService public class IntentService extends Service { ... protected abstract void onHandleIntent(Intent intent); ... Must be overridden by
subclasses to process the Intent concurrently
Only one Intent at a time is processed concurrently
Android Services & Security: Android IntentService
58
Implementation of IntentService public class IntentService extends Service { ... private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } public void handleMessage(Message msg) { onHandleIntent((Intent) msg.obj); stopSelf(msg.arg1); } } ...
Stop the service using the startId, so that we don't stop the service in the middle of handling another job
See previous part on “Programming Started Services”
Android Services & Security: Android IntentService
59
Implementation of IntentService public class IntentService extends Service { ... public void onDestroy() { mServiceLooper.quit(); } }
Shutdown the looper
Android Services & Security: Android IntentService
60
Summary
Android Services & Security: Android IntentService
61
• IntentService provides a framework that codifies a common Android idiom
Summary
onHandleIntent()
…
process intent
Download Service
Service Handler
sendMessage()
onCreate()
onStartCommand()
Intent Intent Service
send intent
startService()
handleMessage()
Client
Android Services & Security: Android IntentService
62
• IntentService provides a framework that codifies a common Android idiom • It creates a Handler
Thread that processes Intent commands in the background
Summary
onHandleIntent()
…
process intent
Download Service
Service Handler
sendMessage()
onCreate()
onStartCommand()
Intent Intent Service
send intent
startService()
handleMessage()
Client
Android Services & Security: Android IntentService
63
• IntentService provides a framework that codifies a common Android idiom • It creates a Handler
Thread that processes Intent commands in the background
• It also uses the HaMeR framework to dispatch calls to onHandleIntent()
Summary
onHandleIntent()
…
process intent
Download Service
3
queue intent
dequeue intent
Service Handler
sendMessage()
onCreate()
onStartCommand()
2
Intent Intent Service
send intent
startService()
handleMessage()
4 5
1 Client
Android Services & Security: Android IntentService
64
• IntentService provides a framework that codifies a common Android idiom • It creates a Handler
Thread that processes Intent commands in the background
• It also uses the HaMeR framework to dispatch calls to onHandleIntent()
Summary
onHandleIntent()
…
process intent
Download Service
3
queue intent
dequeue intent
Service Handler
sendMessage()
onCreate()
onStartCommand()
2
Intent Intent Service
send intent
startService()
handleMessage()
4 5
1 Client
IntentService is designed for Services that process one Intent request at a time
Android Services & Security: Android IntentService
65
• IntentService provides a framework that codifies a common Android idiom • It creates a Handler
Thread that processes Intent commands in the background
• It also uses the HaMeR framework to dispatch calls to onHandleIntent()
Summary
onHandleIntent()
…
process intent
Download Service
3
queue intent
dequeue intent
Service Handler
sendMessage()
onCreate()
onStartCommand()
2
Intent Intent Service
send intent
startService()
handleMessage()
4 5
1 Client
Executors can be used to process Intents concurrently in a pool of Threads
Android Services & Security: Android IntentService
66
Summary • IntentService provides a
framework that codifies a common Android idiom • It creates a Handler
Thread that processes Intent commands in the background
• It also uses the HaMeR framework to serialize calls to onHandleIntent()
• It applies the Command Processor pattern
Client
3
2 Queue request
See upcoming parts on “The Command Processor Pattern”
Android Services & Security: Android IntentService
67 See previous parts on “Programming Started Services”
Summary • IntentService provides a
framework that codifies a common Android idiom • It creates a Handler
Thread that processes Intent commands in the background
• It also uses the HaMeR framework to serialize calls to onHandleIntent()
• It applies the Command Processor pattern
• It shows how frameworks evolve organically by refactoring software
Groundhog Day
Android Services & Security: Android IntentService
68
packages/apps/Calendar/src/com/android/calendar/alerts/ DismissAllAlarmsService.java packages/apps/CellBroadcastReceiver/src/com/android/cellbroadcastreceiver/ CellBroadcastDatabaseService.java packages/apps/Contacts/src/com/android/contacts/ContactSaveService.java packages/apps/Email/src/com/android/email/service/ EmailBroadcastProcessorService.java packages/apps/Exchange/src/com/android/exchange/service/ ExchangeBroadcastProcessorService.java packages/apps/MusicFX/src/com/android/musicfx/Compatibility.java packages/apps/Phone/src/com/android/phone/ClearMissedCallsService.java
Summary • IntentService provides a
framework that codifies a common Android idiom
• IntentService is heavily used in Android’s packaged applications
Android Services & Security: Android IntentService
69
packages/apps/Calendar/src/com/android/calendar/alerts/ DismissAllAlarmsService.java packages/apps/CellBroadcastReceiver/src/com/android/cellbroadcastreceiver/ CellBroadcastDatabaseService.java packages/apps/Contacts/src/com/android/contacts/ContactSaveService.java packages/apps/Email/src/com/android/email/service/ EmailBroadcastProcessorService.java packages/apps/Exchange/src/com/android/exchange/service/ ExchangeBroadcastProcessorService.java packages/apps/MusicFX/src/com/android/musicfx/Compatibility.java packages/apps/Phone/src/com/android/phone/ClearMissedCallsService.java
Summary • IntentService provides a
framework that codifies a common Android idiom
• IntentService is heavily used in Android’s packaged applications
Android Services & Security: Android IntentService
70
packages/apps/Calendar/src/com/android/calendar/alerts/ DismissAllAlarmsService.java packages/apps/CellBroadcastReceiver/src/com/android/cellbroadcastreceiver/ CellBroadcastDatabaseService.java packages/apps/Contacts/src/com/android/contacts/ContactSaveService.java packages/apps/Email/src/com/android/email/service/ EmailBroadcastProcessorService.java packages/apps/Exchange/src/com/android/exchange/service/ ExchangeBroadcastProcessorService.java packages/apps/MusicFX/src/com/android/musicfx/Compatibility.java packages/apps/Phone/src/com/android/phone/ClearMissedCallsService.java
Summary • IntentService provides a
framework that codifies a common Android idiom
• IntentService is heavily used in Android’s packaged applications
www.vogella.com/articles/AndroidServices/article.html#service_intentservices