Android kill background application from our application programmatically

前端 未结 3 681
日久生厌
日久生厌 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<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();
         }
    
    0 讨论(0)
  • 2021-02-06 19:40

    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.

    0 讨论(0)
  • 2021-02-06 19:44

    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

    0 讨论(0)
提交回复
热议问题