11

I've read a lot of documentation and other posts about closing other apps. I know the documentation says Android isn't designed that way. Android wants to hibernate an app as opposed to killing the process. That makes sense but.... I wrote an app that automatically launches other apps based on system events that the user configures. Users love it and want to close/kill those apps after subsequent system events. What I am trying to say is that users are asking me to automate this for them. That is the point of my app. It automatically launches other apps and now they want to automatically close or kill them. A lot of people say that this is not possible but it is. There are several task killer apps out there and they do the very thing I would like to do. I tried the

Process.killProcess(pid); 

but that doesn't seem to do much. Reading the doco on that method it says the Linux Kernal will still enforce permissions. I even attempted to kill all processes and it didn't do anything. Perhaps I am not getting the list of processes correctly. I use the following to get a list of processes to kill

for(RunningAppProcessInfo info:activityManager.getRunningAppProcesses()){

Then I use Process.killProcess(info.pid); to kill the process. I even tried:

activityManager.killBackgroundProcesses(String.valueOf(info.pid));
activityManager.killBackgroundProcesses(String.valueOf(info.processName));

and then

Intent i = new Intent();
i.setComponent(new ComponentName(ProcessName,   ProcessName+".Main"));
context.stopService(i);

Can anyone actually answer this question and not tell me it shouldn't be done or ask why I want to do it in the first place? Please provide a code sample or something meaningful.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
Luke
  • 331
  • 2
  • 5
  • 11

1 Answers1

4

What you said is perfectly possible. Just get the ActivityManager system service and then use .restartPackage() (deprecated) or .killBackgroundProcesses().

ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
activityManager.restartPackage(packageName);

You will need:

<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
alderz
  • 66
  • 1
  • 1
    Looking at the code I posted in the question you can see that I am in fact using activityManager.killBackgroundProcesses(). I have also used .restartPackage() but that didn't work either. I do have both of those permissions as well. I don't get an error when I attempt to kill it just doesn't kill the app. I am launching the calculator app with an intent and I can see it running as a process but when I kill it it stays open as if I haven't done a thing. Even if I try to kill all processes it will only kill my app process. It is as if I don't have the OS level permission to kill other apps. – Luke Sep 06 '11 at 17:01
  • 4
    OK, I finally got this to work! Here is the catch: I had the calculator visible and Android will not remove the active view even if you kill the process. If I show the desktop first then I can kill the app. The perfect test was Pandora. With this app you can listen to music even if it isn't visible. When I kill it while it is visible nothing seems to happen. If I show the desktop first and then kill it the music stops. – Luke Sep 06 '11 at 18:00
  • @luke: I followed your path step-by-step. +1 for not giving up. Could you please tell me what do you mean showing desktop? – Behnam Dec 01 '13 at 07:50
  • @Luke: After all the support you got from community, it's fair to contribute a little more. – Behnam Jan 02 '14 at 06:25
  • 2
    @Campiador Use the following to go to home: `Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); this.startActivity(startMain);` This will simulate pressing the home button, effectively hiding all your apps but still running – Pierre Jan 17 '14 at 18:34
  • Thanks Pierre :) do you have any idea how to remove apps from the "Recent Applications" list. (the list that pops up after prssing the soft button) – Behnam Jan 18 '14 at 05:16