7

I'm creating an application which displays the notification of current playing song.

The song is being played via Service & the initiation & cancellation of notification is done in the service itself.

But If the app is terminated by some exception or if I force close it via task manager, the notification remains on the top on taskbar.

How can I remove this.

Below is the code:

//In Service onStartCommand(), there is a call to initiatePlayback()
private void initiatePlayback() {
    try {
        if (mPlayer.isPlaying()) {
            mPlayer.stop();
        }
        mPlayer.reset();
        mPlayer.setDataSource(currentData);
        mPlayer.prepare();

        if (reqAudioFocus()) {
            mPlayer.start();
        }

        if (mPlayer.isPlaying()) {
            initNotification();
        }
    } catch (Exception e) {
        Toast.makeText(this,
                "PlayTrack->initiatePlayback(): " + e.toString(),
                Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onDestroy() {
    stopPlayback();
    mPlayer.release();
    mPlayer = null;
    super.onDestroy();
}

private void stopPlayback() {
    if (mPlayer.isPlaying()) {
        mPlayer.stop();
        mPlayer.reset();
        cancelNotification();
    }
}
private void cancelNotification() {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        mNotificationManager.cancel(NOTIFICATION_ID);
    }
reiley
  • 3,759
  • 12
  • 58
  • 114
  • 2
    check this: http://stackoverflow.com/questions/12997800/cancel-notification-on-remove-application-from-multitask-panel – Beshoy Fayez Mar 13 '14 at 23:51

4 Answers4

1

Catch the exception and in the catch clause cancel it:

mNotificationManager.cancel(MY_NOTIFICATION_ID);
Andy Res
  • 15,963
  • 5
  • 60
  • 96
1

I know this question is old, but I found it when I believed to be having a similar problem. Turned out for me, the service was being killed along with the app (which correctly cancelled the notification in onDestroy), but then was being restarted (as it should with START_STICKY) and the notification being posted again. In these cases I did not want the service to come back until the user initiated it from the app, so in OnStartCommand I called stopSelf() when the incoming intent was null.

user888867
  • 540
  • 5
  • 16
0

You can override onDestroy() method and use the NotificationManager to cancel your notification. You only need to provide your ID of the notification that should be canceled.

@Override
public void onDestroy() {

    private static final int MY_NOTIFICATION_ID= 1234;
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager;
    mNotificationManager = (NotificationManager) getSystemService(ns);
    mNotificationManager.notify(MY_NOTIFICATION_ID, notification);
    //to cancel:
    mNotificationManager.cancel(MY_NOTIFICATION_ID);
}
Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
  • sorry..didn't loaded the entire relavant code..doing this only..code added – reiley Aug 13 '12 at 08:05
  • @raul8 My solution proposal should still work for your case since your application would go into onDestroy state when it is terminated and you can simply cancel the notification with the code sample I shared. Have you tried doing so? – Korhan Ozturk Aug 13 '12 at 08:14
  • 3
    yes..the problem is OnDestroy is not even called. I'm using 'System' task manager. The thing is other players do not even get closed & I think that is a correct functionality. So now I'll try not to stop the playback on force close via task manager. – reiley Aug 13 '12 at 10:45
  • 2
    This cannot solve the problem. App is not aware of it is being killed(force-close). Activity's callback will never get called when force-closing or killing app. – alexhilton Apr 14 '14 at 06:23
0

You can catch the errors that break your application with:

Thread.setDefaultUncaughtExceptionHandler(newHandler);

Note that doing this will unable your application to close after an exception, so you need to get the old exceptionHandler reference and call it from yours.

Also, add the code to remove notification on this method.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167