pull/1114/head
gedoor 3 years ago
parent 3b30b6b496
commit f176f97641
  1. 3
      app/src/main/java/io/legado/app/base/BaseService.kt
  2. 19
      app/src/main/java/io/legado/app/help/LifecycleHelp.kt

@ -4,6 +4,7 @@ import android.app.Service
import android.content.Intent
import android.os.IBinder
import androidx.annotation.CallSuper
import io.legado.app.help.LifecycleHelp
import io.legado.app.help.coroutine.Coroutine
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -22,6 +23,7 @@ abstract class BaseService : Service(), CoroutineScope by MainScope() {
@CallSuper
override fun onCreate() {
super.onCreate()
LifecycleHelp.onServiceCreate(this)
}
@CallSuper
@ -38,5 +40,6 @@ abstract class BaseService : Service(), CoroutineScope by MainScope() {
override fun onDestroy() {
super.onDestroy()
cancel()
LifecycleHelp.onServiceDestroy(this)
}
}

@ -3,6 +3,7 @@ package io.legado.app.help
import android.app.Activity
import android.app.Application
import android.os.Bundle
import io.legado.app.base.BaseService
import io.legado.app.utils.LanguageUtils
import java.lang.ref.WeakReference
import java.util.*
@ -14,6 +15,7 @@ import java.util.*
object LifecycleHelp : Application.ActivityLifecycleCallbacks {
private val activities: MutableList<WeakReference<Activity>> = arrayListOf()
private val services: MutableList<WeakReference<BaseService>> = arrayListOf()
fun activitySize(): Int {
return activities.size
@ -61,7 +63,7 @@ object LifecycleHelp : Application.ActivityLifecycleCallbacks {
override fun onActivityDestroyed(activity: Activity) {
for (temp in activities) {
if (temp.get() === activity) {
if (temp.get() != null && temp.get() === activity) {
activities.remove(temp)
break
}
@ -80,4 +82,19 @@ object LifecycleHelp : Application.ActivityLifecycleCallbacks {
LanguageUtils.setConfiguration(activity)
}
}
@Synchronized
fun onServiceCreate(service: BaseService) {
services.add(WeakReference(service))
}
@Synchronized
fun onServiceDestroy(service: BaseService) {
for (temp in services) {
if (temp.get() != null && temp.get() === service) {
services.remove(temp)
break
}
}
}
}
Loading…
Cancel
Save