简体   繁体   English

Android:杀死进程并关闭应用程序

[英]Android: kill processes and close applications

Im working on a simple task manager that would allow user to kill running processes and terminate applications. 我正在开发一个简单的任务管理器,允许用户终止正在运行的进程并终止应用程序。 Im getting a running processes list using: 我使用以下命令获取正在运行的进程列表:

List<RunningAppProcessInfo> procInfo = activityManager.getRunningAppProcesses();

With one click of a button I want to kill all running processes that arent important or essencial in any way. 只需单击一下按钮,我就想杀死所有以任何方式都不重要或不重要的正在运行的进程。 For this, Im using: 为此,我使用:

ArrayList<String> packages = new ArrayList<String>();
for (int i = 0; i < procInfo.size(); i++) {
    String process = procInfo.get(i).processName;
    if (!packages.contains(process)) {
            packages.add(process);
    }
}

This will get me names of all running processes and now I should probably filter them somehow using their importance code: 这将为我获取所有正在运行的进程的名称,现在我应该使用其重要性代码以某种方式对其进行过滤:

int importance = procInfo.get(i).importance

But Im not really sure how. 但是我不确定如何。 What should I keep running and what is safe to kill? 我应该继续运行什么并且可以安全杀死什么? Im using 我正在使用

applicationManager.killBackgroundProcesses(process);

to kill my process. 杀死我的过程。 My next question is, is this the best way to kill a process? 我的下一个问题是,这是杀死进程的最佳方法吗? Because when I do it like this and than I open again one of the applications for which all processes were killed, I find myself not in its Main Activity but right there where I left it, so I asume that the application was never terminated even though all its processes were killed. 因为当我这样做并再次打开其中一个所有进程都被杀死的应用程序时,我发现自己不在其主活动中,而是在我离开它的地方,所以我假设即使该应用程序也从未终止它的所有过程都被杀死了。 Also, I fint that application in quite a desolate state (ImageViews and Buttons are missing, TextViews arent where theyre supposed to be). 此外,我发现该应用程序处于相当荒凉的状态(缺少ImageViews和Button,TextViews不在应有的位置)。 Note that this application is also written by myself and normally it works perfectly fine. 请注意,该应用程序也由我自己编写,通常可以正常运行。

My last question is, when this problem with killing processes is solved, I want also to terminate selected applications. 我的最后一个问题是,当解决了杀死进程的问题时,我还想终止选定的应用程序。 How do I get the list of running applications? 如何获取正在运行的应用程序列表? I was thinking getting a list of running processes and than somehow get applications from that list. 我在考虑获取正在运行的进程的列表,而不是从该列表中获取应用程序。 After that, terminating a selected application by killing all its processes. 之后,通过终止所有应用程序来终止所选应用程序。 But I dont think thats the best way since it obviously didnt work when I killed all processes. 但是我不认为这是最好的方法,因为当我杀死所有进程时,它显然不起作用。

Thank you for any reply! 感谢您的答复!

Killing apps in Android is usually considered a bad idea. 在Android中杀死应用程序通常被认为是一个坏主意。 This is because the Android OS is designed to handle all Task Management. 这是因为Android操作系统旨在处理所有任务管理。 Usually when an item is listed as running on the Android OS, it is not even consuming resources. 通常,当某个项目被列为在Android OS上运行时,它甚至不会消耗资源。 If the system does need more resources, it will close the application down properly. 如果系统确实需要更多资源,它将正确关闭应用程序。 If an application is not closed properly, ie by a task killer it can have many bad side effects. 如果未正确关闭应用程序(即由任务杀手关闭),则可能会有许多不良影响。

Also, just because an app is "running" doesn't mean that it has a process running. 另外,仅因为应用程序正在“运行”并不意味着它正在运行一个进程。 If you look at the documentation for ActivityManager it says this. 如果您查看ActivityManager的文档,它会说明这一点。

Information you can retrieve about a particular task that is currently "running" in the system. 您可以检索的有关系统中当前正在“运行”的特定任务的信息。 Note that a running task does not mean the given task actually has a process it is actively running in; 请注意,正在运行的任务并不意味着给定任务实际上具有正在运行的进程。 it simply means that the user has gone to it and never closed it, but currently the system may have killed its process and is only holding on to its last state in order to restart it when the user returns 它只是意味着用户已经去过它并且从未关闭过它,但是当前系统可能已经终止了它的进程,并且仅保持其最后一个状态以便在用户返回时重新启动它。

killBackgroundProcesses() kills a process like the Android OS would (when it needs more memory), so it is likely that it is retaining its last state because it thinks that it was killed because the system needed more resources. killBackgroundProcesses()杀死进程,就像Android OS那样(当它需要更多内存时),因此它可能会保留其最后一个状态,因为它认为它已被杀死是因为系统需要更多资源。 Thus leading the system to believe that there is a chance the user may want to return to the application (why it keeps its last state). 因此,使系统相信用户可能会想返回到应用程序(为什么它保持其最后状态)。

Anyways, after you have read the above and still wish to write a task killer, you may find it beneficial to check out the open source task killer. 无论如何,在阅读了以上内容之后,仍然希望编写任务杀手,您可能会发现签出开源任务杀手很有益。 The code can be seen here: http://code.google.com/p/freetaskmanager/source/browse/ 可以在此处查看代码: http : //code.google.com/p/freetaskmanager/source/browse/

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

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