android service
TRANSCRIPT
Service@AndroidCharlie Tsai
Agenda• Basic Knowledge• Intent Service• Start/Stop Service• Bound Service• Isolated Service• Exported Service• Notification
Basic
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.
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_CREATEBIND_NOT_FOREGROUNDBIND_ABOVE_CLIENTBIND_ALLOW_OOM_MANAGEMENTBIND_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 service2The 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 guaranteeAIDL: 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 customizedThe 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!