Android get PID of other applications
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 an开发者_Python百科d the time it takes me to find what I want in the proc directory and begin observing?
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.
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)
try this
int id= android.os.Process.myPid();
for (RunningAppProcessInfo runningProInfo : runningProcInfo) {
int pid = runningProInfo.pid;
Log.e(TAG+"-pid", ""+pid);
}
Where TAG = "Name_Of_Your_Activity"
精彩评论