如何使用进程ID退出选定的正在运行的应用程序[英] How to exit selected running applications using process id

本文是小编为大家收集整理的关于如何使用进程ID退出选定的正在运行的应用程序的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用此代码来停止Android手机中所选的运行应用程序:

Button view = (Button)findViewById(R.id.button1);
view.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){

String appname = getIntent().getExtras().getString("appname");
String pname=appname.split("\n")[3];
int id=Integer.parseInt(pname);
finish();
android.os.Process.killProcess(id);

id是正确的,但是这个代码不起作用......任何帮助? 是否有任何其他代码通过使用其ID?

来杀死进程

推荐答案

如果检查 Android API for杀死过程它说:

用给定的PID杀死过程.请注意,虽然此API允许我们根据其PID请求杀死任何进程,但内核仍将对您实际能够杀死的PID施加标准限制.通常,这意味着只有运行来电者的软件包/应用程序的进程以及该应用程序创建的任何其他进程;共享共同uid的包也能够杀死彼此的进程.

这个答案解释了为什么它是如此以及它是如何工作的: http://stackoverflow.com/a/7560009/1306012 a>

本文地址:https://www.itbaoku.cn/post/102774.html

问题描述

I am using this code to stop the selected running applications in android phone:

Button view = (Button)findViewById(R.id.button1);
view.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){

String appname = getIntent().getExtras().getString("appname");
String pname=appname.split("\n")[3];
int id=Integer.parseInt(pname);
finish();
android.os.Process.killProcess(id);

The Id is correct but this code is not working... any help?? Is there any other code to kill processes by using their id??

推荐答案

if you check the Android API for Kill Process it says the following:

Kill the process with the given PID. Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.

This answer explains why it is so and how it works: http://stackoverflow.com/a/7560009/1306012