Files
android-sms-gateway/app/src/main/java/com/androidsmsgateway/server/BootReceiver.kt
T
2026-08-25 02:18:50 +03:30

34 lines
1.3 KiB
Kotlin

package com.androidsmsgateway.server
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.core.content.ContextCompat
import com.androidsmsgateway.GatewayApplication
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action != Intent.ACTION_BOOT_COMPLETED) return
val pendingResult = goAsync()
val settingsRepository = (context.applicationContext as GatewayApplication).container.settingsRepository
CoroutineScope(Dispatchers.IO).launch {
try {
runCatching {
val settings = settingsRepository.current()
if (settings.startOnBoot && settings.serverEnabled) {
ContextCompat.startForegroundService(
context,
Intent(context, GatewayServerService::class.java).setAction(GatewayServerService.ACTION_START),
)
}
}
} finally {
pendingResult.finish()
}
}
}
}