android service

42
Service@Android Charlie Tsai

Upload: charile-tsai

Post on 09-Jan-2017

416 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Android Service

Service@AndroidCharlie Tsai

Page 2: Android Service

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

Page 3: Android Service

Basic

Page 4: Android Service

UIThread and WorkerThread

• UIThreadOnly UIThread can control the UIWidgetsWhy?

• WorkerThreadUse WorkerThread to avoid blocking UISome built-in tools help to use WorkerThreadex: Looper & Handler, AsyncTask.

Page 5: Android Service

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

Page 6: Android Service

Service in AndroidManifest

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

Page 7: Android Service
Page 8: Android Service

Intent Service

Page 9: Android 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

Page 10: Android Service
Page 11: Android Service

Start/Stop Service

Page 12: Android Service
Page 13: Android 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.

Page 14: Android Service
Page 15: Android Service

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

Page 16: Android Service

Start and Stop Service

• Context.startService(Intent)• Context.stopService(Intent)• Service.stopSelf()

Page 17: Android Service
Page 18: Android Service

Bound Service

Page 19: Android Service
Page 20: Android 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

Page 21: Android Service

Context.bindService()

• bindService(Intent, ServiceConnection, int flags);

• flags:BIND_AUTO_CREATEBIND_NOT_FOREGROUNDBIND_ABOVE_CLIENTBIND_ALLOW_OOM_MANAGEMENTBIND_WAIVE_PRIORITY

Page 22: Android Service
Page 23: Android Service
Page 24: Android Service

Hyper Service(start/stop + bind)

Page 25: Android Service

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

Page 26: Android Service
Page 27: Android Service

Isolated Service

Page 28: Android 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 service2The process names of service1 and service2 are same

Page 29: Android Service
Page 30: Android Service

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 guaranteeAIDL: Hard to implement, guarantee the calling order

Page 31: Android Service

Messenger• Create a Handler to receive commands• Create a Messenger and use Handler• return Messenger.getBinder() in onBind()• disadvantage:

The arguments cannot be customizedThe messenger system is asynchronized# function may not run immediately when called

Page 32: Android Service
Page 33: Android Service
Page 34: Android Service

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

Page 35: Android Service

Exported Service

Page 36: Android 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.)

Page 37: Android Service
Page 38: Android Service

Notification

Page 39: Android Service

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.

Page 40: Android Service
Page 41: Android Service

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

Page 42: Android Service

Thanks!