0

So I'm trying to get the PID (Process ID) of a certain app that's running on my phone.

What I'm doing first is:

ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pidsTask = activityManager.getRunningAppProcesses();

for(int i = 0; i < pidsTask.size(); i++){
    nameList.append(pidsTask.get(i).processName + pidsTask.get(i).pid + "\n"); 

    }

This get's the name + PID from 'every' app that's running currently.

Now, I want to get the PID of a certain app name "Com.ExampleName". Since the PID can/is Dynamic and can possibly change, I want it to be able to search the array for the name, get the position (i) and get the PID that belongs to that position (i).

How can I acomplish that?

Paramone
  • 2,634
  • 4
  • 31
  • 58
  • why not use a map for this Key, Value (processName, ProcessID) pair? – TheLostMind Mar 31 '14 at 10:23
  • 2
    Why not use a map instead of a List.... Map and search the map based on the value instead of the key? http://stackoverflow.com/questions/1383797/java-hashmap-how-to-get-key-from-value – Schokea Mar 31 '14 at 10:24

1 Answers1

0

The Map suggestion is good, but if you need to do it this way, you can loop through the list, and for each string check

if nameList.get(i).substring(0,appName.length).equals(appName) {
    String pidStr = nameList.get(i).substring(appName.length,nameList.get(i).length())
    int pid= Integer.parseInt(pidStr);
}
Inbl
  • 630
  • 2
  • 5
  • 18