getRunningTasks不能在Android的大号工作 [英] getRunningTasks doesn't work in Android L

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

问题描述

在Android的L,谷歌已禁用getRunningTasks。现在,它只能返回自己的应用程序的任务和家庭发射。我不能再获得其他应用程序的任务。 我们的应用程序需要一个方法来确定目前的顶级应用程序。 任何一个有另一种方法来做到这一点?

我有搜索在谷歌,这个没有更多的话题,除了这一点: <一href="https://$c$c.google.com/p/android-developer-$p$pview/issues/detail?id=29">https://$c$c.google.com/p/android-developer-$p$pview/issues/detail?id=29

解决方案

有关我工作的最近的一个项目,我还需要检测时,某些应用程序的启动。我所有的研究引到getRunningTasks方法,这是德precated从棒棒堂开始。 然而,我的惊喜,我发现有些应用程序锁的应用程序仍然工作的棒棒糖设备,所以他们必须想出了一个解决方案来解决这个问题。所以,我挖得更深一些。下面是我发现了什么:

    1. 在preL设备,他们仍然使用getRunningTasks
  • 在L器件,他们用getRunningAppProcesses,它返回对设备当前运行的进程的列表。你可能会想好,那是没有用的。每个processInfo有一个归咎于所谓的重要性。当一个应用程序成为顶级的活动,其processInfo重要性也变成IMPORTANCE_FOREGROUND。所以,你可以过滤掉这些进程是不是在前台。从各ProcessInfo,你也可以问包它加载列表。然后,您可以检查列表包含在试图保护的应用程序相同的封装。

有些样品code检测时,默认的日历应用程序启动:

 公共类DetectCalendarLaunchRunnable实现Runnable {

@覆盖
公共无效的run(){
  的String [] activePackages;
  如果(Build.VERSION.SDK_INT&GT; Build.VERSION_ codeS.KITKAT_WATCH){
    activePackages = getActivePackages();
  } 其他 {
    activePackages = getActivePackagesCompat();
  }
  如果(activePackages!= NULL){
    对于(字符串activePackage:activePackages){
      如果(activePackage.equals(com.google.android.calendar)){
        //日历应用程序启动时,做一些事情
      }
    }
  }
  mHandler.postDelayed(这一点,1000);
}

的String [] getActivePackagesCompat(){
  最后的名单,其中,ActivityManager.RunningTaskInfo&GT; taskInfo = mActivityManager.getRunningTasks(1);
  最终的组件名组件名= taskInfo.get(0).topActivity;
  最终的String [] activePackages =新的String [1];
  activePackages [0] = componentName.getPackageName();
  返回activePackages;
}

的String [] getActivePackages(){
  决胜盘&LT;字符串&GT; activePackages =新的HashSet&LT;字符串&GT;();
  最后的名单,其中,ActivityManager.RunningAppProcessInfo&GT; processInfos = mActivityManager.getRunningAppProcesses();
  对于(ActivityManager.RunningAppProcessInfo processInfo:processInfos){
    如果(processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
      activePackages.addAll(Arrays.asList(processInfo.pkgList));
    }
  }
  返回activePackages.toArray(新的String [activePackages.size());
}
}
 

注:getRunningAppProcesses也用于调试或建设面向用户的流程管理UI。不知道谷歌将关闭这个后门他们做了getRunningTasks了类似的方式。

所以,不,你不能获得topActivity了。但随着一点点的黑客就可以达到同样的效果。

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?

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

解决方案

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. On pre-L devices, they still use getRunningTasks

    1. On L devices, they use getRunningAppProcesses, which returns a list of processes currently running on the devices. You might think "well, that is not useful". Each processInfo has a attributed called importance. When an app becomes top activity, its processInfo importance also changes to IMPORTANCE_FOREGROUND. So you can filter out those processes that are not in foreground. From a each ProcessInfo, you can also ask a list of packages it loaded. You can then check if the list contains the same package that the app when are trying "protected".

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()]);
}
}

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.

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

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

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