我清除不了一個狀態欄通知。
public void NotificationStatus(){
Intent intent = new Intent(this, Stimulation.class);
NotificationManager notificationManager
= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(
david.flanagan.android.VenAssist.Activities.R.drawable.notification_icon,
"VenAssist Stimulation ON", System.currentTimeMillis());
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(this, "VenAssist",
"Stimulation Started", activity);
notificationManager.notify(0, notification);
}
然後又試圖在服務器中的onDestroy()
函數中調用notification.cancel(0);
清除通知狀態。
我創建了一個通知的實例,但是不確定是否正確。
private NotificationManager notification;
當我取消了服務,想清除通知狀態時,應用程序就崩潰了。
請求大家的幫助,謝謝!
正如 cytown 所說。notification可能為空,才會導致 NullPointerException。
我認為你應該使用一個NotificationManager對象來取消一個notification。使用相同的ID,你的例子中是0,傳遞到NoticationManager.cancel(Int)中:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(0);