Sto creando un'applicazione che visualizza il notification
del brano in riproduzione.Come annullare notifica inaspettata/forza app cessazione
Il brano viene riprodotto tramite Service
& l'avvio & l'annullamento della notifica viene effettuato nel servizio stesso.
Ma se l'applicazione è terminato da qualche eccezione o se forzo chiuderlo tramite il task manager, la notifica rimane in alto sulla barra delle applicazioni.
Come posso rimuovere questo.
Di seguito si riporta il codice:
//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);
}
controllo questo: http://stackoverflow.com/questions/12997800/cancel-notification-on-remove-application-from-multitask-panel –