getRunningTasks 在 Android L 中不起作用 [英] getRunningTasks doesn't work in Android L

查看:47
本文介绍了getRunningTasks 在 Android L 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android L 中,Google 已禁用 getRunningTasks.现在它只能返回自己的应用程序任务和主启动器.我无法再获得其他应用程序任务.我们的应用程序需要该方法来确定当前的热门应用程序.任何人都有另一种方法来做到这一点?

In Android L, Google has disabled getRunningTasks. Now it can only return own apps task and the home launcher. I can no longer get other apps tasks. Our app needs that method to determine current top app. Any one has another method to do this?

我在谷歌搜索过,除此之外没有更多关于此的主题:https://code.google.com/p/android-developer-preview/问题/细节?id=29

I have searched in Google, no more topics about this except this: https://code.google.com/p/android-developer-preview/issues/detail?id=29

推荐答案

对于我最近参与的一个项目,我还需要检测某些应用程序何时启动.我所有的研究都导致了 getRunningTasks 方法,该方法从 Lollipop 开始已被弃用.然而,令我惊讶的是,我发现一些应用程序锁定应用程序仍然可以在棒棒糖设备上运行,所以他们一定想出了一个解决方案来解决这个问题.所以我挖得更深一些.这是我发现的:

For a recent project that I worked on, I also need to detect when certain applications are launched. All my research lead to the getRunningTasks method, which is deprecated starting from Lollipop. However, to my surprises, I discovered that some of the app lock apps still work on lollipop devices, so they must have come up with a solution to get around this. So I dug a little deeper. Here is what I found out:

  1. 在 L 之前的设备上,他们仍然使用 getRunningTasks

    1. 在 L 台设备上,它们使用 getRunningAppProcesses,它返回当前在设备上运行的进程列表.您可能会想好吧,那没有用".每个 processInfo 都有一个称为重要性的属性.当应用程序成为顶级活动时,其 processInfo 重要性也会更改为 IMPORTANCE_FOREGROUND.所以你可以过滤掉那些不在前台的进程.从每个 ProcessInfo 中,您还可以询问它加载的包列表.然后,您可以检查列表是否包含与应用程序在尝试受保护"时相同的包.

  • 检测默认日历应用何时启动的一些示例代码:

    Some sample code to detect when the default calendar app is launched:

    public class DetectCalendarLaunchRunnable implements Runnable {
    
    @Override
    public void run() {
      String[] activePackages;
      if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
        activePackages = getActivePackages();
      } else {
        activePackages = getActivePackagesCompat();
      }
      if (activePackages != null) {
        for (String activePackage : activePackages) {
          if (activePackage.equals("com.google.android.calendar")) {
            //Calendar app is launched, do something
          }
        }
      }
      mHandler.postDelayed(this, 1000);
    }
    
    String[] getActivePackagesCompat() {
      final List<ActivityManager.RunningTaskInfo> taskInfo = mActivityManager.getRunningTasks(1);
      final ComponentName componentName = taskInfo.get(0).topActivity;
      final String[] activePackages = new String[1];
      activePackages[0] = componentName.getPackageName();
      return activePackages;
    }
    
    String[] getActivePackages() {
      final Set<String> activePackages = new HashSet<String>();
      final List<ActivityManager.RunningAppProcessInfo> processInfos = mActivityManager.getRunningAppProcesses();
      for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
        if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
          activePackages.addAll(Arrays.asList(processInfo.pkgList));
        }
      }
      return activePackages.toArray(new String[activePackages.size()]);
    }
    }
    

    注意:getRunningAppProcesses 还用于调试或构建面向用户的进程管理 UI".不确定谷歌是否会像关闭 getRunningTasks 一样关闭这个后门.

    Note: getRunningAppProcesses is also intended for debugging or "building a user-facing process management UI". Not sure if google will close this backdoor the similar way they did to getRunningTasks.

    所以不,你不能再得到 topActivity 了.但只要稍加修改,您就可以获得类似的结果.

    So no, you can't get the topActivity anymore. But with a little bit hack you can achieve similar result.

    这篇关于getRunningTasks 在 Android L 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆