最近做推送消息处理的处理的时候,点击通知需要跳转到指定界面,这时候需要判断APP 是否运行在前台,就找到了这个方法/** * @param context * @return */ public static boolean isBackground(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(context.getPackageName())) { if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { System.out.print(String.format("Foreground App:", appProcess.processName)); return false; } else { System.out.print("Background App:" + appProcess.processName); return true; } } } return false; }
返回true的时候是运行在前台
判断APP是否运行在前台
最新推荐文章于 2022-08-22 10:30:25 发布