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

判斷Android程序是否在前臺運行的兩種方法

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

最新評論

垫江县| 比如县| 云和县| 大兴区| 泗阳县| 景宁| 贵南县| 社会| 七台河市| 莱阳市| 香港 | 那坡县| 远安县| 曲周县| 大姚县| 岑溪市| 廉江市| 石泉县| 加查县| 沂南县| 通海县| 谷城县| 中方县| 惠州市| 隆回县| 张家口市| 玉田县| 栾城县| 延川县| 大厂| 茶陵县| 常州市| 满洲里市| 安丘市| 乐陵市| 德惠市| 新和县| 宁陵县| 吴桥县| 辽宁省| 株洲县|