Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getRunningAppProcesses returns empty list on Android M(5.1.1)

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 Android M(5.1.1)? please help!

am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> list = am.getRunningAppProcesses();
Log.i(TAG, "LQ::examine list.size()=" + list.size());
like image 275
thecr0w Avatar asked Aug 11 '15 05:08

thecr0w


3 Answers

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;
like image 106
thecr0w Avatar answered Oct 22 '22 12:10

thecr0w


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 );
like image 25
kingjason611 Avatar answered Oct 22 '22 14:10

kingjason611


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);
like image 27
Joe Avatar answered Oct 22 '22 13:10

Joe