8

I need create a function but I don't know how to get the PID of the app.

// here return -1 but I need PID to kill process
Integer PID1= android.os.Process.getUidForName("com.android.email");

android.os.Process.killProcess(PID1);
BeNdErR
  • 17,471
  • 21
  • 72
  • 103
cobes
  • 93
  • 1
  • 1
  • 9
  • You cannot kill a third-party process via PID using `killProcess()`. Quoting [the documentation](http://developer.android.com/reference/android/os/Process.html#killProcess%28int%29): "Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes." – CommonsWare Aug 13 '14 at 21:56
  • have a look here http://stackoverflow.com/questions/7454909/how-to-get-pid-of-android-application-with-out-using-adb-shell – BeNdErR Aug 13 '14 at 21:56
  • for getUidForName, the argument isn't of the package name. It's of the user name of the process (each process gets its user name). For this, you can write "ps" or "top -n 1" command, and use something like "Process.getUidForName("u0_a185")" or "Process.getUidForName("root")" . – android developer Oct 26 '16 at 23:08

1 Answers1

6

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

}
Davide
  • 622
  • 4
  • 10