Android Service Intro
-
Upload
jintin-lin -
Category
Technology
-
view
872 -
download
0
Transcript of Android Service Intro
![Page 1: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/1.jpg)
ServicesJintin
![Page 2: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/2.jpg)
AGENDA
Definition
LifeCycle
Method
Manifest
Service Family
Close to You
Comparison
Q&A
![Page 3: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/3.jpg)
Definition
![Page 4: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/4.jpg)
Application Components
Activities
Services
Content Providers
Broadcast Receivers
![Page 5: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/5.jpg)
What is not a Service..
not a separate process
not a Thread
![Page 6: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/6.jpg)
What a Service is..
long running operation in background
no UI interaction
![Page 7: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/7.jpg)
What a Service is..Context
Context Wrapper
Activity
ContextIml
Service
ContextThemeWrappe
![Page 8: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/8.jpg)
Lifecycle
![Page 9: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/9.jpg)
Two way Lifecycle
startService
bindService
both..
![Page 10: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/10.jpg)
StartService
start by other Application Component (such like Activity, Service..) who calling startService
destroy by calling stopService() or stopSelf()
![Page 11: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/11.jpg)
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
![Page 12: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/12.jpg)
Methods
![Page 13: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/13.jpg)
Callback Methods
onStartCommand()
onBind()
onCreate()
onDestroy()
![Page 14: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/14.jpg)
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
![Page 15: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/15.jpg)
onStartCommand()
wrap original onStart() before 2.0
return an Integer to indicate which behaviour to do when System killed it
![Page 16: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/16.jpg)
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
![Page 17: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/17.jpg)
Service Connection
onServiceConnected (ComponentName name,IBinder service)
onServiceDisConnected (ComponentName name)
![Page 18: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/18.jpg)
onCreate()
called before onStartCommand() and onBind()
won’t call if Service is already running
![Page 19: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/19.jpg)
onDestroy()
called when the service is no longer used
should clean up all the resource such as thread, listener, receivers
![Page 20: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/20.jpg)
Stop a Service
stopService(Intent intent)
stopSelf()
stopSelf(int startId)
![Page 21: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/21.jpg)
Manifest
![Page 22: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/22.jpg)
Manifest<manifest ... > ... <application ... > <service android:name=".MyService" /> ... </application> </manifest>
![Page 23: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/23.jpg)
Manifest
support Intent Filter for implicit-Intent call
android:exported=”false” to forbid implicit-Intent call
![Page 24: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/24.jpg)
Service Family
![Page 25: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/25.jpg)
Intent Service
extends Service
auto generate worker thread
generate work queue to avoid multi-threading
![Page 26: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/26.jpg)
Intent Service
auto stop Service when all request is done
default onBind() return null
default onStartCommand() redirect to onHandleIntent()
![Page 27: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/27.jpg)
Close to You
![Page 28: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/28.jpg)
Notify User
Toast Notifications
Status Bar Notifications
![Page 29: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/29.jpg)
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
![Page 30: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/30.jpg)
Comparison
![Page 31: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/31.jpg)
Service VS Activity
no UI
won’t killed when screen rotate
Process important hierarchy (killing priority)
more..
![Page 32: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/32.jpg)
Service VS Thread
Main Thread or not
Context embedded
more..
![Page 33: Android Service Intro](https://reader033.fdocuments.net/reader033/viewer/2022060111/556399f3d8b42a7c458b5357/html5/thumbnails/33.jpg)
Service VS Intent Service
Intent Service provide Worker queue that only allow one Thread to execute one time
more..