title: Java Android - close application (notifications) after swiping in recents
body: <p>How can I close application (or just remove all notifications) after swiping in recent apps? In API level &lt;= 22, it's working automaticly (just force quit the application) But API level 23 - marshmallow+ application after swiping is not closed and notification is also not closed.</p>
  
  <p>How i can kill the notification or application after swiping off in recnet apps?</p>
  
  <p>I'm showing notification onPause and removing it on onResume... but when I swipe the application in recent apps (application is closed - after re-opening the app have new initialization) but notification is still here and I need to kill this notifi.</p>
  
  <p>Thanks for your answers guys.</p>
  
  <p>BTW: I try the Service (onTaskRemoved()) but this function is never be called and I also do not know why..</p>
  
  <pre><code>import android.app.Service;
  import android.content.Intent;
  import android.os.IBinder;
  import android.util.Log;
  
  public class MyService extends Service {
  
      private static final String TAG = "VYKAL";
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public void onCreate() {
          super.onCreate();
          Log.i(TAG, "onCreate()");
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          Log.i(TAG, "onStartCommand()");
          return START_NOT_STICKY;
      }
  
      @Override
      public void onTaskRemoved(Intent rootIntent) {
          super.onTaskRemoved(rootIntent);
          Log.i(TAG, "onTaskRemoved()");
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          Log.i(TAG, "onDestroy()");
      }
  
      @Override
      public void onLowMemory() {
          super.onLowMemory();
          Log.i(TAG, "onLowMemory()");
      }
  }
  
  
  super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Intent serviceIntent = new Intent(MainActivity.this, MyService.class);
          startService(serviceIntent);
  </code></pre>
  
  <p>EDIT: notification</p>
  
  <pre><code>Intent notificationIntent = new Intent(compat, MainActivity.class);
  
          notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                  | Intent.FLAG_ACTIVITY_SINGLE_TOP);
          PendingIntent intent = PendingIntent.getActivity(compat, 0,
                  notificationIntent, 0);
  
          mBuilder = new NotificationCompat.Builder(compat)
                  .setSmallIcon(getSmallIcon(laststate))
                  .setContentText(lastcontent)
                  .setContentTitle(compat.getString(R.string.APPLICATION_NAME))
                  .setContentIntent(intent).setOngoing(true);
  
          RemoteViews rv = new RemoteViews(compat.getPackageName(), R.layout.notification_layout);
          rv.setImageViewResource(R.id.imagenotileft, getLargeIconSRC(laststate));
          rv.setTextViewText(R.id.title, compat.getString(R.string.APPLICATION_NAME));
          rv.setTextViewText(R.id.text, lastcontent);
          mBuilder.setContent(rv);
  
          NotificationManager manager = (NotificationManager) compat.getSystemService(NOTIFICATION_SERVICE);
          manager.notify(0, mBuilder.build());
  </code></pre>