make sure we launch startForeground from the main thread

This commit is contained in:
tibbi
2022-05-19 10:30:45 +02:00
parent d77dd0e992
commit 732704c5d4
5 changed files with 17 additions and 5 deletions

View File

@ -5,7 +5,9 @@ import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import com.simplemobiletools.clock.R
@ -110,7 +112,9 @@ class StopwatchService : Service() {
}
fun startStopwatchService(context: Context) {
ContextCompat.startForegroundService(context, Intent(context, StopwatchService::class.java))
Handler(Looper.getMainLooper()).post {
ContextCompat.startForegroundService(context, Intent(context, StopwatchService::class.java))
}
}
object StopwatchStopService

View File

@ -6,7 +6,9 @@ import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import com.simplemobiletools.clock.R
@ -50,7 +52,10 @@ class TimerService : Service() {
firstTimer.label.isNotEmpty() -> getString(R.string.timer_single_notification_label_msg, firstTimer.label)
else -> resources.getQuantityString(R.plurals.timer_notification_msg, runningTimers.size, runningTimers.size)
}
startForeground(TIMER_RUNNING_NOTIF_ID, notification(formattedDuration, contextText, firstTimer.id!!))
Handler(Looper.getMainLooper()).post {
startForeground(TIMER_RUNNING_NOTIF_ID, notification(formattedDuration, contextText, firstTimer.id!!))
}
} else {
stopService()
}
@ -109,7 +114,9 @@ class TimerService : Service() {
}
fun startTimerService(context: Context) {
ContextCompat.startForegroundService(context, Intent(context, TimerService::class.java))
Handler(Looper.getMainLooper()).post {
ContextCompat.startForegroundService(context, Intent(context, TimerService::class.java))
}
}
object TimerStopService