fix: Properly stop foreground service

This commit is contained in:
Myzel394 2023-08-17 12:48:52 +02:00
parent 3e41030315
commit 8dcefc59eb
No known key found for this signature in database
GPG Key ID: 50098FCA22080F0F

View File

@ -37,7 +37,6 @@ class AudioRecorderModel: ViewModel() {
val progress: Float
get() = (recordingTime!! / recorderService!!.settings!!.maxDuration).toFloat()
private var intent: Intent? = null
var recorderService: AudioRecorderService? = null
private set
@ -86,15 +85,13 @@ class AudioRecorderModel: ViewModel() {
}
fun startRecording(context: Context) {
reset()
runCatching {
context.unbindService(connection)
}
intent = Intent(context, AudioRecorderService::class.java)
ContextCompat.startForegroundService(context, intent!!)
context.bindService(intent!!, connection, Context.BIND_AUTO_CREATE)
val intent = Intent(context, AudioRecorderService::class.java)
ContextCompat.startForegroundService(context, intent)
context.bindService(intent, connection, Context.BIND_AUTO_CREATE)
}
fun stopRecording(context: Context, saveAsLastRecording: Boolean = true) {
@ -104,9 +101,11 @@ class AudioRecorderModel: ViewModel() {
runCatching {
context.unbindService(connection)
context.stopService(intent)
}
val intent = Intent(context, AudioRecorderService::class.java)
context.stopService(intent)
reset()
}