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). 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!

Upvotes: 0

Views: 7185

Answers (1)

user631063
user631063

Reputation:

Killing apps in Android is usually considered a bad idea. This is because the Android OS is designed to handle all Task Management. Usually when an item is listed as running on the Android OS, it is not even consuming resources. 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.

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. 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/

Upvotes: 4

Related Questions