Android Service Intro

34
Services Jintin

Transcript of Android Service Intro

Page 1: Android Service Intro

ServicesJintin

Page 2: Android Service Intro

AGENDA

Definition

LifeCycle

Method

Manifest

Service Family

Close to You

Comparison

Q&A

Page 3: Android Service Intro

Definition

Page 4: Android Service Intro

Application Components

Activities

Services

Content Providers

Broadcast Receivers

Page 5: Android Service Intro

What is not a Service..

not a separate process

not a Thread

Page 6: Android Service Intro

What a Service is..

long running operation in background

no UI interaction

Page 7: Android Service Intro

What a Service is..Context

Context Wrapper

Activity

ContextIml

Service

ContextThemeWrappe

Page 8: Android Service Intro

Lifecycle

Page 9: Android Service Intro

Two way Lifecycle

startService

bindService

both..

Page 10: Android Service Intro

StartService

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

destroy by calling stopService() or stopSelf()

Page 11: Android Service Intro

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

Methods

Page 13: Android Service Intro

Callback Methods

onStartCommand()

onBind()

onCreate()

onDestroy()

Page 14: Android Service Intro

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

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

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

Service Connection

onServiceConnected (ComponentName name,IBinder service)

onServiceDisConnected (ComponentName name)

Page 18: Android Service Intro

onCreate()

called before onStartCommand() and onBind()

won’t call if Service is already running

Page 19: Android Service Intro

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

Stop a Service

stopService(Intent intent)

stopSelf()

stopSelf(int startId)

Page 21: Android Service Intro

Manifest

Page 22: Android Service Intro

Manifest<manifest ... > ... <application ... > <service android:name=".MyService" /> ... </application> </manifest>

Page 23: Android Service Intro

Manifest

support Intent Filter for implicit-Intent call

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

Page 24: Android Service Intro

Service Family

Page 25: Android Service Intro

Intent Service

extends Service

auto generate worker thread

generate work queue to avoid multi-threading

Page 26: Android Service Intro

Intent Service

auto stop Service when all request is done

default onBind() return null

default onStartCommand() redirect to onHandleIntent()

Page 27: Android Service Intro

Close to You

Page 28: Android Service Intro

Notify User

Toast Notifications

Status Bar Notifications

Page 29: Android Service Intro

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

Comparison

Page 31: Android Service Intro

Service VS Activity

no UI

won’t killed when screen rotate

Process important hierarchy (killing priority)

more..

Page 32: Android Service Intro

Service VS Thread

Main Thread or not

Context embedded

more..

Page 33: Android Service Intro

Service VS Intent Service

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

more..

Page 34: Android Service Intro

Thank you !

Email : [email protected]