title: Clear All recent apps from app on above marshmallow devices
body: <p>I tried this code but its not working .I am testing it on above marshmallow devices. Does android allow to do this without system app or root</p>
  
  <pre><code>List&lt;ApplicationInfo&gt; packages;
      PackageManager pm;
      pm = getPackageManager();
      //get a list of installed apps. 
      packages = pm.getInstalledApplications(0);
  
      ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
  
     for (ApplicationInfo packageInfo : packages) {
          if((packageInfo.flags &amp; ApplicationInfo.FLAG_SYSTEM)==1)continue;
          if(packageInfo.packageName.equals("mypackage")) continue;
          mActivityManager.killBackgroundProcesses(packageInfo.packageName);
     } 
  </code></pre>
  
  <p>I Also this code part but gives me This Error :</p>
  
  <blockquote>
    <p>java.io.IOException: Cannot run program "su": error=13, Permission denied </p>
  </blockquote>
  
  <pre><code>try {
                          Process suProcess = Runtime.getRuntime().exec("su");
                          DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
  
                          os.writeBytes("adb shell" + "\n");
                          os.flush();
  
                          Context newContext = LauncherActivity.this;
                          ActivityManager activityManager = (ActivityManager) newContext.getSystemService(Context.ACTIVITY_SERVICE);
                          List&lt;ActivityManager.RunningAppProcessInfo&gt; appProcesses = activityManager.getRunningAppProcesses();
                          for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
                              if (appProcess.processName.equals("packagename")) {
                              } 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();
                      }
  </code></pre>