startForeground() 不显示我的通知
我正在尝试让我的 Service
在前台运行。我尝试使用此示例(请查找“运行服务”部分在前台”),但是 startForeground()
实际上并没有显示我的通知。并且不会抛出任何异常。为了使其显示,我需要使用 NotificationManager
就像这里 解释道。使用 NotificationManager
我的通知可以正常工作,但我不确定在对 startForeground()
进行“静默”调用后我的 Service
是否处于前台。
有什么问题吗?
编辑:我刚刚测试了这个示例项目 应该演示 startForeground()
,但它不起作用!我使用 API v7.0,我在模拟器和真实设备(SE Xperia Neo)上进行了测试。不出现通知。
EDIT2:如果我尝试调用setForeground()
,则会收到警告setForeground:忽略旧的 API 调用
。
我还尝试按照此处所述使用startForegroundCompat()
,但效果是完全一样。我使用 ActivityManager.RunningServiceInfo
检查我的服务是否处于前台,如此处,我发现我的服务不是前台。
I am trying to make my Service
running in foreground. I tried to use this example (please look for the section "Running a Service in the Foreground"), but startForeground()
does not actually show my notification. And no exceptions is thrown. To make it shown, I need to use NotificationManager
like here explained. With NotificationManager
my notification works, but i'm not sure that my Service
is foreground after this "silent" call to startForeground()
.
What can be wrong?
EDIT: I just tested this sample project that should demonstrate startForeground()
, but it does not work! I use API v7.0, I tested it both on emulator and real device (SE Xperia Neo). Notification does not appear.
EDIT2: if i try to call setForeground()
then i got a warning setForeground: ignoring old API call
.
I also tried to use startForegroundCompat()
as described here, but effect is absolutelly the same. I check if my service is foreground using ActivityManager.RunningServiceInfo
as described here, and I see that my service is not foreground.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我刚刚注意到,如果
id
参数设置为0
,startForeground()
不会显示通知图标...我希望它可以帮助有人坚持这个问题。
I just noticed that
startForeground()
doesn't show the notification icon if theid
parameter is set to0
...I hope that it could help someone stuck on this.
除了最佳答案。
您还应该检查是否调用了
setSmallIcon
。在我的 Android 手机上,如果不调用
setSmallIcon
,我就无法获得预期的结果in addition to the best answer.
you should also check that have you called
setSmallIcon
.On my android phone, I cannot get what I expected without calling
setSmallIcon
您的服务是启动服务还是绑定服务?我对绑定服务也有同样的问题,但是在绑定服务之前启动服务允许我调用
startForeground(int, notification)
并显示通知。Is your service a started service or a bound service? I had the same issue with a bound service, but starting the service before binding it allowed me to call
startForeground(int, notification)
with and have the notification show up.这可能是一个旧线程,但我想添加我刚刚学到的尚未提及的内容:
在
stopSelf()
Service 可能仍然存在> 被调用是因为有Activity
已绑定到Service
。事实上,在这种情况下,startForeground() 不会显示通知,也不会给出任何异常。This might be an old thread, but I'd like to add what I just learned which is not mentioned yet:
It is possible that a
Service
is still alive afterstopSelf()
is called because there areActivity
that have bound to theService
. As a matter of fact, thestartForeground()
is just not going to show the notification nor giving any exception in this circumstance.就我而言,在
startForeground(...)
调用后没有通知,因为我仅使用.setSubText(...)
来设置它的消息(因为它是在大多数设备上以较小的字体呈现)。但是,如果您不使用.setContentText(...)
设置消息,某些设备小米红米 Note 4 将不会显示任何通知。希望能帮助某人
in my case there was no notification after
startForeground(...)
invoke because I used only.setSubText(...)
for setting the message of it (because it is rendered with smaller font on most of devices). But some of devices Xiaomi Redmi Note 4 won't show any notification if You not set message by using.setContentText(...)
.Hope that will help someone
德米特里。我刚刚遇到了你的问题并找到了原因。
如果您的应用正在更改组件的状态
PackageManager.setComponentEnabledSetting())
Android 会从前台删除该服务及其通知图标。2011 年 11 月报告的错误
DMitry. I have just suffered your problem and found the cause.
If your app is changing state of a COMPONENT
PackageManager.setComponentEnabledSetting())
Android removes the service from foregraound and its notification icon.Bug reported at Nov, 2011