清除 Android 状态通知

发布于 2024-12-26 22:32:55 字数 1096 浏览 3 评论 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);     
}

然后,我尝试通过调用“

notification.cancel(0);

我已创建通知实例”来清除服务的 onDestroy() 函数中的通知状态,但我不确定该实例是否正确。

private NotificationManager notification;

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

Im having difficulty clearing a status bar Notification.

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

I'm then trying to clear the notification status in the onDestroy() function of my service by calling

notification.cancel(0);

I have created an instance of the notification, which im not sure is correct.

private NotificationManager notification;

When I try to cancel the service, hence clear the status bar, the app crashes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静待花开 2025-01-02 22:32:55

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

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

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

NotificationManager 文档

Your notification object may be null, which would cause a NullPointerException.

I think you should be using a NotificationManager object to cancel a notification, use the same id (in your case, 0) and pass that to NoticationManager.cancel(Int):

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

NotificationManager docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文