Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get PID from package name?

Tags:

android

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);
like image 890
cobes Avatar asked Aug 13 '14 21:08

cobes


People also ask

How to get PID from package name in android?

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.

How get PID details in Linux?

In this quick article, we've explored how to get the name and the command line of a given PID in the Linux command line. The ps -p <PID> command is pretty straightforward to get the process information of a PID. Alternatively, we can also access the special /proc/PID directory to retrieve process information.

How do I find out what processes are running on my Android?

The ability to view running processes in Android is found in the Developer Options. In order to do that, open the Settings app and locate About Phone. In the About Phone section, locate Build Number (Figure A). The Build Number of your device lists the date your version of Android was built.


1 Answers

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

}
like image 113
Davide Avatar answered Oct 05 '22 06:10

Davide