1

I am developing an app for radio streaming using exoplayer library. So, I want to clear(delete) exoplayer notification bar (with play/stop buttons) when I close the app(from recent apps e.t.c). Now, I close the app but the notification bar still appears and doesn't close with swipe left/right.

I tried the following source code but it doesn't work (i tried to run the lines of the source code separately from the methods but I can't solve the problem).

public class RadioService extends Service {
......
.......
......
public void onTaskRemoved(Intent rootIntent) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(NOTIFICATION_ID);
        stopForeground(true);
        notificationManager.cancelAll();

    }

    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(NOTIFICATION_ID);

    }
}

I declared it in AndroidManifest.xml too.

Any help please?

2 Answers2

0

Check this answer How to kill a foreground service along with the application and an example https://androidwave.com/foreground-service-android-example/

at android 8 and above you have to use foreground service.

padPad
  • 121
  • 2
  • 5
0

Add a small delay after the clear of notification and before killing the app or create a function that will inform you the notification is cleared

CDrosos
  • 2,418
  • 4
  • 26
  • 49