2

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 application programmatically.

I refer this post but I am not able to understand how to implement this. Actually I want to develop somthing like ShutApp. In this, application force-closes app other background running application.

I am stuck into developing this feature. Any hint/suggestion would be helpful.

Thanks in advance.

EDIT

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
Community
  • 1
  • 1
anddev
  • 3,144
  • 12
  • 39
  • 70

3 Answers3

3

This will force-stop all applications including system-app

    try
     {
        Process suProcess = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

        os.writeBytes("adb shell" + "\n");
        os.flush();

        Context newContext=this;
        ActivityManager activityManager = (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();
     }
Eldos Das
  • 31
  • 4
0

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.

Community
  • 1
  • 1
Black Magic
  • 2,706
  • 5
  • 35
  • 58
  • I know this would be bad practice but I have requirement for the same. And also all battery saver application is following same procedure. I just want to know how can we develop this. – anddev Mar 09 '15 at 09:36
  • See my edit, please use google to find your answers before you ask it here. – Black Magic Mar 09 '15 at 10:31
  • 1
    Thanks for your quick response but I already tried this and its not working. Please be sure before suggesting the answer :) – anddev Mar 09 '15 at 10:48
  • Did you add the permission to the Manifest? – Black Magic Mar 09 '15 at 11:00
  • Yes I added all required permissions. – anddev Mar 09 '15 at 12:12
  • See my edited question. I added all those permission into my Manifest.xml. Here I dont want to kill my current working app. But through my current app I want to kill all other background running apps. – anddev Mar 09 '15 at 12:15
  • The answer referred here doesn't work! I hope the author must have tried this before referring it. – Gaurav Singla Jun 02 '20 at 05:50
0

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