获取Lollipop中运行任务的baseActivity

时间:2016-08-26 02:33:32

标签: android

请帮我更改Android L的代码。在API 21中不推荐使用函数getRunningTask

ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
  List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);     

  for (RunningTaskInfo task : tasks) {
    if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
      return true;
    }

1 个答案:

答案 0 :(得分:0)

使用此代码:

  ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.AppTask> alltasks= activityManager.getAppTasks();

for (ActivityManager.AppTask task : alltasks) {
    Log.d(TAG, "stackId: " + task.getTaskInfo().stackId);
}