How can I programmatically force stop an Android app with Java?
Solution 1
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" />
Solution 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
Related videos on Youtube
Comments
-
Mitte about 4 years
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?
-
Hawk almost 9 yearsYour question is lacking enough details.
-
-
Mitte almost 9 yearsCan I kill other apps in this way? Can you show more snippets? Thanks!
-
Mehrdad1373 about 7 yearsfrom Android M , you cannot stop other app processes .it is completely up to Android os(unless you have root access).
-
milosmns about 7 yearsNote that calling
#finish()
will have almost no effect if you callSystem.exit(0)
one line after that call. So if you really want to force-exit, wait somewhere else for theonDestroy()
callback (like.. listen for lifecycle events in the application class). But also note that all of these kill methods are very aggressive and theProcess
calls will both restart the service if you marked it asSTICKY
at service creation.