55

How can I get an android application pid without using adb shell? Is there any API to get pid. any help will be appreciated

Gabboxl
  • 91
  • 3
  • 16
RanjitRock
  • 1,421
  • 5
  • 20
  • 36

2 Answers2

112

As every application has its own process id, one can get it by

int pid = android.os.Process.myPid();
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
17

This also works:

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
int processid = 0;
for (int i = 0; i < pids.size(); i++) {
    ActivityManager.RunningAppProcessInfo info = pids.get(i);
    if (info.processName.equalsIgnoreCase("here your package name")) {
       processid = info.pid;
    } 
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
kiran boghra
  • 3,662
  • 2
  • 18
  • 23