AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

如何判斷APP在前臺還是后臺?

來源: 責編: 時間:2024-01-22 08:40:56 230觀看
導讀如何判斷一個APP在前臺還是后臺?可以通過RunningTasks,RunningProcess 以及ActivityLifecycleCallback判定。RunningTasks方式注意:getRunningTask方法在5.0以上已經被廢棄,只能返回自己和系統的一些不敏感的task,不再返回

如何判斷一個APP在前臺還是后臺?可以通過RunningTasks,RunningProcess 以及ActivityLifecycleCallback判定。wq728資訊網——每日最新資訊28at.com

RunningTasks方式

注意:getRunningTask方法在5.0以上已經被廢棄,只能返回自己和系統的一些不敏感的task,不再返回其他應用的task,用此方法來判斷自身App是否處于后臺是有效的,但是無法判斷其他應用是否處于前臺。wq728資訊網——每日最新資訊28at.com

private fun getTopApplication() {    //首先獲取到ActivityManager    val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager    if (activityManager.getRunningTasks(1) == null){        Log.e(TAG, "getForegroundActivity: ")        return    }    var runningTaskInfo = activityManager.getRunningTasks(1)[0]    if (runningTaskInfo == null) {        Log.e(TAG, "runningTaskInfo is null")        return    }    runningTaskInfo.topActivity?.let {        Log.e(TAG, "top application is ${it.packageName}")    }}

RunningProcess方式

注意:RunningProcess方法在5.0以上已經被廢棄,例如,在聊天類型的App中,大部分時間需要常駐后臺來不間斷地獲取服務器的消息,就必須把Service設置成START_STICKY,kill后會被重啟(等待5s左右)來保證Service常駐后臺。如果Service設置了這個屬性,這個App的進程就可以判斷為前臺。wq728資訊網——每日最新資訊28at.com

appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
private fun isAppForeground(): Boolean {    val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager    var runningAppProcesses = activityManager.runningAppProcesses    if (runningAppProcesses == null) {        Log.e(TAG, "runningAppProcesses is null")        return false    }    runningAppProcesses.forEach {        if (it.processName == packageName && (it.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)) {            return true        }    }    return false}

ActivityLifecycleCallbacks方式

class MyActivityLifecycleCallbacks(    var onActivityCreatedAction: ((Activity, Bundle?) -> Unit)? = null,    var onActivityStartedAction: ((Activity) -> Unit)? = null,    var onActivityResumedAction: ((Activity) -> Unit)? = null,    var onActivityPausedAction: ((Activity) -> Unit)? = null,    var onActivityStoppedAction: ((Activity) -> Unit)? = null,    var onActivitySaveInstanceStateAction: ((Activity, Bundle) -> Unit)? = null,    var onActivityDestroyedAction: ((Activity) -> Unit)? = null) : Application.ActivityLifecycleCallbacks {    private var mCount=0    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {        onActivityCreatedAction?.invoke(activity, savedInstanceState)    }    override fun onActivityStarted(activity: Activity) {        mCount++        onActivityStartedAction?.invoke(activity)    }    override fun onActivityResumed(activity: Activity) {        onActivityResumedAction?.invoke(activity)    }    override fun onActivityPaused(activity: Activity) {        onActivityPausedAction?.invoke(activity)    }    override fun onActivityStopped(activity: Activity) {        mCount--        onActivityStoppedAction?.invoke(activity)    }    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {        onActivitySaveInstanceStateAction?.invoke(activity, outState)    }    override fun onActivityDestroyed(activity: Activity) {        onActivityDestroyedAction?.invoke(activity)    }    /**     * 這里我們把mCount的實際數值返回回去     */    fun getCount():Int = mCount}

這里我們對ActivityLifecycleCallbacks的實現類做了一層封裝,利用Kotlin的高階函數,當我們需要去實現那個聲明周期的回調的時候,就通過高階函數來提供回調處理,否則默認不做任何處理。然后我們在Application的onCreate中進行注冊:wq728資訊網——每日最新資訊28at.com

class LifeApplication : Application() {    private val TAG = "LifeApplication"    private val mActivityLifecycleCallbacks by lazy {        MyActivityLifecycleCallbacks(            onActivityCreatedAction = { activit, bundle ->            Log.e(TAG, "onCreate: ")        },        onActivityStoppedAction = { activity ->            Log.e(TAG, "onStop ")        },        onActivityDestroyedAction = { activity ->            Log.e(TAG, "onDestroy")        })    }    override fun onCreate() {        super.onCreate()        instance = this        //注冊生命周期回調事件        registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks)    }    /**     * 用于判斷當前進程是否處于前臺     */    fun isForegroundMethod(): Boolean = mActivityLifecycleCallbacks.getCount() > 0    companion object{        private var instance :LifeApplication?= null        fun getInstance () = instance!!    }

當我們不管是點擊Back鍵還是Home鍵都會回調到onStop方法,我們在onStart和onStop中分別對mCount值做了加減,這樣我們可以通過該數值來判斷當前App是前臺還是后臺。wq728資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-65868-0.html如何判斷APP在前臺還是后臺?

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 實現定時任務的六種策略

下一篇: Python編程新手須知:避免代碼陷阱的秘訣

標簽:
  • 熱門焦點
Top