0

I have an app that play some media and media can be played or paused from notification bar.now I want to close notification when user remove app from his phone's app list by swiping.I know that the activity onDestroy() method does not call.

here is some part of my code:

public class MyNotification extends Notification {

    private Context ctx;
    private NotificationManager mNotificationManager;
    RemoteViews mContentView;

    @SuppressLint("NewApi")
    public MyNotification(Context ctx) {
        super();
        this.ctx = ctx;
        String ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
        CharSequence tickerText = G.lastPlayingBookMaster.getName();
        long when = System.currentTimeMillis();
        Notification.Builder builder = new Notification.Builder(ctx);
        @SuppressWarnings("deprecation")
        Notification notification = this;
        notification.when = when;
        notification.tickerText = tickerText;
        if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            notification.icon = R.drawable.icon_notification_white;
        }else{
            notification.icon = R.drawable.icon_launcher;
        }

        mNotificationManager.notify(548853, notification);
    }
}

And in my main activity I have this method:

 public void showNotification() {
    myNotification = new MyNotification(this);
}

Now what can I do???? tnx

saleh sereshki
  • 2,402
  • 3
  • 17
  • 18
  • Try with onStop() method of activity to remove notification using mNotificationManager.cancel(548853); – Hitesh Bhalala Dec 16 '15 at 08:54
  • Possible duplicate of [Cancel notification on remove application from multitask panel](http://stackoverflow.com/questions/12997800/cancel-notification-on-remove-application-from-multitask-panel) – yennsarah Dec 16 '15 at 08:56
  • I saw that and I tried to do but it did not work for me – saleh sereshki Dec 16 '15 at 09:29

0 Answers0