需要加一个权限:
<uses-permission android:name="android.permission.GET_TASKS"/>
Java代码
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTasks = manager .getRunningTasks(1);
RunningTaskInfo cinfo = runningTasks.get(0);
ComponentName component = cinfo.topActivity;
Log.e("current activity is ", component.getClassName());
原文:https://blog.csdn.net/dyllove98/article/details/9631663