[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;
}

posted on 2012-08-17 17:03  亭子  阅读(249)  评论(0编辑  收藏  举报

导航