使用activityManager.getRunningAppProcesses()获取(真实的)前台流程 [英] Get (real) foreground process using activityManager.getRunningAppProcesses()

查看:681
本文介绍了使用activityManager.getRunningAppProcesses()获取(真实的)前台流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为用户确定当前可见的应用程序.为此,我使用 activityManager.getRunningAppProcesses()方法.

I am trying to determine the currently visible application for the user. To do so, I use the activityManager.getRunningAppProcesses() method.

我知道从Android 5.1.1开始不支持该方法-没关系.

一开始它就像魅力一样工作,我要遍历整个列表的RunningAppProcessInfos并检查其重要性.

In the beginning it worked like charm, I am iterating through the list of RunningAppProcessInfos and check for the importance.

tl;博士什么是获取当前前台进程并防止假阳性的正确方法?列表的顺序如何完成-API表示未指定.有没有办法正确订购它们?

我做了什么:

不幸的是,ActivityManager返回的列表在每个设备上都不相同.即使当前不可见该过程,某些设备也会返回多个重要性为100(即FOREGROUND)的信息.

Unfortunately the list returned by the ActivityManager is different on every device. Some devices return multiple infos with the importance 100 (which is FOREGROUND), even if the process is currently not visible.

这引起了我有很多误报的问题.例如,每次我都在facebook上测试它并创建一个吐司Facebook是前台.我切换到家庭,所以我的启动器应该有是前台程序.大约10秒钟之后,facebook再次以重要性FOREGROUND出现在列表中(它既未打开也不可见),我得到了误报.

This caused the problem that I had a lot of false positives. For example, I tested it with facebook and created a toast, every time facebook is in foreground. I switched to Home, so my launcher should have been the foreground process. It was for like 10 seconds, after that, facebook appeared with importance FOREGROUND in the list again (it was not open nor visible) and I got a false positive.

我认识到某些设备在最近的过程中按顺序对列表进行排序.因此,我决定尝试以下方法:

I recognized that some devices order the list by the recent process on top. So I decided to try the following approach:

ActivityManager.RunningAppProcessInfo appProcess = appProcesses.get(0);
        final String processName = appProcess.processName;
        final String packageName = removeProcessSuffix(processName);
        if (appProcess.importance == FOREGROUND || appProcess.importance == VISIBLE){
            //do something, like the toast
        }

使用 appProcesses.get(0); ,我仅检查了第一个元素,该错误消失了.不再有误报.

With appProcesses.get(0); I only examine the first element and the bug was gone. No false positives anymore.

但是,现在在某些设备上,我根本没有前台处理.因为他们不以任何方式对列表进行排序.例如,一些4.2索尼智能手机就是其中的一些候选者.我得到了列表,但是当前真正可见的过程在索引11左右.

But, now on some devices I do not get the foreground process at all anymore. Because they do not order the list in any way. For example some 4.2 Sony smartphones are some of the candidates. I get the list, but the really currently visible process is at index 11 or so.

我不知道如何做才能获得当前的前台流程,而没有误报.

I have no clue what to do, to get the current foreground process, without false positives.

什么是获取当前前台进程并防止假阳性的正确方法?列表的顺序如何完成-API表示未指定.有没有办法正确订购它们?

What is the correct method to get current foreground process and prevent false postives? How is the order of the list done - the API says it is not specified. Is there a way to order them correctly?

谢谢!

推荐答案

获取当前前台进程的正确方法是什么?防止误报?

What is the correct method to get current foreground process and prevent false positives?

UsageStatsManager 是唯一获得以下授权的官方API当前正在运行的应用程序(请参阅#50 ).

UsageStatsManager is the only official API to get the current running app (see #50).

使用 getRunningTasks(int maxNum) getRunningAppProcesses() AccessibilityService 来获取前台应用从未如此可靠.前两种方法的文档具有以下警告: 注意:此方法仅用于调试

Using getRunningTasks(int maxNum), getRunningAppProcesses() or AccessibilityService to get the foreground app has never been reliable. The documentation for the first two methods has the following warning: Note: this method is only intended for debugging

以下是使用 UsageStatsManager 获取顶级应用的示例a>:

Below is an example for getting the top app using UsageStatsManager:

Calendar endCal = Calendar.getInstance();
Calendar beginCal = Calendar.getInstance();
beginCal.add(Calendar.MINUTE, -30);
UsageStatsManager manager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
List<UsageStats> stats =  manager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,  
    beginCal.getTimeInMillis(), endCal.getTimeInMillis());
Collections.sort(stats, new Comparator<UsageStats>() {

  @Override public int compare(UsageStats lhs, UsageStats rhs) {
    long time1 = lhs.getLastTimeUsed();
    long time2 = rhs.getLastTimeUsed();
    if (time1 > time2) {
      return -1;
    } else if (time1 < time2) {
      return 1;
    }
    return 0;
  }
});
// The first "UsageStats" in the list will be the top application.
// If the list is empty you will need to ask for permissions to use UsageStatsManager
// To request permission:
// startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));


我知道这不是您想要的答案.诸如 CM安全性获取前台应用程序 getRunningTasks(int maxNum)


I know this isn't the answer you are hoping for. Apps like CM Security and AppLock use UsageStatsManager on Android 5.1.1+. Due to SeLinux, it is impossible to get the foreground app using getRunningTasks(int maxNum) or getRunningAppProcesses().

这篇关于使用activityManager.getRunningAppProcesses()获取(真实的)前台流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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