gpt4 book ai didi

android - 终止服务后通知仍在栏中

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:19 24 4
gpt4 key购买 nike

我有一个有趣的问题:

我开始我的 Activity ,然后从这个 Activity 开始我在另一个进程中的服务。当服务启动时,它会在栏中显示一条通知。通知使用 .setOngoing(true),因此只有当我从 Activity 中停止服务时它才会消失。

然后在 Eclipse 中,在 Devices 选项卡中我终止了我的服务进程,但通知仍然存在!要关闭它,我必须再次启动该服务,然后必须停止它。

Why is my Notification not killed together with Service?

最佳答案

所以你说服务在另一个进程上?

我正在开发具有类似结构的应用程序。请检查包 com.philippheckel.service 中的“AbstractService”类和“ServiceManager”类。

Here are the classes

Here an Example

下面是我如何创建通知

使用 Compat.Builder 来兼容旧的和新的 android 版本

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.service_header))
.setContentText("Connect to: http://" + httpd.getip() + ":8080")
.setContentIntent(pendingIntent)
.setOngoing(true);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(01, mBuilder.build());

下面是我如何关闭/取消通知

看到我再次使用相同的 ID 了吗?这很重要

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(01);

关于android - 终止服务后通知仍在栏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19024845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com