Android Close Other applications

I have code that will launch another application using intentions, but what can I do to shut down or kill another application?

Here is the startup code (works fine):

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 background processes, but no luck:

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

I also tried this to kill him:

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

Update:

I want to kill other applications, because I launch other applications, and users requested this additional function (automatic killing is a natural continuation of automatic launch). Answer the question of how to do this in code. I know about Advanced Task Mgr, so it's possible, but how?

+4
source share
5 answers

but what can I do to shut down or kill another application?

No. Your user can exit this "other application" whenever he wants.

+4
source

In the end, I found a real answer to this question. Please see Automatically close Android apps for solution!

+4
source

The main answer is you cannot. With Froyo, the only available tool is ActivityManager.killBackgroundProcess (), which:

(a) Allows you to kill processes that are said to be in the background and not displayed to the user. That is, it does not allow you to disrupt the normal execution of another application.

(b) In any case, it is not required, since Android will kill background processes for you if it needs their memory.

You just do not need to do what you currently think, think. "Because my users asked for it," this is not a reason to circumvent other people's processes.

+3
source

can you use Process.killProcess ()? you need to know the PID. to get the PID, you can run "ps", but it will not be reliable, as the text output format may differ on different devices or releases. you can provide your own library for collecting process names and identifiers in a standard format for your own purpose.

+2
source

The only right way to kill another β€œapplication” is to send some intention to the application (for example, Pandora) or its service, which will ask him to exit.

Task killers use tools outside the usual Android tools, such as Process.killProcess() , which speak to the Linux kernel, not Android. The problem with this approach is that it does not tell Android to close the program, but instead directly to the Linux kernel. If the background service is killed, for example, without calling Context.stopService() , Android may restart it later automatically. In addition, killProcess does not just close the Activity, it kills all components and does not allow them to work normally.

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

+2
source

Source: https://habr.com/ru/post/1332947/


All Articles