这个方法需要app获取android.permission.REAL_GET_TASKS权限
Android 5.0以上只对系统应用开放。
Android5.0以下需要添加权限:
android.permission.GET_TASKS
private String getTopActivity() {
String className = null;
ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = mActivityManager.getRunningTasks(1);
if (!list.isEmpty() && list.get(0) != null && list.get(0).topActivity != null) {
className = list.get(0).topActivity.getClassName();
}
return className;
}