从服务中删除持续通知

发布于 2025-01-06 04:21:23 字数 1472 浏览 0 评论 0原文

我有一项在启动时创建通知的服务。

然后 ondestroy() 我希望将其删除。

我只是使用 .cancel(NOTIFICATION_ID);

当它是正常通知时它效果很好,但当我使用正在进行的事件时它不会取消它。

我确实读到过一些关于如果 Android 有资源的话服务就不会停止的内容,但是如何克服这个问题呢?

我使用此代码来启动服务

    final Intent bg_service = new Intent(BackgroundService.class.getName());

    btn_start = (Button)findViewById(R.id.btn_start);
    btn_stop = (Button)findViewById(R.id.btn_stop);

    btn_start.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startService(bg_service);
        }
    });

    btn_stop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            stopService(bg_service);
        }
    });

我在创建时使用

            notificationManager = (NotificationManager) 
            getSystemService(NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.ic_launcher,
            "A new notification", System.currentTimeMillis());

    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

    Intent intent = new Intent(this, mainapp.class);
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "...",
            "...", activity);
    notificationManager.notify(19314, notification);

此代码 在销毁时使用此代码

            noficationManager.cancel(19314);

I have a service that create a notification when it is started.

And then ondestroy() i want it to be removed.

I just use .cancel(NOTIFICATION_ID);

It works great when it is a normal notification but when i am using ongoing event it just won't cancel it.

I did read something about that services doesn't stop if android have the resources for it, but how to overcome this?

I use this code to start the service

    final Intent bg_service = new Intent(BackgroundService.class.getName());

    btn_start = (Button)findViewById(R.id.btn_start);
    btn_stop = (Button)findViewById(R.id.btn_stop);

    btn_start.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startService(bg_service);
        }
    });

    btn_stop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            stopService(bg_service);
        }
    });

I use this in on create

            notificationManager = (NotificationManager) 
            getSystemService(NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.ic_launcher,
            "A new notification", System.currentTimeMillis());

    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

    Intent intent = new Intent(this, mainapp.class);
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "...",
            "...", activity);
    notificationManager.notify(19314, notification);

And this in on destroy

            noficationManager.cancel(19314);

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

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

发布评论

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

评论(1

静赏你的温柔 2025-01-13 04:21:23

如果可以的话,我会以不同的方式看待“持续”通知。 Service 上有一些方法专门用于添加和删除“正在进行的”通知。

Service.startForeground (int,通知)

Service.stopForeground(boolean)

也许您可以尝试从 onDestroy() 调用 stopForegroud(boolean)代码>方法优先...

I would look at doing 'ongoing' notifications a bit differently if you can. There are methods that live on Service that are dedicated to adding and removing an 'ongoing' notification.

Service.startForeground(int, Notification)

Service.stopForeground(boolean)

Perhaps you could just try calling stopForegroud(boolean) from your onDestroy() method first...

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