简体   繁体   中英

Get Index value of specific string in a loop

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM