How to kill all running applications in android?
- You can use
Process.killProcess(int pid)
to kill processes that have the sameUID
with your App. - You can use
ActivityManager.killBackgroundProcesses(String packageName)
,withKILL_BACKGROUND_PROCESSES
permission in your manifest(for API >= 8) orActivityManager.restartPackage (String packageName)
(for API < 8) to kill specified process,except of forground process.
So if you would to kill all other processes when your program is foreground process,you would to use ActivityManager.killBackgroundProcesses
or ActivityManager.restartPackage
:
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
//get a list of installed apps.
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
String myPackage = getApplicationContext().getPackageName();
for (ApplicationInfo packageInfo : packages) {
if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
if(packageInfo.packageName.equals(myPackage)) continue;
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
In above snippet code,each process will be killed unless it be process of your App or system process.
References:
How to close all active applications from my Android app?
How do task killers work?
How to close all running app programmatically in android?
You can only kill your own apps, e.g. the ones running in the same process or with the same userID. You can not kill others, unless the device is rooted.
Have a look at this answer though, about 'killing' background processes of apps.
How can I kill processes in Android?
- The message you're getting indicates that your app has crashed. You need to look at its LogCat to find out why. This is documented in the Android Developer Guide.
- Please state your reason for killing all background processes, because I can't think of any appropriate reason to do this. People persist in claiming that "task killers" or "app killers" improve performance, but this attitude ignores the real problem: poorly-written apps. As long as we continue to claim that task killers help, users will continue to use apps that leave unnecessary services, etc. running. Forcing users to use task killers is like dealing with a termite problem by killing one termite at a time as you see them. The real answer is to exterminate all the termites.
In short, anyone who shows you how to kill all background processes is doing you a disservice and the Android community a disservice.
Kill all recent applications in android
First get a list of RunningTaskInfo
records using the following:
[public List<ActivityManager.RunningTaskInfo> getRunningTasks (int maxNum)][1]
Reference
After that, you can use the process-ids in that list to terminate the relevant processes
using either
activityManager.killBackgroundProcess(pid);
or
android.os.Process.killProcess(pid);
depending upon whether you can kill that process. Way to kill these is detailed here.
Exit Android App - Kill all processes
int p = android.os.Process.myPid();
android.os.Process.killProcess(p);
Related Topics
Error "Package Android.Support.V7.App Does Not Exist"
Android Device Monitor "Data" Folder Is Empty
Android Getting Exact Scroll Position in Listview
Sending Message to a Handler on a Dead Thread When Getting a Location from an Intentservice
How to Get Thumbnail for Video in My /Sdcard/Android/Data/Mypackage/Files Folder
Starting Activity Through Notification: Avoiding Duplicate Activities
Android Studio 3.1 Edittext Stackoverflowerror
Isvalidfragment Android API 19
Android: Tint Using Drawablecompat
How to Play .Mp4 Video in Videoview in Android
Recyclerview Smoothscroll to Position in The Center. Android
Android: How to Get a Modal Dialog or Similar Modal Behavior
How to Implement Ssl Certificate Pinning While Using React Native
Using Espresso to Unit Test Google Maps
How to Store Files Generated from App in "Downloads" Folder of Android
How to Launch Android Calendar Application Using Intent (Froyo)