Get the PID of a process to kill it, without knowing its full name

user1114653 picture user1114653 · Dec 24, 2011 · Viewed 17.8k times · Source

I'm coding an Android application. Now I'm going to a part where the application should kill a process. But I don't know its full name or its PID. I Know the commands:

android.os.Process.killProcess(Pid)

and

android.os.Process.getUidForName("com.android.email")

But my problem is that I don't know the full name of the process.

It's an native code process, so not something like com.something.something

The process is /data/data/com.something.something/mybinary

but it's running with commands like

/data/data/com.something.something/mybinary -a 123 -b 456

because of this I can't use

android.os.Process.getUidForName("/data/data/com.something.something/mybinary")

Answer

AAnkit picture AAnkit · Dec 24, 2011

You can use:

ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
 List<RunningAppProcessInfo> services = manager.getRunningAppProcesses();
 String service1name = services[1].processName;

You can get all running process's package names, check which one you want to kill, choose that process get process id by service.pid.

And call:

android.os.Process.killProcess(service.pid);