forked from teamclouday/AndroidMic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidMicApp.kt
More file actions
74 lines (61 loc) · 2.19 KB
/
Copy pathAndroidMicApp.kt
File metadata and controls
74 lines (61 loc) · 2.19 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package io.github.teamclouday.androidMic
import android.app.Application
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import android.os.Messenger
import android.util.Log
import io.github.teamclouday.androidMic.domain.service.BIND_SERVICE_ACTION
import io.github.teamclouday.androidMic.domain.service.ForegroundService
import io.github.teamclouday.androidMic.ui.MainActivity
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
private const val TAG = "AndroidMicApp"
class AndroidMicApp : Application() {
companion object {
lateinit var appModule: AppModule
var service: Messenger? = null
var isBound = false
}
private val scope = MainScope()
override fun onCreate() {
super.onCreate()
appModule = AppModuleImpl(this)
scope.launch {
appModule.appPreferences.preload()
}
}
private val mConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
Log.d(TAG, "onServiceConnected")
Companion.service = Messenger(service)
isBound = true
// notify current running activity that service is connected
val notifyIntent = Intent(applicationContext, MainActivity::class.java).apply {
action = Intent.ACTION_VIEW
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra("ForegroundServiceBound", true)
}
startActivity(notifyIntent)
}
override fun onServiceDisconnected(name: ComponentName?) {
Log.d(TAG, "onServiceDisconnected")
service = null
isBound = false
}
}
// start and bind to service
fun bindService() {
val intent = Intent(this, ForegroundService::class.java).apply {
action = BIND_SERVICE_ACTION
}
startService(intent)
bindService(intent, mConnection, BIND_AUTO_CREATE)
}
fun unBindService() {
unbindService(mConnection)
service = null
isBound = false
}
}