//获取到进程管理器
activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
//获取到当前正在运行的任务栈
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);//参数是想获得的个数,可以随意写
//获取到最上面的进程
RunningTaskInfo taskInfo = tasks.get(0);
//获取到最顶端应用程序的包名
String packageName = taskInfo.topActivity.getPackageName();
System.out.println(packageName);