[code]判断程序是否在前台运行

public boolean isTopActivity(Context context, String packageName) {

 

  System.out.println("packageName:" + packageName);

  ActivityManager activityManager = (ActivityManager) context

        .getSystemService(Context.ACTIVITY_SERVICE);

 

  List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);

  if (tasksInfo.size() > 0) {

    System.out.println("curr packageName:"

                    + tasksInfo.get(0).topActivity.getPackageName());

    // 判断应用程序是否位于堆栈的顶层

    if (packageName.equals(tasksInfo.get(0).topActivity.getPackageName())) {

      return true;

    }

  }

  return false;
}

你可能感兴趣的:(code)