Android Kill Background Application From Our Application Programmatically
My requirement forces me to kill another apps from my developed application. Details: My requirement is how to kill all background running application from currently developing ap
Solution 1:
This will force-stop all applications including system-app
try
{
ProcesssuProcess= Runtime.getRuntime().exec("su");
DataOutputStreamos=newDataOutputStream(suProcess.getOutputStream());
os.writeBytes("adb shell" + "\n");
os.flush();
Context newContext=this;
ActivityManageractivityManager= (ActivityManager) newContext.getSystemService( Context.ACTIVITY_SERVICE );
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for(RunningAppProcessInfo appProcess : appProcesses){
if(appProcess.processName.equals("com.yourPackageName")){
}
else{
os.writeBytes("am force-stop "+appProcess.processName + "\n");
}
}
os.flush();
os.close();
suProcess.waitFor();
}
catch (IOException ex)
{
ex.getMessage();
Toast.makeText(getApplicationContext(), ex.getMessage(),Toast.LENGTH_LONG).show();
}
catch (SecurityException ex)
{
Toast.makeText(getApplicationContext(), "Can't get root access2",
Toast.LENGTH_LONG).show();
}
catch (Exception ex)
{
Toast.makeText(getApplicationContext(), "Can't get root access3",
Toast.LENGTH_LONG).show();
}
Solution 2:
I am pretty sure this is not possible, it would be bad practice for Android to allow apps to close other apps.
The answer is in this post: Kill another process/application programmatically
Please google your question before asking it here.
Solution 3:
We cannot actually kill all the background apps . However, we can kill our own app from recents by calling finishAndRemoveTask() which is available from lollipop.
Another Solution is we can use android:excludeFromRecents
in activity in the manifest file
Post a Comment for "Android Kill Background Application From Our Application Programmatically"