在不知道其全名的情况下获取进程的 PID 以杀死它 [英] Get the PID of a process to kill it, without knowing its full name
问题描述
我正在编写一个 Android 应用程序.现在我要去应用程序应该终止进程的部分.但我不知道它的全名或 PID.我知道命令:
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)
和
android.os.Process.getUidForName("com.android.email")
但我的问题是我不知道进程的全名.
But my problem is that I don't know the full name of the process.
这是一个本地代码进程,所以不是像 com.something.something
It's an native code process, so not something like com.something.something
流程为/data/data/com.something.something/mybinary
但它正在运行像
/data/data/com.something.something/mybinary -a 123 -b 456
因此我不能使用
android.os.Process.getUidForName("/data/data/com.something.something/mybinary")
推荐答案
您可以使用:
ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> services = manager.getRunningAppProcesses();
String service1name = services[1].processName;
你可以得到所有正在运行的进程的包名,查看你想杀死哪个进程,通过service.pid选择那个进程获取进程id.
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.
并调用:
android.os.Process.killProcess(service.pid);
这篇关于在不知道其全名的情况下获取进程的 PID 以杀死它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!