RanjitRock
RanjitRock

Reputation: 1441

How to get pid of android application without using adb shell?

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

Upvotes: 55

Views: 48161

Answers (2)

kiran boghra
kiran boghra

Reputation: 3822

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

Upvotes: 17

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

int pid = android.os.Process.myPid();

Upvotes: 112

Related Questions