0

我很难清除状态栏通知。

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;

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

4

1 回答 1

2

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

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

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

NotificationManager 文档

于 2012-01-12T19:38:39.693 回答