refactor: Improve background streaming service management
- Added action handling for stopping the background streaming service, allowing for a clean shutdown when requested. - Enhanced the stopStreaming method to ensure proper foreground service termination based on Android version compatibility. - Updated keepAlive method to conditionally start the foreground service based on the Android version, improving service management and reliability. - Implemented error handling for service start and stop operations to enhance robustness and provide clearer feedback on failures.
This commit is contained in:
@@ -44,6 +44,13 @@ class BackgroundStreamingService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
|
when (intent?.action) {
|
||||||
|
ACTION_STOP -> {
|
||||||
|
stopStreaming()
|
||||||
|
return START_NOT_STICKY
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val notification = createMinimalNotification()
|
val notification = createMinimalNotification()
|
||||||
val desiredType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
val desiredType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
resolveForegroundServiceType(intent)
|
resolveForegroundServiceType(intent)
|
||||||
@@ -67,15 +74,12 @@ class BackgroundStreamingService : Service() {
|
|||||||
acquireWakeLock()
|
acquireWakeLock()
|
||||||
println("BackgroundStreamingService: Started foreground service")
|
println("BackgroundStreamingService: Started foreground service")
|
||||||
}
|
}
|
||||||
ACTION_STOP -> {
|
|
||||||
stopStreaming()
|
|
||||||
}
|
|
||||||
"KEEP_ALIVE" -> {
|
"KEEP_ALIVE" -> {
|
||||||
keepAlive()
|
keepAlive()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return START_STICKY // Restart if killed by system
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startForegroundInternal(notification: Notification, type: Int): Boolean {
|
private fun startForegroundInternal(notification: Notification, type: Int): Boolean {
|
||||||
@@ -175,9 +179,18 @@ class BackgroundStreamingService : Service() {
|
|||||||
private fun stopStreaming() {
|
private fun stopStreaming() {
|
||||||
activeStreams.clear()
|
activeStreams.clear()
|
||||||
releaseWakeLock()
|
releaseWakeLock()
|
||||||
stopForeground(true)
|
|
||||||
|
if (isForeground) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
|
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||||
|
} else {
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
stopForeground(true)
|
||||||
|
}
|
||||||
|
isForeground = false
|
||||||
|
}
|
||||||
|
|
||||||
stopSelf()
|
stopSelf()
|
||||||
isForeground = false
|
|
||||||
println("BackgroundStreamingService: Service stopped")
|
println("BackgroundStreamingService: Service stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,9 +325,13 @@ class BackgroundStreamingHandler(private val activity: MainActivity) : MethodCal
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun stopForegroundService() {
|
private fun stopForegroundService() {
|
||||||
val serviceIntent = Intent(context, BackgroundStreamingService::class.java)
|
try {
|
||||||
serviceIntent.action = BackgroundStreamingService.ACTION_STOP
|
val serviceIntent = Intent(context, BackgroundStreamingService::class.java)
|
||||||
context.startService(serviceIntent)
|
serviceIntent.action = BackgroundStreamingService.ACTION_STOP
|
||||||
|
context.stopService(serviceIntent)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("BackgroundStreamingHandler: Failed to stop foreground service: ${e.message}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startBackgroundMonitoring() {
|
private fun startBackgroundMonitoring() {
|
||||||
@@ -354,15 +371,25 @@ class BackgroundStreamingHandler(private val activity: MainActivity) : MethodCal
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun keepAlive() {
|
private fun keepAlive() {
|
||||||
// Just notify the service to refresh
|
if (activeStreams.isEmpty()) return
|
||||||
val serviceIntent = Intent(context, BackgroundStreamingService::class.java)
|
|
||||||
serviceIntent.action = "KEEP_ALIVE"
|
try {
|
||||||
serviceIntent.putExtra("streamCount", activeStreams.size)
|
val serviceIntent = Intent(context, BackgroundStreamingService::class.java)
|
||||||
serviceIntent.putExtra(
|
serviceIntent.action = "KEEP_ALIVE"
|
||||||
BackgroundStreamingService.EXTRA_REQUIRES_MICROPHONE,
|
serviceIntent.putExtra("streamCount", activeStreams.size)
|
||||||
streamsRequiringMic.isNotEmpty(),
|
serviceIntent.putExtra(
|
||||||
)
|
BackgroundStreamingService.EXTRA_REQUIRES_MICROPHONE,
|
||||||
context.startService(serviceIntent)
|
streamsRequiringMic.isNotEmpty(),
|
||||||
|
)
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
context.startForegroundService(serviceIntent)
|
||||||
|
} else {
|
||||||
|
context.startService(serviceIntent)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("BackgroundStreamingHandler: Failed to keep alive service: ${e.message}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createNotificationChannel() {
|
private fun createNotificationChannel() {
|
||||||
|
|||||||
Reference in New Issue
Block a user