startForeground() 不显示我的通知 [英] startForeground() does not show my Notification

查看:21
本文介绍了startForeground() 不显示我的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的 Service 在前台运行.我尝试使用这个示例(请查找运行服务"部分在前台"),但 startForeground() 实际上并没有显示我的通知.并且不会抛出任何异常.为了让它显示出来,我需要使用 NotificationManagerhere 解释.使用 NotificationManager 我的通知有效,但我不确定我的 Service 在对 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 Neo) 上都对其进行了测试.不显示通知.

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: ignoring old API call.

EDIT2: if i try to call setForeground() then i got a warning setForeground: ignoring old API call.

我也尝试使用 startForegroundCompat()此处所述,但效果完全一样.我使用 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() 不显示我的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆