我试过但是当api水平高于20时我没有得到回应。 (响应变为空)。
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
String activityOnTop;
if (Build.VERSION.SDK_INT > 20) {
activityOnTop = mActivityManager.getRunningAppProcesses().get(1).processName;
} else {
List<ActivityManager.RunningTaskInfo> RunningTask1 = mActivityManager
.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar1 = RunningTask1.get(0);
activityOnTop = ar1.topActivity.getPackageName();
}
Log.e("activityOnTop", "!!!!!!!!!!" + activityOnTop);