protected static boolean isTopActivity(Activity activity){ String packageName = "xxxxx"; ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1); if(tasksInfo.size() > 0){ System.out.println("---------------包名-----------"+tasksInfo.get(0).topActivity.getPackageName()); //应用程序位于堆栈的顶层 if(packageName.equals(tasksInfo.get(0).topActivity.getPackageName())){ return true; } } return false; }