I am trying to close particular 3 application if already opened and running in the background apps. I have tried like this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CloseApps();
}
private void CloseApps() {
ActivityManager actvityManager = (ActivityManager)
this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo runningProInfo : procInfos) {
if (runningProInfo.processName.equals("com.aaa.aaaa")) {
actvityManager.killBackgroundProcesses(runningProInfo.processName);
}else if (runningProInfo.processName.equals("com.ccc.ccc")) {
actvityManager.killBackgroundProcesses(runningProInfo.processName);
}
else if (runningProInfo.processName.equals("com.bbb.bbb.ad")){
actvityManager.killBackgroundProcesses(runningProInfo.processName);
}
}
}
Note: I'm working on android 7.1 version.
I got solution for this question
private void CloseApps() {
Process suProcess = null;
try {
suProcess = Runtime.getRuntime().exec("su");
DataOutputStream dataOutputStream = new DataOutputStream(suProcess.getOutputStream());
dataOutputStream.writeBytes("am force-stop com.aaaa.aaaa" + "\n");
} catch (IOException e) {
e.printStackTrace();
}.