-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomService.kt
More file actions
42 lines (33 loc) · 1.61 KB
/
CustomService.kt
File metadata and controls
42 lines (33 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class CustomService : Service() {
//use worker thread if long running operation to not block main UI thread
//or provide some multithreading if needed
private val handlerThread = HandlerThread("HandlerThread")
private lateinit var handler : Handler
override fun onBind(intent: Intent?): IBinder? {
//invokes when bindService called, retrieve intent and decide what to do
//if service is created by bindService and onStartCommand wasn't called then runs only as components are bound
return null //provide communication interface or return null when no bind needed
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//invokes when startService or startForegroundService called, retrieve intent and decide what to do
//continues to run until stops itself or by another component
return START_STICKY //restart service strategy after destroyed by the system
}
override fun onCreate() {
//invokes one time setup before onStartCommand or onBind
//not called when service already running
super.onCreate()
handlerThread.start()
handler = Handler(handlerThread.looper)
}
override fun onDestroy() {
//invokes when stopSelf or stopService is called
//service is no longer used or is being destroyed by system or client
super.onDestroy()
handler.removeCallbacksAndMessages(null)
handlerThread.quitSafely()
handlerThread.interrupt()
}
//more lifecycle callbacks
}
//run startService or bindService and pass Intent to run Service