I am creating an application that displays a notification current executable song.
The song is played through the Service , and the initiation and cancellation of the notification is performed in the service itself.
But if the application is terminated by any exception or I force it to close through the task manager, the notification remains on the taskbar in the 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
source share