清除状态通知Android

时间:2012-01-12 19:26:59

标签: android android-notification-bar

我无法清除状态栏通知。

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;

当我尝试取消服务,因此清除状态栏时,应用程序崩溃。

1 个答案:

答案 0 :(得分:2)

您的通知对象可能为null,这会导致NullPointerException

我认为您应该使用NotificationManager对象取消通知,使用相同的ID(在您的情况下为0)并将其传递给NoticationManager.cancel(Int)

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(0);

NotificationManager docs