@@ -7,6 +7,7 @@ import android.app.PendingIntent
77import android.app.Service
88import android.content.Context
99import android.content.Intent
10+ import android.content.pm.ServiceInfo
1011import android.net.ConnectivityManager
1112import android.net.Network
1213import android.net.NetworkCapabilities
@@ -58,6 +59,7 @@ class AirSyncService : Service() {
5859
5960 override fun onCreate () {
6061 super .onCreate()
62+ serviceInstance = this
6163 Log .d(TAG , " AirSyncService created" )
6264 createNotificationChannel()
6365 MacDeviceStatusManager .startMonitoring(this )
@@ -103,7 +105,15 @@ class AirSyncService : Service() {
103105 Log .d(TAG , " Starting AirSync scanning mode" )
104106 isScanning = true
105107 connectedDeviceName = null
106- startForeground(NOTIFICATION_ID , buildNotification())
108+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
109+ startForeground(
110+ NOTIFICATION_ID ,
111+ buildNotification(),
112+ ServiceInfo .FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
113+ )
114+ } else {
115+ startForeground(NOTIFICATION_ID , buildNotification())
116+ }
107117
108118 val dataStoreManager =
109119 DataStoreManager .getInstance(applicationContext)
@@ -161,15 +171,31 @@ class AirSyncService : Service() {
161171 if (isScanning) {
162172 Log .d(TAG , " App in foreground, switching to ACTIVE discovery" )
163173 UDPDiscoveryManager .setDiscoveryMode(this , DiscoveryMode .ACTIVE )
164- startForeground(NOTIFICATION_ID , buildNotification()) // Update notification if needed
174+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
175+ startForeground(
176+ NOTIFICATION_ID ,
177+ buildNotification(),
178+ ServiceInfo .FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
179+ )
180+ } else {
181+ startForeground(NOTIFICATION_ID , buildNotification())
182+ }
165183 }
166184 }
167185
168186 private fun handleAppBackground () {
169187 if (isScanning) {
170188 Log .d(TAG , " App in background, switching to PASSIVE discovery" )
171189 UDPDiscoveryManager .setDiscoveryMode(this , DiscoveryMode .PASSIVE )
172- startForeground(NOTIFICATION_ID , buildNotification()) // Update notification if needed
190+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
191+ startForeground(
192+ NOTIFICATION_ID ,
193+ buildNotification(),
194+ ServiceInfo .FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
195+ )
196+ } else {
197+ startForeground(NOTIFICATION_ID , buildNotification())
198+ }
173199 }
174200 }
175201
@@ -180,7 +206,15 @@ class AirSyncService : Service() {
180206 }
181207 Log .d(TAG , " Starting AirSync foreground service (connected)" )
182208 isScanning = false
183- startForeground(NOTIFICATION_ID , buildNotification())
209+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
210+ startForeground(
211+ NOTIFICATION_ID ,
212+ buildNotification(),
213+ ServiceInfo .FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
214+ )
215+ } else {
216+ startForeground(NOTIFICATION_ID , buildNotification())
217+ }
184218
185219 val dataStoreManager =
186220 DataStoreManager .getInstance(applicationContext)
@@ -333,6 +367,7 @@ class AirSyncService : Service() {
333367
334368 override fun onDestroy () {
335369 Log .d(TAG , " AirSyncService destroyed" )
370+ serviceInstance = null
336371 WebSocketUtil .unregisterConnectionStatusListener(connectionStatusListener)
337372
338373 networkCallback?.let {
@@ -365,6 +400,10 @@ class AirSyncService : Service() {
365400
366401 const val EXTRA_DEVICE_NAME = " device_name"
367402
403+ private var serviceInstance: AirSyncService ? = null
404+
405+ fun isRunning (): Boolean = serviceInstance != null
406+
368407 fun startScanning (context : Context ) {
369408 val intent = Intent (context, AirSyncService ::class .java).apply {
370409 action = ACTION_START_SCANNING
@@ -396,10 +435,14 @@ class AirSyncService : Service() {
396435
397436 private fun startAction (context : Context , intent : Intent ) {
398437 try {
399- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
400- context.startForegroundService(intent)
401- } else {
438+ if (isRunning()) {
402439 context.startService(intent)
440+ } else {
441+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
442+ context.startForegroundService(intent)
443+ } else {
444+ context.startService(intent)
445+ }
403446 }
404447 } catch (e: Exception ) {
405448 Log .e(TAG , " Error starting foreground service" , e)
0 commit comments