Android Service

cha122977 1,993 views 42 slides Jul 01, 2016
Slide 1
Slide 1 of 42
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42

About This Presentation

Introduction of Android Service.
This is presentation on 2016/March @ Taiwan Android Developer Study Group


Slide Content

Service@Android Charlie Tsai

Agenda Basic Knowledge Intent Service Start/Stop Service Bound Service Isolated Service Exported Service Notification

Basic

UIThread and WorkerThread UIThread Only UIThread can control the UIWidgets Why? WorkerThread Use WorkerThread to avoid blocking UI Some built-in tools help to use WorkerThread ex: Looper & Handler, AsyncTask.

What is Service? One of application components of Android Need to be declared in AndroidManefest.xml Usually used to do long-tern background work Service can interactive with other component (exported = true) Service can (but not need to) run in another process Service is running in UI Thread

Service in AndroidManifest android:name=“[package/service]” android:enabled=“[ true |false]” android:exported=“[ true |false]” android:isolatedProcess=“[true| false ]” android:process=“[name/of/process]”

Intent Service

Intent Service It is implemented to do something in non-UI thread. we only need to implement the WorkerThread It only execute one at time (queueing) It used HandlerThread It will stop when completed

Start/Stop Service

Can be started/stopped several times, but only one instance. (implicit singleton) No start/stop counting Service can stop itself. (call Service.stopSelf()) If you need to start service for specified event, use Broadcast Receiver.

int onStartCommand The return value define the behavior of service return START_STICKY return START_NOT_STICKY return START_REDELIVER_INTENT flag: START_FLAG_REDELIVERY, START_FLAG_RETRY

Start and Stop Service Context.startService(Intent) Context.stopService(Intent) Service.stopSelf()

Bound Service

Bind Service A service can be bind several times. And service will maintain a bindCount When someone bind service, bindCount + 1 When someone unbind service, bindCount - 1 Service destroy itself when bindCount == 0

Context.bindService() bindService(Intent, ServiceConnection, int flags); flags: BIND_AUTO_CREATE BIND_NOT_FOREGROUND BIND_ABOVE_CLIENT BIND_ALLOW_OOM_MANAGEMENT BIND_WAIVE_PRIORITY

Hyper Service (start/stop + bind)

Hyper Service Service create: startService() or bindService() when no service created Service destroy: if has been started: stopService()(or stopSelf()) && if has been bound: bindCount == 0

Isolated Service

Isolated Service android:isolatedProcess=“true” android:process=“ : [process_name]” (Note: the “:” is needed) Even you give two isolated processes the same process name, they will NOT run in same process. Thus, you get at least 3 process: app, service1, and service2 The process names of service1 and service2 are same

IPC issues # IPC = Inter Process Communication Since the service is run in another process, we can not call its function directly Solution: Messenger: Simple, but the calling order is not guarantee AIDL: Hard to implement, guarantee the calling order

Messenger Create a Handler to receive commands Create a Messenger and use Handler return Messenger.getBinder() in onBind() disadvantage: The arguments cannot be customized The messenger system is asynchronized # function may not run immediately when called

AIDL the function call is synchronized # function call is blocked until return This is too complexity so we don’t discuss here Please check official document: http://developer.android.com/intl/zh-tw/guide/components/aidl.html

Exported Service

Exported Service Exported Service make you use other application’s service The exported service is run in the process of its application, NOT in the process of caller If used as bound service, the binder of it should support IPC (Thus, it usually used with AIDL.)

Notification

Show Notification Notification is part of Service Use NotificationCompat.Builder startForeground(int notificationId, Notification) # the notificationId must NOT be 0 startForeground with same id will replace the previous notification which has same id.

Conclusion IntentService Start / Stop Service Bound Service (bind/unbind) Isolated Service Exported Service Notification

Thanks!