问题描述
我试图让我的服务
在前台运行。我试图用这个例子(请认准节的在前台运行的服务的),但 startForeground()
实际上并没有显示我的通知。而没有异常被抛出。为了使它显示,我需要使用 NotificationManager
喜欢这里 的解释。随着 NotificationManager
我的通知工作,但我不知道我的服务
终止后,这个无声电话的前景 startForeground()
。
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()
.
什么是错的?
修改:我只是测试此示例项目应该表现出 startForeground()
,但它不工作!我使用的API v7.0的,我无论在模拟器和真实设备(SE的Xperia新)进行了测试。通知将不会出现。
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 :如果我试图调用 setForeground()
然后我得到了一个警告 setForeground:无视旧的API调用
。
EDIT2: if i try to call setForeground()
then i got a warning setForeground: ignoring old API call
.
我还试图用 startForegroundCompat()
这里描述,但效果是absolutelly相同。检查我的服务使用前景 ActivityManager.RunningServiceInfo
描述这里,和我看到我的服务的不可以前景。
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.
推荐答案
我只注意到 startForeground()
不显示通知图标,如果 ID
参数设置为 0
...
I just noticed that startForeground()
doesn't show the notification icon if the id
parameter is set to 0
...
startForeground(0, notification); // Doesn't work...
startForeground(1, notification); // Works!!!
我希望它可以帮助别人停留在此。
I hope that it could help someone stuck on this.
这篇关于startForeground()不显示我的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!