5

I have some code that will launch a different application using intents but what can I do to close or kill the other app?

Here is the launch code (works great):

Intent i = new Intent();
  i.setAction(Intent.ACTION_MAIN);
  i.addCategory(Intent.CATEGORY_LAUNCHER);
  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  i.setComponent(
     new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName, 
       resolveInfo.activityInfo.name));

I tried to kill the background processes but no luck:

ActivityManager activityManager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
  activityManager.killBackgroundProcesses("com.pandora.android");

I also tried this to kill it:

context.stopService(new Intent(context, Class.forName("com.bla.bla")));

Update:

I want to kill other applications because I launch other applications and users have requested this additional feature (auto kill is a natural extension of auto launch). Please answer the question of how to accomplish this in code. I know about Advanced Task Mgr so it is possible but how?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Luke
  • 331
  • 2
  • 5
  • 11
  • 2
    Android doesn't work like a computer, but here are still legitimate reasons to kill processes. That's the whole reason Task Killer exists. – AaronM Dec 21 '10 at 19:53
  • 3
    What are you trying to accomplish? There is probably a more user friendly way than killing other apps. – Computerish Dec 21 '10 at 20:10
  • 1
    Just out of curiosity, why do you want to kill other applications? – Chris Fei Dec 21 '10 at 21:33
  • @cfei: There are task manager apps out there (like Advanced Task Mgr) whose main purpose is to kill running apps. Seems like a must app for a developer. Better question might be: Why do you need to write your own? – Paul Sasik Dec 21 '10 at 21:34
  • 3
    possible duplicate of [Android-Close Other Apps](http://stackoverflow.com/questions/4503277/android-close-other-apps) – CommonsWare Dec 21 '10 at 21:35
  • 1
    I want to kill other applications because I launch other applications and users have requested this additional feature. Please answer the question of how to accomplish this in code. I know about Advanced Task Mgr so it is possible but how? – Luke Dec 21 '10 at 22:17
  • 1
    I just saw that you already asked this question. Don't post the same question multiple times. – hackbod Dec 22 '10 at 01:59
  • Yea, sorry about that this site doesn't recognize me as the same user for some reason. I used the same name and email to post it though. I reposted and now it recognizes me. – Luke Dec 22 '10 at 03:47
  • Possible duplicate of [Automate closing of applications in Android](https://stackoverflow.com/questions/7195167/automate-closing-of-applications-in-android) – Cristik Feb 14 '19 at 08:27

5 Answers5

4

I ended up finding the real answer to this question. Please see Automate closing of applications in Android for the solution!

Community
  • 1
  • 1
Luke
  • 331
  • 2
  • 5
  • 11
3

but what can I do to close or kill the other app?

You don't. Your user can exit that "other app" when they wish to.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
3

Basic answer, you can't. As of Froyo, the only facility available is ActivityManager.killBackgroundProcess(), which:

(a) Only allows you to kill processes that are, as it said, in the background and not visible to the user. That is, it does not allow you to disrupt the normal execution of the other app.

(b) Is not needed anyway, because Android will kill the background processes for you if it needs their memory.

You simply do not need to do what you at this point seem to think you do. "Because my users have asked for it" is not a reason to be going around killing other people's processes.

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • 2
    I have Froyo installed on my phone and the application Task Killer. Task Killer does infact have the ability to kill other applications and processes. This means that what you said is not true. – Luke Dec 22 '10 at 03:42
  • 2
    I wrote an application to launch other applications when certain system events occur. For example if you plug in your headphones you can configure Pandora to launch automatically using this application that I have written. The users want my application to now kill Pandora (or any app that has been configured) when the headphones are unplugged. Sure the users can do this themselves but that defeats the purpose of this handy auto launch/kill app. – Luke Dec 22 '10 at 03:45
2

The only proper way to kill another "App" would be to send some kind of Intent to the app (ex. Pandora) or it's service that will ask it to exit.

Task killers use facilities outside of the normal means of Android like Process.killProcess() that talks to the Linux Kernel and not Android. The problem with that approach is it does not tell Android to close the program, but instead the Linux Kernel directly. If a background service is killed, for example, without calling Context.stopService(), Android may restart it later automatically. Also, killProcess doesn't just close the Activity, it kills all the components and doesn't allow them to shutdown normally.

If you started Pandora with Activity.startActivityForResult() you could try closing it with Activity.finishActivity(), but there's no guarantee that it will work the way you want it to.

Exikle
  • 1,155
  • 2
  • 18
  • 42
penguin359
  • 1,289
  • 13
  • 26
1

You can use Process.killProcess()? you need to know the PID. To get the PID, you can exec "ps", but that's not going to be robust as the text output format could be different across devices or releases. You could provide your own native library to gather the process names and IDs into a standard format for your own purpose.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • That is a really good idea but it didn't quite do the job. I was able to use ActivityManager to get a list of running processes but when I use Process.killProcess(pid) it does nothing. Reading the doco it says you can only kill your own processes. Thanks for actually trying to solve this problem though! – Luke Dec 22 '10 at 01:40