Android kill background application from our application programmatically

孤者浪人 提交于 2019-12-03 09:11:01

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!