Services
Jintin
AGENDA
Definition


Service Family


LifeCycle


Close to You


Method


Comparison


Manifest

Q&A
Definition
Application Components
Activities

Services

Content Providers

Broadcast Receivers
What is not a Service..

not a separate process

not a Thread
What a Service is..

long running operation in background

no UI interaction
What a Service is..
Context

ContextIml

Context Wrapper

ContextThemeWrappe
Activity

Service
Lifecycle
Two way Lifecycle
startService

bindService

both..
StartService
start by other Application Component
(such like Activity, Service..) who calling
startService

destroy by cal...
BindService
bind when Application Component
calling bindService()

unbind when calling unbindService()

provide IBinder to...
Methods
Callback Methods
onStartCommand()

onBind()

onCreate()

onDestroy()
onStartCommand()
when Service is start by calling
startService(), onStartCommand will be
called

the Service will indefinit...
onStartCommand()

wrap original onStart() before 2.0

return an Integer to indicate which
behaviour to do when System kill...
onBind()
help to bind Service and other
Application Component by
ServiceConnection

return a IBinder instance to interact
...
Service Connection
onServiceConnected
(ComponentName name,IBinder service)

onServiceDisConnected
(ComponentName name)
onCreate()

called before onStartCommand() and
onBind()

won’t call if Service is already running
onDestroy()
called when the service is no longer
used

should clean up all the resource such
as thread, listener, receiver...
Stop a Service
stopService(Intent intent)

stopSelf()

stopSelf(int startId)
Manifest
Manifest
<manifest ... >

...

<application ... >

<service android:name=".MyService" />

...

</application>

</manifest>
Manifest
support Intent Filter for implicitIntent call

android:exported=”false” to forbid
implicit-Intent call
Service Family
Intent Service
extends Service

auto generate worker thread

generate work queue to avoid
multi-threading
Intent Service
auto stop Service when all request is
done

default onBind() return null

default onStartCommand() redirect...
Close to You
Notify User

Toast Notifications

Status Bar Notifications
Foreground Service
more actively aware

less opportunity to been killed

startForeground() bind with an
Notification for th...
Comparison
Service VS Activity
no UI

won’t killed when screen rotate

Process important hierarchy
(killing priority)

more..
Service VS Thread
Main Thread or not

Context embedded

more..
Service VS Intent Service

Intent Service provide Worker queue
that only allow one Thread to
execute one time

more..
Thank you

!

Email :
JintinApps@gmail.com
Upcoming SlideShare
Loading in …5
×

Android Service Intro

1,479 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,479
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
101
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Android Service Intro

  1. 1. Services Jintin
  2. 2. AGENDA Definition Service Family LifeCycle Close to You Method Comparison Manifest Q&A
  3. 3. Definition
  4. 4. Application Components Activities Services Content Providers Broadcast Receivers
  5. 5. What is not a Service.. not a separate process not a Thread
  6. 6. What a Service is.. long running operation in background no UI interaction
  7. 7. What a Service is.. Context ContextIml Context Wrapper ContextThemeWrappe Activity Service
  8. 8. Lifecycle
  9. 9. Two way Lifecycle startService bindService both..
  10. 10. StartService start by other Application Component (such like Activity, Service..) who calling startService destroy by calling stopService() or stopSelf()
  11. 11. BindService bind when Application Component calling bindService() unbind when calling unbindService() provide IBinder to interact with other component when none is bind, the service destroy
  12. 12. Methods
  13. 13. Callback Methods onStartCommand() onBind() onCreate() onDestroy()
  14. 14. onStartCommand() when Service is start by calling startService(), onStartCommand will be called the Service will indefinitely running you have to call stopSelf() or stopService() to destroy Service
  15. 15. onStartCommand() wrap original onStart() before 2.0 return an Integer to indicate which behaviour to do when System killed it
  16. 16. onBind() help to bind Service and other Application Component by ServiceConnection return a IBinder instance to interact with other Component return null to not allow binding
  17. 17. Service Connection onServiceConnected (ComponentName name,IBinder service) onServiceDisConnected (ComponentName name)
  18. 18. onCreate() called before onStartCommand() and onBind() won’t call if Service is already running
  19. 19. onDestroy() called when the service is no longer used should clean up all the resource such as thread, listener, receivers
  20. 20. Stop a Service stopService(Intent intent) stopSelf() stopSelf(int startId)
  21. 21. Manifest
  22. 22. Manifest <manifest ... > ... <application ... > <service android:name=".MyService" /> ... </application> </manifest>
  23. 23. Manifest support Intent Filter for implicitIntent call android:exported=”false” to forbid implicit-Intent call
  24. 24. Service Family
  25. 25. Intent Service extends Service auto generate worker thread generate work queue to avoid multi-threading
  26. 26. Intent Service auto stop Service when all request is done default onBind() return null default onStartCommand() redirect to onHandleIntent()
  27. 27. Close to You
  28. 28. Notify User Toast Notifications Status Bar Notifications
  29. 29. Foreground Service more actively aware less opportunity to been killed startForeground() bind with an Notification for the status bar to interact with the user like music player
  30. 30. Comparison
  31. 31. Service VS Activity no UI won’t killed when screen rotate Process important hierarchy (killing priority) more..
  32. 32. Service VS Thread Main Thread or not Context embedded more..
  33. 33. Service VS Intent Service Intent Service provide Worker queue that only allow one Thread to execute one time more..
  34. 34. Thank you ! Email : JintinApps@gmail.com

×