This document discusses various types of services in Android, including IntentService, which handles asynchronous tasks on a worker thread; starting and stopping services; bound services that can be bound and unbound; isolated services that run in separate processes; exported services that can be accessed by other apps; and using notifications with services. The key points are: IntentService offloads tasks to a worker thread; services can be started and stopped or bound and unbound; isolated services run separately for security; exported services allow inter-app communication; and notifications are used to show status from long-running services.
4. 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.
5. 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
6. Service in AndroidManifest
• android:name=“[package/service]”
• android:enabled=“[true|false]”
• android:exported=“[true|false]”
• android:isolatedProcess=“[true|false]”
• android:process=“[name/of/process]”
9. 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
13. • 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.
14.
15. 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
16. Start and Stop Service
• Context.startService(Intent)
• Context.stopService(Intent)
• Service.stopSelf()
20. 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
25. 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
28. 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
29.
30. 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
31. 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
32.
33.
34. 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
36. 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.)
39. 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.