最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

判斷Android程序是否在前臺(tái)運(yùn)行的兩種方法

 更新時(shí)間:2015年06月11日 11:06:39   投稿:junjie  
這篇文章主要介紹了判斷Android程序是否在前臺(tái)運(yùn)行的兩種方法,本文直接給出實(shí)現(xià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)文章

最新評(píng)論

萨迦县| 福建省| 洛浦县| 楚雄市| 永胜县| 沛县| 秦安县| 灵寿县| 那坡县| 东港市| 溧水县| 资阳市| 林州市| 临潭县| 连山| 宁武县| 新民市| 镇宁| 开江县| 百色市| 日喀则市| 兴安盟| 台南县| 巴楚县| 新平| 武隆县| 东平县| 汪清县| 麦盖提县| 临湘市| 永定县| 乌鲁木齐市| 措美县| 龙南县| 彭水| 贺兰县| 乐安县| 博罗县| 长治县| 苍梧县| 蓬莱市|