0

I am beginner in android. When i click a Button KILL selected apps to close or kill a Particular(Running) Application or Selected Application. I Know it is possible. But how to work out it. My Sample UI is
http://answers.oreilly.com/index.php?app=core&module=attach&section=attach&attach_rel_module=post&attach_id=555
I am try to use some code. But all the codes can be used to close only for activities. Not Closed Application. May i tried to refer some stackoverflow answers. But i am not clearly understand anything. My code is

finish();
System.exit(0);
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
android.os.Process.killProcess(android.os.Process.myPid());

Please reply your Answers and comments are valuable me. Thanks.

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
Bala
  • 445
  • 5
  • 11
  • 26
  • I dont know Android programming, but could it be that System.exit(0) closes your process that should kill the others before killing them? – Corsair Jun 12 '12 at 06:25
  • 1
    It is not Android way to close an application. Android applications run at the background all the time. Application's allocated resources are deleted by OS when other applications need space for their resources. But activities mostly keep waiting at the background. – Jan Jun 12 '12 at 06:28
  • Thanks. But any other idea to close my application or other applications – Bala Jun 12 '12 at 07:01

2 Answers2

1

I Know it is possible.

Not at your will though.

android.os.Process.killProcess(android.os.Process.myPid());

A note from the documentation about this method:

Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.

About system.exit(0) , read this answer. And keep that in your mind.

And about finish(), the documentation clearly says the following:

"Call this when your activity is done and should be closed."

To sum it all up - don't try to make a task killer. The android OS takes care of that perfectly well. Don't meddle with it.

The wheel has been invented and is being used perfectly, don't reinvent it.

Please read this. You need to understand that Android doesn't require task killers. Google it and you will find many such articles.

Community
  • 1
  • 1
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • Ya. I already worked this code. It's close only activities. After may i check the task manager my application is running into the background. – Bala Jun 12 '12 at 06:27
  • "You cannot kill an application at your will". – Kazekage Gaara Jun 12 '12 at 06:33
  • Thanks. But currently i am running lot of applications. In that all applications are stored into the task manager. I closed some applications after i see the task manager that application is always running in the task manager or background. – Bala Jun 12 '12 at 06:58
  • It will keep running in the background unless the OS decides to completely kill it. Don't worry about managing resources on your own, let the OS do it. Run as many applications as you want. – Kazekage Gaara Jun 12 '12 at 07:05
  • I don't understand your question. Please rephrase it. – Kazekage Gaara Jun 12 '12 at 07:15
  • Thanks. Only one Doubt. I closed all the application. After i can see the task manager, it displays the recently closed applications. I click one application in the task manager it is running. – Bala Jun 12 '12 at 07:21
  • That is what I'm trying to tell you. Your closing an application doesn't kill it. It runs in the background. The OS will stop it whenever it feels that the resources occupied by it are required somewhere else. You don't have to worry about it. – Kazekage Gaara Jun 12 '12 at 07:23
0

Use restart application instead of killProcess

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage("YOUR_PACKAGE_NAME");

restartPackage is the wrapper of killProcess method.

use below permission to use restartPackage method

<uses-permission android:name="android.permission.RESTART_PACKAGES" /> 
Vivek Kumar Srivastava
  • 2,158
  • 1
  • 16
  • 23