From e8816ee6455a7a6f3faeba776a747acdcb870389 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Sat, 9 Mar 2024 09:56:04 +0100 Subject: [PATCH] Remove forground service --- .../android/extsample/internal/Connection.kt | 11 +----- serviceLibrary/src/main/AndroidManifest.xml | 1 - .../mqtt/android/service/MqttAndroidClient.kt | 37 +++---------------- .../info/mqtt/android/service/MqttService.kt | 14 +------ 4 files changed, 7 insertions(+), 56 deletions(-) diff --git a/extendedSample/src/main/java/info/mqtt/android/extsample/internal/Connection.kt b/extendedSample/src/main/java/info/mqtt/android/extsample/internal/Connection.kt index 762f2231..454d0dd1 100644 --- a/extendedSample/src/main/java/info/mqtt/android/extsample/internal/Connection.kt +++ b/extendedSample/src/main/java/info/mqtt/android/extsample/internal/Connection.kt @@ -203,8 +203,6 @@ class Connection private constructor( } companion object { - private const val FOREGROUND = false - fun createConnection( clientHandle: String, clientId: String, @@ -219,14 +217,7 @@ class Connection private constructor( } else { "tcp://$host:$port" } - val intent = Intent(context, MainActivity::class.java).apply { - flags = Intent.FLAG_ACTIVITY_SINGLE_TOP - } - val foregroundNotification = Notify.foregroundNotification(context, clientId, intent, R.string.notifyForeground) - val client = MqttAndroidClient(context, uri, clientId).apply { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && FOREGROUND) - setForegroundService(foregroundNotification) - } + val client = MqttAndroidClient(context, uri, clientId) return Connection(clientHandle, clientId, host, port, context, client, tlsConnection, connectionOptions) } } diff --git a/serviceLibrary/src/main/AndroidManifest.xml b/serviceLibrary/src/main/AndroidManifest.xml index 52f55053..60790482 100755 --- a/serviceLibrary/src/main/AndroidManifest.xml +++ b/serviceLibrary/src/main/AndroidManifest.xml @@ -3,7 +3,6 @@ - diff --git a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt index e27fd083..2b38e628 100755 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttAndroidClient.kt @@ -1,8 +1,6 @@ package info.mqtt.android.service -import android.app.Notification import android.content.* -import android.os.Build import android.os.Bundle import android.os.IBinder import android.util.SparseArray @@ -77,9 +75,6 @@ class MqttAndroidClient @JvmOverloads constructor( @Volatile private var serviceBound = false - // notification for Foreground Service - private var foregroundServiceNotification: Notification? = null - private var clientJob: Job? = null private var clientScope: CoroutineScope? = null /** @@ -206,17 +201,11 @@ class MqttAndroidClient @JvmOverloads constructor( val serviceStartIntent = Intent() serviceStartIntent.setClassName(context, SERVICE_NAME) var service: Any? = null - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && foregroundServiceNotification != null) { - serviceStartIntent.putExtra(MqttService.MQTT_FOREGROUND_SERVICE_NOTIFICATION, foregroundServiceNotification) - serviceStartIntent.putExtra(MqttService.MQTT_FOREGROUND_SERVICE_NOTIFICATION_ID, FOREGROUND_ID) - service = context.startForegroundService(serviceStartIntent) - } else { - try { - service = context.startService(serviceStartIntent) - } catch (ex: IllegalStateException) { - val listener = token.actionCallback - listener?.onFailure(token, ex) - } + try { + service = context.startService(serviceStartIntent) + } catch (ex: IllegalStateException) { + val listener = token.actionCallback + listener?.onFailure(token, ex) } if (service == null) { val listener = token.actionCallback @@ -1150,21 +1139,6 @@ class MqttAndroidClient @JvmOverloads constructor( return tokenMap[activityToken!!.toInt()] } - /** - * Sets foregroundServiceNotification object. If it is not null at the time of - * MqttService start then the service will run in foreground mode which is - * mandatory to keep MQTT service operation when app is - * in the background on Android version >=8. - * - * - * This method has no effect if Build.VERSION.SDK_INT < Build.VERSION_CODES.O - * - * @param notification notification to be used when MqttService runs in foreground mode - */ - fun setForegroundService(notification: Notification) { - foregroundServiceNotification = notification - } - /** * Sets the DisconnectedBufferOptions for this client * @@ -1295,7 +1269,6 @@ class MqttAndroidClient @JvmOverloads constructor( companion object { private val SERVICE_NAME = MqttService::class.java.name - private const val FOREGROUND_ID = 77 } } diff --git a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt index fac27ad7..d2d9bb01 100644 --- a/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt +++ b/serviceLibrary/src/main/java/info/mqtt/android/service/MqttService.kt @@ -1,7 +1,6 @@ package info.mqtt.android.service import android.annotation.SuppressLint -import android.app.Notification import android.app.Service import android.content.BroadcastReceiver import android.content.Context @@ -13,7 +12,6 @@ import android.os.Build import android.os.Bundle import android.os.IBinder import android.os.PowerManager -import info.mqtt.android.service.extension.parcelableExtra import info.mqtt.android.service.room.MqMessageDatabase import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -237,16 +235,6 @@ class MqttService : Service(), MqttTraceHandler { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { // run till explicitly stopped, restart when process restarted registerBroadcastReceivers() - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - val foregroundServiceNotification = intent?.parcelableExtra(MQTT_FOREGROUND_SERVICE_NOTIFICATION) - if (foregroundServiceNotification != null) { - isForegroundStarted = true - startForeground( - intent.getIntExtra(MQTT_FOREGROUND_SERVICE_NOTIFICATION_ID, 1), - foregroundServiceNotification - ) - } - } return START_STICKY } @@ -331,7 +319,7 @@ class MqttService : Service(), MqttTraceHandler { if (isForegroundStarted) { @Suppress("DEPRECATION") if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) - stopForeground(Service.STOP_FOREGROUND_REMOVE) + stopForeground(STOP_FOREGROUND_REMOVE) else stopForeground(true) }