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

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

@ -3,6 +3,7 @@ package io.legado.app.help
import android.app.Activity import android.app.Activity
import android.app.Application import android.app.Application
import android.os.Bundle import android.os.Bundle
import io.legado.app.base.BaseService
import io.legado.app.utils.LanguageUtils import io.legado.app.utils.LanguageUtils
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.* import java.util.*
@ -14,6 +15,7 @@ import java.util.*
object LifecycleHelp : Application.ActivityLifecycleCallbacks { object LifecycleHelp : Application.ActivityLifecycleCallbacks {
private val activities: MutableList<WeakReference<Activity>> = arrayListOf() private val activities: MutableList<WeakReference<Activity>> = arrayListOf()
private val services: MutableList<WeakReference<BaseService>> = arrayListOf()
fun activitySize(): Int { fun activitySize(): Int {
return activities.size return activities.size
@ -61,7 +63,7 @@ object LifecycleHelp : Application.ActivityLifecycleCallbacks {
override fun onActivityDestroyed(activity: Activity) { override fun onActivityDestroyed(activity: Activity) {
for (temp in activities) { for (temp in activities) {
if (temp.get() === activity) { if (temp.get() != null && temp.get() === activity) {
activities.remove(temp) activities.remove(temp)
break break
} }
@ -76,8 +78,23 @@ object LifecycleHelp : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
activities.add(WeakReference(activity)) activities.add(WeakReference(activity))
if (!LanguageUtils.isSameWithSetting(activity)){ if (!LanguageUtils.isSameWithSetting(activity)) {
LanguageUtils.setConfiguration(activity) 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