diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b8fd816ac..0abecd057 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -350,11 +350,6 @@ android:enabled="true" android:exported="false" /> - - = OnlinePlayerService::class.java) { + if (isBackgroundServiceRunning(context, serviceClass)) { // Intent to stop background mode service - val intent = Intent(context, OnlinePlayerService::class.java) + val intent = Intent(context, serviceClass) context.stopService(intent) } } @@ -60,9 +60,9 @@ object BackgroundHelper { /** * Check if the [OnlinePlayerService] service is currently running. */ - fun isBackgroundServiceRunning(context: Context): Boolean { + fun isBackgroundServiceRunning(context: Context, serviceClass: Class<*> = OnlinePlayerService::class.java): Boolean { @Suppress("DEPRECATION") return context.getSystemService()!!.getRunningServices(Int.MAX_VALUE) - .any { OnlinePlayerService::class.java.name == it.service.className } + .any { serviceClass.name == it.service.className } } } diff --git a/app/src/main/java/com/github/libretube/services/ClosingService.kt b/app/src/main/java/com/github/libretube/services/ClosingService.kt deleted file mode 100644 index 7cf45f11b..000000000 --- a/app/src/main/java/com/github/libretube/services/ClosingService.kt +++ /dev/null @@ -1,23 +0,0 @@ -package com.github.libretube.services - -import android.app.NotificationManager -import android.app.Service -import android.content.Intent -import androidx.core.content.getSystemService -import com.github.libretube.constants.PLAYER_NOTIFICATION_ID - -class ClosingService : Service() { - - override fun onBind(intent: Intent?) = null - - // Handle application closing - override fun onTaskRemoved(rootIntent: Intent?) { - super.onTaskRemoved(rootIntent) - - // destroy the player notification when the app gets destroyed - getSystemService()!!.cancel(PLAYER_NOTIFICATION_ID) - - // Destroy the service - stopSelf() - } -} diff --git a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt index f8d797f3f..84ee5d795 100644 --- a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt @@ -118,4 +118,12 @@ class OfflinePlayerService : LifecycleService() { super.onBind(intent) return null } + + /** + * Stop the service when app is removed from the task manager. + */ + override fun onTaskRemoved(rootIntent: Intent?) { + super.onTaskRemoved(rootIntent) + onDestroy() + } } diff --git a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt index 87bf103e6..4c01ae85e 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt @@ -33,7 +33,6 @@ import com.github.libretube.helpers.NetworkHelper import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.ThemeHelper import com.github.libretube.helpers.WindowHelper -import com.github.libretube.services.ClosingService import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.dialogs.ErrorDialog import com.github.libretube.ui.fragments.AudioPlayerFragment @@ -65,13 +64,6 @@ class MainActivity : BaseActivity() { // enable auto rotation if turned on requestOrientationChange() - // start service that gets called on closure - try { - startService(Intent(this, ClosingService::class.java)) - } catch (e: Exception) { - e.printStackTrace() - } - // show noInternet Activity if no internet available on app startup if (!NetworkHelper.isNetworkAvailable(this)) { val noInternetIntent = Intent(this, NoInternetActivity::class.java)