Android kill background application from our application programmatically

12,897

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();
     }
Share:
12,897
anddev
Author by

anddev

Updated on June 14, 2022

Comments

  • anddev
    anddev almost 2 years

    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"/>