Skip to content

Instantly share code, notes, and snippets.

@EHG613
Created February 28, 2023 02:32
Show Gist options
  • Save EHG613/6a6dc4b860b926e5916ae109b9cdf9cd to your computer and use it in GitHub Desktop.
Save EHG613/6a6dc4b860b926e5916ae109b9cdf9cd to your computer and use it in GitHub Desktop.
GET_TASKS权限在5.0已废弃;可以通过如下方式获取任务栈列表
Log.e("Live",getTaskId()+"");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
ActivityManager activityManager = getSystemService(ActivityManager.class);
//获取任务栈列表
List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();
for (ActivityManager.AppTask task : tasks) {
ActivityManager.RecentTaskInfo info = task.getTaskInfo();
int activityCount = info.numActivities; //栈内Activity数量
String topActivity = info.topActivity.getClassName(); //栈顶Activity
String baseActivity = info.baseActivity.getClassName(); //栈底Activity
Log.e("Live",activityCount+";"+topActivity+";"+baseActivity+";"+info.affiliatedTaskId);
// task.setExcludeFromRecents(true); //不显示在最近任务列表中
// task.finishAndRemoveTask(); //销毁任务栈内全部Activity
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment