【问题标题】:getRunningAppProcesses returns empty list on Android M(5.1.1)getRunningAppProcesses 在 Android M(5.1.1) 上返回空列表
【发布时间】:2015-11-03 05:05:24
【问题描述】:

我刚刚测试了我的应用程序和 CM、ATM Android 助手等。它们都无法获得正在运行的进程列表,但它们在预操作系统版本上运行良好。那么Android M(5.1.1)发生了什么?请帮忙!

am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> list = am.getRunningAppProcesses();
Log.i(TAG, "LQ::examine list.size()=" + list.size());

【问题讨论】:

  • “那么 Android 5.1.1 Lollipop 发生了什么?” Android 5.1.1 Lollipop 发生了什么?结果如何?
  • 我无法在 5.1.1 上获取正在运行的任务列表。 BUt 在 pre os 版本上运行良好。
  • 所以 getRunningAppProcessesgetRunningTasks ?你能决定什么是行不通的吗?
  • getRunningAppProcesses
  • 注意:此方法仅用于调试,您是否将其用于其他目的?

标签: android process


【解决方案1】:

我可以在 android 6.0 上使用getRunningServices() 方法获取前台应用列表。谢谢@thecr0w

ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List appProcessInfoList = mActivityManager.getRunningServices(Integer.MAX_VALUE);

【讨论】:

    【解决方案2】:

    我决定改用 getRunningServices!

    Du Speed Booster & Power Clean 改用 getRunningServices,也许 getRunningAppProcesses 将来会被弃用。

    谢谢谷歌,谢谢字母表。

        Hashtable<String, List<ActivityManager.RunningServiceInfo>> hashtable = new Hashtable<String, List<ActivityManager.RunningServiceInfo>>();
        ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo rsi : am.getRunningServices(Integer.MAX_VALUE)) {
            if (isCanceled()) {
                return;
            }
    
            String pkgName = rsi.service.getPackageName();
            if (hashtable.get(pkgName) == null) {
                List<ActivityManager.RunningServiceInfo> list = new ArrayList<ActivityManager.RunningServiceInfo>();
                list.add(rsi);
                hashtable.put(pkgName, list);
            } else {
                hashtable.get(pkgName).add(rsi);
            }
        }
    
        int i = 0;
        int size =  hashtable.size();
        for (Iterator it = hashtable.keySet().iterator(); it.hasNext(); i++) {
            String key = (String) it.next();
            List<ActivityManager.RunningServiceInfo> value = hashtable.get(key);
            ProcessItem item = new ProcessItem(getContext(), value.get(0).pid, key, totalCpu, totalRam);
            if (!whiteList.contains(item.pkgName)) {
                if (!killList.contains(item.pkgName)) {
                    killList.add(item.pkgName);
                    ramTotal += item.ram;
    
                    if (getListener() != null) {
                        Progress progress = new Progress(this);
                        progress.setArg1(i);
                        progress.setArg2(size);
                        progress.setMsg(item.appName);
                        progress.setObj(item);
                        getListener().onExamining(progress);
                    }
                }
            }
        }
        hashtable.clear();
        hashtable = null;
    

    【讨论】:

    • getRunningServices 将如何返回所有正在运行的应用程序进程?如果我没记错的话,它只会返回正在运行的服务
    • @Lester,我们是像 ATK 一样的进程杀手应用,所以我们需要运行进程或服务之类的东西。
    【解决方案3】:

    另一种选择是使用UsageStatsManager

    UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(Context.USAGE_STATS_SERVICE);
    long endTime = System.currentTimeMillis();
    long beginTime = endTime - 1000*60;
    
    // We get usage stats for the last minute
    List<UsageStats > stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, beginTime, endTime);
    
    // Sort the stats by the last time used
    if(stats != null) 
    {
        SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>();
        for (UsageStats usageStats : stats) 
        {
            mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
        }
        if(mySortedMap != null && !mySortedMap.isEmpty()) 
        {
            topActivity =  mySortedMap.get(mySortedMap.lastKey()).getPackageName();
        }
    }
    

    为了使其工作,您需要PACKAGE_USAGE_STATS 权限。您可以通过在设置中打开屏幕来提示用户执行此操作:

    Intent usageAccessIntent = new Intent( Settings.ACTION_USAGE_ACCESS_SETTINGS );
    usageAccessIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
    startActivity( usageAccessIntent );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 2021-04-14
      • 2017-04-21
      相关资源
      最近更新 更多