我正在創建一個顯示當前播放歌曲notification
的應用程序。如何取消意外/強制終止應用程序的通知
該歌曲正在播放通過Service
&啓動&取消通知是在服務本身完成的。
但是,如果應用程序被某種異常終止,或者如果我強制通過任務管理器關閉它,通知將保留在任務欄的頂部。
我該如何刪除它。
下面是代碼:
//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);
}
檢查此: http://stackoverflow.com/questions/12997800/cancel-notification-on-remove-application-from-multitask-panel – 2014-03-13 23:51:03