I have a my Android app, and I would like to close all other active applications from my app. Is there a way to do it in android?
Asked
Active
Viewed 4,693 times
3 Answers
3
You can do something like this,but it is not recommended:
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);
for (ApplicationInfo packageInfo : packages) {
if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
if(packageInfo.packageName.equals("mypackage")) continue;
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}

hasanghaforian
- 13,858
- 11
- 76
- 167

Rasel
- 15,499
- 6
- 40
- 50
-
How to kill foreground apps if we are running this from a background service? – zeitgeist Jun 08 '22 at 06:30
0
You can do like this:
int p = android.os.Process.myPid();
android.os.Process.killProcess(p);

Pratik Butani
- 60,504
- 58
- 273
- 437
-
,I am working on the same and If You have working code then share..Because whatever you are saying is not working. – Custadian Apr 24 '13 at 12:46
-
-
The app is closed by not removed from overview button recents screen. Also, will this work for other apps? – zeitgeist Mar 02 '22 at 10:45
0
To avoid to killing system apps you can do this,i include code for particlur brand like Huawei have own apps (which often contain Huawei) and samsung have own so this code avoid to kill system apps and will close all other apps.System apps often contain android and adil is my package which i dont wanna kill
ActivityManager actvityManager = (ActivityManager)
getApplicationContext().getSystemService( getApplicationContext().ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for(int pnum = 0; pnum < procInfos.size(); pnum++)
{
if((procInfos.get(pnum)).processName.contains("android")||(procInfos.get(pnum)).processName.contains("system")||(procInfos.get(pnum)).processName.contains("huawei")||(procInfos.get(pnum)).processName.contains("adil"))
{
//Toast.makeText(getApplicationContext(), "system apps", Toast.LENGTH_SHORT).show();
}
else
{
actvityManager.killBackgroundProcesses(procInfos.get(pnum).processName);
Toast.makeText(getApplicationContext(), "killed "+procInfos.get(pnum).processName, Toast.LENGTH_SHORT).show();
}
}

Adiii
- 54,482
- 7
- 145
- 148