/*** * 判断当前程序是不在前台显示 * * @return */ public static boolean isTopRun(Context context) { Boolean isTopRun = false; try { ActivityManager activityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager .getRunningTasks(1); if (tasksInfo.size() > 0) { String packageName = tasksInfo.get(0).topActivity .getPackageName(); if (!TextUtils.isEmpty(packageName) && packageName.startsWith(getAppPackageName())) { isTopRun = true; } } } catch (Exception e) { e.printStackTrace(); } return isTopRun; }
/*** * 判断应用是否在运行 * * @return */ public static boolean isAppRunning(Context context){ boolean isAppRunning = false; try { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> list = activityManager.getRunningTasks(100); String pageName = "com.example.packagename"; pageName = getAppPackageName(); for (RunningTaskInfo info : list) { if (info.topActivity.getPackageName().equals(pageName) || info.baseActivity.getPackageName().equals(pageName)) { isAppRunning = true; Log.i("SystemUtils",info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName()); break; } } } catch (Exception e) { e.printStackTrace(); } return isAppRunning; }
/*** * 获取当前显示的Activity * * @return */ public static void getTopActivity(Context context){ ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ComponentName cn = am.getRunningTasks(1).get(0).topActivity; }