国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2020-04-11 11:29:55
字體:
來源:轉載
供稿:網友
@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;   } 


方法二:

/**   * 需要權限: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;   } 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴里| 保山市| 驻马店市| 博野县| 焉耆| 广安市| 泽库县| 志丹县| 泾阳县| 夏河县| 灵宝市| 潞城市| 水富县| 启东市| 玛沁县| 西盟| 额尔古纳市| 蒙城县| 思南县| 海安县| 砀山县| 分宜县| 钟山县| 汶川县| 揭东县| 沙河市| 黔东| 建水县| 泰宁县| 区。| 潜江市| 景德镇市| 南靖县| 华阴市| 桐城市| 德钦县| 织金县| 英山县| 德保县| 汶上县| 石棉县|