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);

通知マネージャーのドキュメント

于 2012-01-12T19:38:39.693 に答える