title: How to remove applications run in background?
body: <p>I have an application which lists all installed applications and launch them on item click.Now I updated it with blocking some applications like Settings, android market place etc...For this I used a service which stops only when exiting from my application.This service continuously checke for currently running applications.If it is a blocked one then show a new activity like this.</p>
  
  <pre><code>private void currentlyRunningApps() {
      List&lt;RunningTaskInfo&gt; runningTasks = ((ActivityManager) getApplicationContext() .getSystemService("activity")).getRunningTasks(1);
      if (runningTasks != null &amp;&amp; !runningTasks.isEmpty()) {
          for (int i = 0; i &lt; runningTasks.size(); i++) {
              RunningTaskInfo runningtaskinfo = (RunningTaskInfo) runningTasks .get(i);
              String packageName = runningtaskinfo.topActivity.getPackageName();
              if(checkBlackListedTask(runningtaskinfo)) {
  
                  handler.post(new Runnable() {
                      @Override
                      public void run() {
                       // show action not allowed activity 
                      }
                  });
                  break;
  
              }
          }
      }
  }
  </code></pre>
  
  <p>This works fine.But  when exiting from my application the blocked applications stayed in background.I tried <code>killBackgroundProcesses</code> and <code>restartPackage</code> method.But they won't work.</p>
  
  <pre><code> Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);     
               forceStopPackage.setAccessible(true);     
               forceStopPackage.invoke(am,package name);
  </code></pre>
  
  <p>This throws null pointer exception </p>
  
  <p>How can i close these blocked application before showing <code>'action not allowed'</code> activity ?</p>
  
  <p>Is there any way to fix this issue?</p>
  
  <p>Thanks in advance</p>