Kotlin--›Android 一行代码监听程序APP切换至后台(判断程序是否在后台运行)

其实主要就是通过ApplicationActivityLifecycleCallbacks回调, 判断当前所有Activity的生命周期.

整个核心代码如下:

object RBackground {

    const val CREATE = "onActivityCreated"
    const val STARTED = "onActivityStarted"
    const val RESUMED = "onActivityResumed"
    const val PAUSED = "onActivityPaused"
    const val STOPPED = "onActivityStopped"
    const val SAVE = "onActivitySaveInstanceState"

    /** class_name|state */
    val stack = SparseArray<String>()

    private val observers = mutableListOf<OnBackgroundObserver>()

    /**主线程回调*/
    val lifecycleCallbacks = object : Application.ActivityLifecycleCallbacks {

        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            pushItem(activity.hashCode(), activity.javaClass.name, CREATE)
        }

        override fun onActivityStarted(activity: Activity) {
            changeItem(activity.hashCode(), activity.javaClass.name, STARTED)
        }

        override fun onActivityResumed(activity: Activity) {
            changeItem(activity.hashCode(), activity.javaClass.name, RESUMED)
        }

        override fun onActivityPaused(activity: Activity) {
            if (activity.isFinishing) {
            } else {
                changeItem(activity.hashCode(), activity.javaClass.name, PAUSED)
            }
        }

        override fun onActivityStopped(activity: Activity) {
            if (activity.isFinishing) {
            } else {
                changeItem(activity.hashCode(), activity.javaClass.name, STOPPED)
            }
        }

        override fun onActivityDestroyed(activity: Activity) {
            removeItem(activity.hashCode(), activity.javaClass.name)
        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) {
            changeItem(activity.hashCode(), activity.javaClass.name, SAVE)
        }
    }

    private fun pushItem(code: Int, className: String, state: String) {
        val item = "$className|$state"

        stack.put(code, item)

        observers.forEach {
            it.onActivityChanged(stack, isBackground())
        }
    }

    private fun changeItem(code: Int, className: String, state: String) {
        pushItem(code, className, state)
    }

    private fun removeItem(code: Int, className: String) {
        stack.remove(code)

        observers.forEach {
            it.onActivityChanged(stack, isBackground())
        }
    }

    fun init(application: Application, observer: OnBackgroundObserver? = null) {
        application.registerActivityLifecycleCallbacks(lifecycleCallbacks)

        observer?.let {
            registerObserver(it)
        }
    }

    fun isBackground(): Boolean {
        var result = stack.size() != 0

        for (i in 0 until stack.size()) {
            val value = stack.get(stack.keyAt(i))
            if (value.endsWith(STOPPED)) {

            } else {
                result = false
                break
            }
        }

        return result
    }

    fun registerObserver(observer: OnBackgroundObserver) {
        if (!observers.contains(observer)) {
            observers.add(observer)
        }
    }

    fun unregisterObserver(observer: OnBackgroundObserver) {
        observers.remove(observer)
    }
}

interface OnBackgroundObserver {
    fun onActivityChanged(stack: SparseArray<String>, background: Boolean)
}

使用方法:

RBackground.init(this, object : OnBackgroundObserver {
            override fun onActivityChanged(stack: SparseArray<String>, background: Boolean) {
                L.d("$stack")
                if (background) {
                    //切换到后台
                        Tip.tip("您已切换至后台")
                }
            }
        })

群内有各(pian)种(ni)各(jin)样(qun)的大佬,等你来撩.

联系作者

点此快速加群

请使用QQ扫码加群, 小伙伴们都在等着你哦!

Kotlin--›Android 一行代码监听程序APP切换至后台(判断程序是否在后台运行)_第1张图片

关注我的公众号, 每天都能一起玩耍哦!

Kotlin--›Android 一行代码监听程序APP切换至后台(判断程序是否在后台运行)_第2张图片

你可能感兴趣的:(Kotlin)