13

How can I force stop an app with Java? I'm trying to build a memory cleaner that can help clean out background processes.

I know there is a way to kill the process of an app, but when you go to the running list, the app is still there even after you have killed it. And I have tried a lot of similar memory cleaning apps, only one of them can totally force stop apps but it has so many useless notifications - very annoying.

P.S.: When you go to Settings -> Apps, you will see a list of apps. Click on one of these apps and you end up on the app's info. There is a button named "force stop". By clicking on it, the app is killed. I want to perform that kind of action in my app. How can this be done?

force stop example

Edric
  • 24,639
  • 13
  • 81
  • 91
Mitte
  • 111
  • 1
  • 2
  • 12
  • 1
    Your question is lacking enough details. – Hawk Apr 27 '15 at 04:56
  • I tried to attach a screen shot to help explain but it told me that I don't have enough reputation to attach pictures :( – Mitte Apr 27 '15 at 04:59
  • possible duplicate of [How to kill an application with all its activities?](http://stackoverflow.com/questions/3105673/how-to-kill-an-application-with-all-its-activities) – Kishore Apr 27 '15 at 05:08
  • possible duplicate of [Quitting an application - is that frowned upon?](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon) – Gemtastic Apr 27 '15 at 05:11
  • @Kishore not really, that's about killing your own app, not a system app. – k_g Apr 27 '15 at 05:11
  • Yes, I want to kill not only my own app, but also other apps. – Mitte Apr 27 '15 at 05:14
  • AFAIK, to kill a system app, the killer has to be a system app ,i.e., the killer process needs to have root or superuser permissions. – Kishore Apr 27 '15 at 05:15
  • Can I kill other user apps? – Mitte Apr 27 '15 at 05:17
  • You can check out ActivityManager and lowmemorykiller code in the android framework. These actually do the job of killing and swapping out processes. – Kishore Apr 27 '15 at 05:23

2 Answers2

11

get the process ID of your application, and kill that process onDestroy() method

@Override
public void onDestroy()
 {
    super.onDestroy();

    int id= android.os.Process.myPid();
    android.os.Process.killProcess(id);
 }

or

getActivity().finish();
System.exit(0);

and if you want to kill other apps from your activity, then this should work

You can send the signal using:

Process.sendSignal(pid, Process.SIGNAL_KILL);

To completely kill the process, it's recommended to call:

ActivityManager.killBackgroundProcesses(packageNameToKill)

before sending the signal.

Please, note that your app needs to own the KILL_BACKGROUND_PROCESSES permission. Thus, in the AndroidManifest.xml, you need to include:

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
Edric
  • 24,639
  • 13
  • 81
  • 91
Jeffy Lazar
  • 1,903
  • 13
  • 20
  • 1
    Can I kill other apps in this way? Can you show more snippets? Thanks! – Mitte Apr 27 '15 at 05:16
  • This will kill your app, not other apps. – Kishore Apr 27 '15 at 05:16
  • from Android M , you cannot stop other app processes .it is completely up to Android os(unless you have root access). – Mehrdad Feb 24 '17 at 22:05
  • Note that calling `#finish()` will have almost no effect if you call `System.exit(0)` one line after that call. So if you really want to force-exit, wait somewhere else for the `onDestroy()` callback (like.. listen for lifecycle events in the application class). But also note that all of these kill methods are very aggressive and the `Process` calls will both restart the service if you marked it as `STICKY` at service creation. – milosmns Mar 17 '17 at 18:39
  • As far as I can tell, if this takes place when there is more than one activity on the task stack, Android will restart the application, and bring up the next lower activity - even with all of these steps. In other words, it will, from a task stack standpoint, act as if you just did a finish() on the current activity. – John Moore Feb 01 '19 at 22:02
2

Try to use following code

    finish(); // for stopping Current Activity

    // add this line for Removing Force Close

    @Override
        protected void onDestroy() {
    // closing Entire Application
            android.os.Process.killProcess(android.os.Process.myPid());
            super.onDestroy();
        }
    }

May be this solution will help you Force Close an app programmatically

Community
  • 1
  • 1
Santosh Shinde
  • 6,045
  • 7
  • 44
  • 68