How to Get Pid of Android Application Without Using Adb Shell

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

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

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

Android get PID of other applications

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:

  1. Get all running app processes.
  2. Find your app.
  3. Get its PID.

Android: determinine my app's process ID?

You're looking for this, I think:

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

adb: Find PID from the adb shell

Not sure if you can get the PID directly however you can try the following


set `ps |grep android.process.acore`
echo $2

This has the affect of setting the output of the ps command into variables $1, $2, $3 etc. The PID value is in $2

How to get PID from package name?

try

restartPackage code

ActivityManager aM = (ActivityManager);
getApplicationContext().getSystemService(getApplicationContext().ACTIVITY_SERVICE);
aM.restartPackage("com.android.email");

Kill BackGround Process Code

ActivityManager aM = (ActivityManager)
getApplicationContext().getSystemService(Catalogue.content_holder.getApplicationContext().ACTIVITY_SERVICE);
aM.killBackgroundProcesses("com.android.email");

Here is code which fetches all running Application and check wether email app is already running or not , if it is running then kill that process

ActivityManager manager =  (ActivityManager) getApplicationContext.getSystemService(getApplicationContext.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> activityes = ((ActivityManager)manager).getRunningAppProcesses();

for (int iCnt = 0; iCnt < activityes.size(); iCnt++){

System.out.println("APP: "+iCnt +" "+ activityes.get(iCnt).processName);

if (activityes.get(iCnt).processName.contains("com.android.email")){
android.os.Process.sendSignal(activityes.get(iCnt).pid, android.os.Process.SIGNAL_KILL);
android.os.Process.killProcess(activityes.get(i).pid);
//manager.killBackgroundProcesses("com.android.email");

//manager.restartPackage("com.android.email");

System.out.println("Inside if");
}

}


Related Topics



Leave a reply



Submit