获取一个进程的PID来杀死它

获取一个进程的PID来杀死它

本文介绍了获取一个进程的PID来杀死它,而不知道它的全名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Android应用程序。现在我要去应用程序杀死进程的一部分。但我不知道它的全名或其PID。我知道命令:

  android.os.Process.killProcess(Pid)

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

但是我的问题是我不知道



这是一个本地的代码过程,所以不像com.something.something那样的东西



该过程是 /data/data/com.something.something/mybinary



但它正在运行, / p>

  /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); 
列表< RunningAppProcessInfo> services = manager.getRunningAppProcesses();
String service1name = services [1] .processName;

您可以获取所有正在运行的进程的包名,检查要杀死的包,选择该进程获取进程id由service.pid。



并调用:

  android .os.Process.killProcess(service.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)

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")
解决方案

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

这篇关于获取一个进程的PID来杀死它,而不知道它的全名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:33