getRunningAppProcesses returns empty list on Android M(5.1.1)

前端 未结 3 448
花落未央
花落未央 2021-02-03 10:34

I just tested my app and CM, ATM Android Assistant, etc. All of them can not get the running process list but they works fine on pre OS version. So what\'s going on with Androi

相关标签:
3条回答
  • 2021-02-03 10:59

    I could get foreground app list by using getRunningServices() method on android 6.0. Thanks @thecr0w

    ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List appProcessInfoList = mActivityManager.getRunningServices(Integer.MAX_VALUE);
    
    0 讨论(0)
  • 2021-02-03 11:07

    I decide to use getRunningServices instead!

    Du Speed Booster & Power Clean uses getRunningServices instead, maybe getRunningAppProcesses is deprecated in future.

    Thank you google, thank you alphabet.

        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;
    
    0 讨论(0)
  • 2021-02-03 11:09

    Another option is to use 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();
        }
    }
    

    In order for this to work, you need PACKAGE_USAGE_STATS permission. You can prompt the user to do this by opening the screen in settings:

    Intent usageAccessIntent = new Intent( Settings.ACTION_USAGE_ACCESS_SETTINGS );
    usageAccessIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
    startActivity( usageAccessIntent );
    
    0 讨论(0)
提交回复
热议问题