我正在创建一个显示当前播放歌曲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