本文共 912 字,大约阅读时间需要 3 分钟。
Android 判断app是否在前台还是在后台运行,直接看代码,可直接使用。
- public static boolean isBackground(Context context) {
- ActivityManager activityManager = (ActivityManager) context
- .getSystemService(Context.ACTIVITY_SERVICE);
- List<RunningAppProcessInfo> appProcesses = activityManager
- .getRunningAppProcesses();
- for (RunningAppProcessInfo appProcess : appProcesses) {
- if (appProcess.processName.equals(context.getPackageName())) {
-
-
-
-
- Log.i(context.getPackageName(), "此appimportace ="
- + appProcess.importance
- + ",context.getClass().getName()="
- + context.getClass().getName());
- if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
- Log.i(context.getPackageName(), "处于后台"
- + appProcess.processName);
- return true;
- } else {
- Log.i(context.getPackageName(), "处于前台"
- + appProcess.processName);
- return false;
- }
- }
- }
- return false;
- }
转载地址:http://akogx.baihongyu.com/