Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

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

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")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...