Top Banner
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
70

S2 M1 P4 Android IntentService

Jul 21, 2016

Download

Documents

phalaar

describes Android IntentService
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: S2 M1 P4 Android IntentService

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

Page 2: S2 M1 P4 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

Page 3: S2 M1 P4 Android IntentService

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()

Page 4: S2 M1 P4 Android IntentService

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()

Page 5: S2 M1 P4 Android IntentService

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()

Page 6: S2 M1 P4 Android IntentService

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()

Page 7: S2 M1 P4 Android IntentService

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()

Page 8: S2 M1 P4 Android IntentService

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()

Page 9: S2 M1 P4 Android IntentService

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()

Page 10: S2 M1 P4 Android IntentService

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

Page 11: S2 M1 P4 Android IntentService

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()

Page 12: S2 M1 P4 Android IntentService

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()

Page 13: S2 M1 P4 Android IntentService

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()

Page 14: S2 M1 P4 Android IntentService

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

Page 15: S2 M1 P4 Android IntentService

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

Page 16: S2 M1 P4 Android IntentService

Android Services & Security: Android IntentService

16

Overview of the IntentService

(Part 1)

Page 17: S2 M1 P4 Android IntentService

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); }

Page 18: S2 M1 P4 Android IntentService

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); }

Page 19: S2 M1 P4 Android IntentService

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); }

Page 20: S2 M1 P4 Android IntentService

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

Page 21: S2 M1 P4 Android IntentService

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

Page 22: S2 M1 P4 Android IntentService

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

Page 23: S2 M1 P4 Android IntentService

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

Page 24: S2 M1 P4 Android IntentService

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

Page 25: S2 M1 P4 Android IntentService

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); }

Page 26: S2 M1 P4 Android IntentService

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); }

Page 27: S2 M1 P4 Android IntentService

Android Services & Security: Android IntentService

27

Overview of the IntentService

(Part 2)

Page 28: S2 M1 P4 Android IntentService

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

Page 29: S2 M1 P4 Android IntentService

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

Page 30: S2 M1 P4 Android IntentService

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()

Page 31: S2 M1 P4 Android IntentService

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

Page 32: S2 M1 P4 Android IntentService

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

Page 33: S2 M1 P4 Android IntentService

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()

Page 34: S2 M1 P4 Android IntentService

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()

Page 35: S2 M1 P4 Android IntentService

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()

Page 36: S2 M1 P4 Android IntentService

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()

Page 37: S2 M1 P4 Android IntentService

Android Services & Security: Android IntentService

37

The IntentService

Implementation

Page 38: S2 M1 P4 Android IntentService

Android Services & Security: Android IntentService

38

Implementation of IntentService

frameworks/base/core/java/android/os/IntentService.java has the code

Page 39: S2 M1 P4 Android IntentService

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

Page 40: S2 M1 P4 Android IntentService

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

Page 41: S2 M1 P4 Android IntentService

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

Page 42: S2 M1 P4 Android IntentService

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

Page 43: S2 M1 P4 Android IntentService

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

Page 44: S2 M1 P4 Android IntentService

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”

Page 45: S2 M1 P4 Android IntentService

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

Page 46: S2 M1 P4 Android IntentService

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

Page 47: S2 M1 P4 Android IntentService

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()

Page 48: S2 M1 P4 Android IntentService

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

Page 49: S2 M1 P4 Android IntentService

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

Page 50: S2 M1 P4 Android IntentService

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

Page 51: S2 M1 P4 Android IntentService

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

Page 52: S2 M1 P4 Android IntentService

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

Page 53: S2 M1 P4 Android IntentService

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()

Page 54: S2 M1 P4 Android IntentService

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()

Page 55: S2 M1 P4 Android IntentService

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

Page 56: S2 M1 P4 Android IntentService

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

Page 57: S2 M1 P4 Android IntentService

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

Page 58: S2 M1 P4 Android IntentService

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”

Page 59: S2 M1 P4 Android IntentService

Android Services & Security: Android IntentService

59

Implementation of IntentService public class IntentService extends Service { ... public void onDestroy() { mServiceLooper.quit(); } }

Shutdown the looper

Page 60: S2 M1 P4 Android IntentService

Android Services & Security: Android IntentService

60

Summary

Page 61: S2 M1 P4 Android IntentService

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

Page 62: S2 M1 P4 Android IntentService

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

Page 63: S2 M1 P4 Android IntentService

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

Page 64: S2 M1 P4 Android IntentService

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

Page 65: S2 M1 P4 Android IntentService

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

Page 66: S2 M1 P4 Android IntentService

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”

Page 67: S2 M1 P4 Android IntentService

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

Page 68: S2 M1 P4 Android IntentService

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

Page 69: S2 M1 P4 Android IntentService

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

Page 70: S2 M1 P4 Android IntentService

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