判斷Android程序是否在前臺(tái)運(yùn)行的兩種方法
@Override protected void onStop() { if (!isAppOnForeground()) { Debug.i("dwy", "enter background"); mIsBackground = true; } else { Debug.i("dwy", "foreground"); mIsBackground = false; }
Judge is App in background when onStop() get called.
public boolean isAppOnForeground() { // Returns a list of application processes that are running on the // device ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); String packageName = getApplicationContext().getPackageName(); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager .getRunningAppProcesses(); if (appProcesses == null) return false; for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { // The name of the process that this object is associated with. if (appProcess.processName.equals(packageName) && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } return false; }
方法二:
/** * 需要權(quán)限:android.permission.GET_TASKS * * @param context * @return */ public boolean isApplicationBroughtToBackground(Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); if (tasks != null && !tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; Debug.i(TAG, "topActivity:" + topActivity.flattenToString()); Debug.f(TAG, "topActivity:" + topActivity.flattenToString()); if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; }
相關(guān)文章
一文詳解無(wú)痕埋點(diǎn)在Android中的實(shí)現(xiàn)
很多時(shí)候因?yàn)楫a(chǎn)品都會(huì)要獲取用戶(hù)的行為,需要客戶(hù)端進(jìn)行相關(guān)的埋點(diǎn),下面這篇文章主要給大家介紹了關(guān)于無(wú)痕埋點(diǎn)在Android中實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02Android 媒體庫(kù)數(shù)據(jù)更新方法總結(jié)
這篇文章主要介紹了Android 媒體庫(kù)數(shù)據(jù)更新方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04Android 實(shí)現(xiàn)手機(jī)接通電話(huà)后振動(dòng)提示的功能
本文主要介紹Android 實(shí)現(xiàn)手機(jī)接通電話(huà)后振動(dòng)提示的功能,這里整理了詳細(xì)的相關(guān)資料,并附有示例代碼,有需要的朋友可以參考下2016-08-08Android瀑布流照片墻實(shí)現(xiàn) 體驗(yàn)不規(guī)則排列的美感
這篇文章主要為大家詳細(xì)介紹了Android瀑布流照片墻實(shí)現(xiàn),體驗(yàn)不規(guī)則排列的美感,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android自定義控件LinearLayout實(shí)例講解
這篇文章主要為大家詳細(xì)介紹了Android自定義控件LinearLayout實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Android webview與js交換JSON對(duì)象數(shù)據(jù)示例
js主動(dòng)調(diào)用android的對(duì)象方式,android也無(wú)法返回給js一個(gè)jsonobject,需要js做一下轉(zhuǎn)換,具體代碼如下,感興趣的朋友可以參考下哈2013-06-06Android側(cè)滑效果簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android側(cè)滑效果簡(jiǎn)單實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Intent傳遞對(duì)象之Serializable和Parcelable的區(qū)別
Intent在不同的組件中傳遞對(duì)象數(shù)據(jù)的應(yīng)用非常普遍,大家都知道在intent傳遞對(duì)象的方法有兩種:1、實(shí)現(xiàn)Serializable接口、2、實(shí)現(xiàn)Parcelable接口,接下來(lái)通過(guò)本文給大家介紹Intent傳遞對(duì)象之Serializable和Parcelable的區(qū)別,感興趣的朋友一起學(xué)習(xí)吧2016-01-01Android如何判斷手機(jī)是否有錄音權(quán)限的工具類(lèi)
這篇文章主要為大家詳細(xì)介紹了Android判斷手機(jī)是否有錄音的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06