简体   繁体   English

如何在Android中强制关闭当前正在运行的应用程序

[英]how to force close the currently running application in android

i am trying to find out the list of processes which is running in the device. 我试图找出设备中正在运行的进程列表。 after that i want to force close all the process except my current application. 之后,我想强制关闭除当前应用程序以外的所有进程。 I tried using this below code . 我尝试使用下面的代码。 Its listing out the processed but its not killing the processes . 它列出了已处理的,但是不会杀死进程。

ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);

 List<RunningAppProcessInfo> process = manager.getRunningAppProcesses();  




  for (int i = 1 ;i< process.size();i++)

  {
    int pid = process.get(i).pid;            

    System.out.println("Task " + process.get(i).processName);  

    android.os.Process.killProcess(pid);is 
   }

This above code only i tried to kill the processes. 上面的代码只是我试图杀死进程。 once after the killprocess called i again called the getRunningAppProcess() to check the process are killed or not. 在killkill调用之后,我再次调用getRunningAppProcess()来检查进程是否被杀死。 still its showing all the processes. 仍然显示所有过程。

Its listing out the processed but its not killing the processes . 它列出了已处理的,但是不会杀死进程。

Of course. 当然。 You do not have rights to kill other processes using killProcess() . 您无权使用killProcess()杀死其他进程。 I have no idea why you think you would have such a right. 我不知道你为什么认为自己会有这样的权利。

We want to kill all foreground as well as background application also. 我们也想杀死所有前台和后台应用程序。

This will crash the operating system. 这将使操作系统崩溃。

If you only want your application running, build your own devices with your own firmware and your own OS and your own GUI framework, put your application on those devices, and distribute those devices. 如果仅希望您的应用程序运行,请使用自己的固件,操作系统和GUI框架构建自己的设备,将应用程序放在这些设备上,然后分发这些设备。

You can try killBackgroundProcesses() (it needs package names), you need the KILL_BACKGROUND_PROCESSES permission for it. 您可以尝试killBackgroundProcesses()(它需要包名称),需要它的KILL_BACKGROUND_PROCESSES权限。 And you can kill your own process without a special permission (be sure to do it last). 而且,您可以在没有特殊许可的情况下终止自己的进程(请确保最后执行)。 If it still doesn't work, you need to root the phone, and do it as root. 如果仍然无法正常工作,则需要将手机设置为root,然后以root用户身份进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM