feat: 添加前后台状态变化监听
This commit is contained in:
@ -5,6 +5,7 @@ import android.app.Application
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.quyunshuo.androidbaseframemvvm.base.utils.ActivityStackManager
|
import com.quyunshuo.androidbaseframemvvm.base.utils.ActivityStackManager
|
||||||
|
import com.quyunshuo.androidbaseframemvvm.base.utils.ForegroundBackgroundHelper
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Activity生命周期监听
|
* Activity生命周期监听
|
||||||
@ -23,6 +24,7 @@ class ActivityLifecycleCallbacksImpl : Application.ActivityLifecycleCallbacks {
|
|||||||
|
|
||||||
override fun onActivityStarted(activity: Activity) {
|
override fun onActivityStarted(activity: Activity) {
|
||||||
Log.e(TAG, "${activity.javaClass.simpleName} --> onActivityStarted")
|
Log.e(TAG, "${activity.javaClass.simpleName} --> onActivityStarted")
|
||||||
|
ForegroundBackgroundHelper.onActivityStarted()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onActivityResumed(activity: Activity) {
|
override fun onActivityResumed(activity: Activity) {
|
||||||
@ -35,6 +37,7 @@ class ActivityLifecycleCallbacksImpl : Application.ActivityLifecycleCallbacks {
|
|||||||
|
|
||||||
override fun onActivityStopped(activity: Activity) {
|
override fun onActivityStopped(activity: Activity) {
|
||||||
Log.e(TAG, "${activity.javaClass.simpleName} --> onActivityStopped")
|
Log.e(TAG, "${activity.javaClass.simpleName} --> onActivityStopped")
|
||||||
|
ForegroundBackgroundHelper.onActivityStopped()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
|
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
|
||||||
|
|||||||
@ -0,0 +1,84 @@
|
|||||||
|
package com.quyunshuo.androidbaseframemvvm.base.utils
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前后台切换帮助类,该类实现了前后台监听以及支持注册变化响应监听
|
||||||
|
*
|
||||||
|
* @see ForegroundBackgroundObserver
|
||||||
|
* @see ForegroundBackgroundSubject
|
||||||
|
*
|
||||||
|
* @author Qu Yunshuo
|
||||||
|
* @since 2023/5/31 14:22
|
||||||
|
*/
|
||||||
|
object ForegroundBackgroundHelper : ForegroundBackgroundSubject {
|
||||||
|
|
||||||
|
private var mActivityStartCount = 0
|
||||||
|
|
||||||
|
private var mIsForeground = false
|
||||||
|
|
||||||
|
private val mObservers = mutableListOf<ForegroundBackgroundObserver>()
|
||||||
|
|
||||||
|
fun onActivityStarted() {
|
||||||
|
mActivityStartCount++
|
||||||
|
if (mActivityStartCount == 1) {
|
||||||
|
mIsForeground = true
|
||||||
|
notifyObservers()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onActivityStopped() {
|
||||||
|
mActivityStartCount--
|
||||||
|
if (mActivityStartCount == 0) {
|
||||||
|
mIsForeground = false
|
||||||
|
notifyObservers()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通知所有订阅者状态变化
|
||||||
|
*/
|
||||||
|
override fun notifyObservers() {
|
||||||
|
mObservers.forEach {
|
||||||
|
it.foregroundBackgroundNotify(mIsForeground)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加订阅者
|
||||||
|
*
|
||||||
|
* @param observer ForegroundBackgroundObserver
|
||||||
|
*/
|
||||||
|
override fun addObserve(observer: ForegroundBackgroundObserver) {
|
||||||
|
mObservers.add(observer)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除订阅者
|
||||||
|
*
|
||||||
|
* @param observer ForegroundBackgroundObserver
|
||||||
|
*/
|
||||||
|
override fun removeObserver(observer: ForegroundBackgroundObserver) {
|
||||||
|
mObservers.remove(observer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订阅者需要实现的接口
|
||||||
|
*
|
||||||
|
* @author Qu Yunshuo
|
||||||
|
* @since 2023/5/31 14:23
|
||||||
|
*/
|
||||||
|
interface ForegroundBackgroundObserver {
|
||||||
|
fun foregroundBackgroundNotify(isForeground: Boolean)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 被观察者抽象主题
|
||||||
|
*
|
||||||
|
* @author Qu Yunshuo
|
||||||
|
* @since 2023/5/31 14:24
|
||||||
|
*/
|
||||||
|
interface ForegroundBackgroundSubject {
|
||||||
|
fun notifyObservers()
|
||||||
|
fun addObserve(observer: ForegroundBackgroundObserver)
|
||||||
|
fun removeObserver(observer: ForegroundBackgroundObserver)
|
||||||
|
}
|
||||||
@ -9,6 +9,7 @@ import com.google.auto.service.AutoService
|
|||||||
import com.quyunshuo.androidbaseframemvvm.base.app.ApplicationLifecycle
|
import com.quyunshuo.androidbaseframemvvm.base.app.ApplicationLifecycle
|
||||||
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
|
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
|
||||||
import com.quyunshuo.androidbaseframemvvm.base.constant.VersionStatus
|
import com.quyunshuo.androidbaseframemvvm.base.constant.VersionStatus
|
||||||
|
import com.quyunshuo.androidbaseframemvvm.base.utils.ForegroundBackgroundObserver
|
||||||
import com.quyunshuo.androidbaseframemvvm.base.utils.ProcessUtils
|
import com.quyunshuo.androidbaseframemvvm.base.utils.ProcessUtils
|
||||||
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
|
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
|
||||||
import com.quyunshuo.androidbaseframemvvm.base.utils.network.NetworkStateClient
|
import com.quyunshuo.androidbaseframemvvm.base.utils.network.NetworkStateClient
|
||||||
@ -24,7 +25,7 @@ import com.tencent.smtt.sdk.QbSdk.PreInitCallback
|
|||||||
* @since 4/16/21 3:37 PM
|
* @since 4/16/21 3:37 PM
|
||||||
*/
|
*/
|
||||||
@AutoService(ApplicationLifecycle::class)
|
@AutoService(ApplicationLifecycle::class)
|
||||||
class CommonApplication : ApplicationLifecycle {
|
class CommonApplication : ApplicationLifecycle, ForegroundBackgroundObserver {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
// 全局CommonApplication
|
// 全局CommonApplication
|
||||||
@ -146,4 +147,8 @@ class CommonApplication : ApplicationLifecycle {
|
|||||||
)
|
)
|
||||||
return "Bugly -->> init complete"
|
return "Bugly -->> init complete"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun foregroundBackgroundNotify(isForeground: Boolean) {
|
||||||
|
Log.d("ForegroundBackground", "isForeground: $isForeground")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user