getRunningAppProcesses returns empty list on Android M(5.1.1)

前端 未结 3 497
花落未央
花落未央 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条回答
  •  萌比男神i
    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> hashtable = new Hashtable>();
        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 list = new ArrayList();
                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 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;
    

提交回复
热议问题