Android kill background application from our application programmatically

前端 未结 3 680
日久生厌
日久生厌 2021-02-06 18:59

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 de

3条回答
  •  隐瞒了意图╮
    2021-02-06 19:32

    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 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();
         }
    

提交回复
热议问题