I would like to be able to start an activity or service and get the PID of that process as quickly as possible, immediately would be the best case scenario. Do I have any options other than browsing the /proc directory, which then leads to a variable-amount-of-time race condition between the time the activity/service is launched and the time it takes me to find what I want in the proc directory and begin observing?
Asked
Active
Viewed 2.7k times
13
-
1Just curious, but what on earth for? – John May 18 '11 at 03:23
-
1Apps components can be run on different process and components from different apps can share the same process, so there's no a one-to-one relationship, so it's pointless. – Diego Torres Milano May 18 '11 at 04:35
-
@john working on an application profiler for security purposes. It's important that I be able to get my monitoring tools in place and gathering data before the app spins up. – MattC May 18 '11 at 13:30
4 Answers
13
I think you'd need to use ActivityManager: see http://developer.android.com/reference/android/app/ActivityManager.RunningAppProcessInfo.html for the process info. You could:
- Get all running app processes.
- Find your app.
- Get its PID.

Femi
- 64,273
- 8
- 118
- 148
-
Thanks. I fear this too will lead to a race condition between when I start the app to profile and get the PID, but it seems cleaner than browsing the /proc directory so you get a +1 – MattC May 18 '11 at 14:32
-
1Ah: if you're looking in another process for this then yes, there is some possibility you will not see it for an (undefined) little while. Not sure there is a better alternative though: let me know if you find one. – Femi May 18 '11 at 14:50
-
1Is there any way to accomplish this on Android API 21 and above? I don't think RunningAppProcessInfo is exposed in Android L and above. – gaurav jain Dec 23 '15 at 13:20
-
Is there a difference between google processes and normal app processes? If I check my running processes like this, I retrieve the current process of my own application. But the mobile is running Google Maps the same time, but I can't determine the Google Maps process. (or is there any other way on how to retrieve google maps pid?) – The Chris Mar 18 '19 at 11:00
7
ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pidsTask = activityManager.getRunningAppProcesses();
for(int i = 0; i < pidsTask.size(); i++) {
nameList.add(pidsTask.get(i).processName);
idList.add(pidsTask.get(i).uid);
}
pidsTask.get(i).uid // Return PID for Apps(Process)

SilentKiller
- 6,944
- 6
- 40
- 75

Lyusten Elder
- 3,213
- 1
- 14
- 10
-
1the uid IS NOT the apps actual pid for example, its pid may be 302 while its uid may be 10039 – PSP CODER May 02 '20 at 07:31
0
try this
int id= android.os.Process.myPid();

neeraj t
- 4,654
- 2
- 27
- 30
-
4
-
3this answer is copied http://stackoverflow.com/questions/7454909/how-to-get-pid-of-android-application-with-out-using-adb-shell – devin Jan 17 '13 at 20:48
-1
for (RunningAppProcessInfo runningProInfo : runningProcInfo) {
int pid = runningProInfo.pid;
Log.e(TAG+"-pid", ""+pid);
}
Where TAG = "Name_Of_Your_Activity"