11

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 L (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());
Ryan M
  • 18,333
  • 31
  • 67
  • 74
thecr0w
  • 2,148
  • 4
  • 33
  • 59
  • "So what's going on with Android 5.1.1 Lollipop?" and what's going on with Android 5.1.1 Lollipop? what's the result? – pskink Aug 11 '15 at 05:50
  • I cannot get Running Task List on 5.1.1. BUt works fine on pre os version. – thecr0w Aug 11 '15 at 06:36
  • so `getRunningAppProcesses` or `getRunningTasks` ? can you decide what is not working? – pskink Aug 11 '15 at 06:57
  • getRunningAppProcesses – thecr0w Aug 11 '15 at 06:58
  • **Note: this method is only intended for debugging**, do you use it for other purposes? – pskink Aug 11 '15 at 07:00
  • Check this [difference-between-running-task-and-running-process-in-android](http://stackoverflow.com/questions/8148420/difference-between-running-task-and-running-process-in-android) – Sushil Aug 11 '15 at 07:49
  • Du Speed Booster & Power Clean uses getRunningServices instead, maybe getRunningAppProcesses is deprecated in future. – thecr0w Aug 11 '15 at 08:26
  • @pskink debugging OR use cases to show the user a UI-ish overview of the running processes- There are legal reasons to use this api! – JacksOnF1re Sep 18 '15 at 17:31

3 Answers3

6

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;
thecr0w
  • 2,148
  • 4
  • 33
  • 59
  • 1
    how getRunningServices will return all running app processes? If i am not mistaken it wull return only running services – Lester Aug 23 '15 at 18:16
  • 2
    @Lester, We are a process killer app like ATK, so we need running things like process or service. – thecr0w Aug 25 '15 at 02:54
2

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 );
1

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);
N J
  • 27,217
  • 13
  • 76
  • 96
Joe
  • 19
  • 3