我正在尝试让我的 Service
在前台运行。我尝试使用 this example (请查看“在前台运行服务”部分),但 startForeground()
实际上并没有显示我的通知。并且不会抛出任何异常。为了让它显示,我需要使用 NotificationManager
像 here解释。使用 NotificationManager
我的通知有效,但我不确定我的 Service
在此“无声”调用 startForeground()
后是否处于前台。
有什么问题?
编辑:我刚刚测试了this sample project这应该演示 startForeground()
,但它不起作用!我使用 API v7.0,我在模拟器和真实设备(SE Xperia Neo)上都对其进行了测试。通知未出现。
EDIT2:如果我尝试调用 setForeground()
,则会收到警告 setForeground: ignoring old API call
。
我也尝试使用 startForegroundCompat()
描述 here ,但效果是完全一样的。我使用 ActivityManager.RunningServiceInfo
检查我的服务是否在前台,如 here 所述,我发现我的服务不是前台。
最佳答案
我刚刚注意到,如果 id
参数设置为 0
...startForeground()
不会显示通知图标...
startForeground(0, notification); // Doesn't work...
startForeground(1, notification); // Works!!!
我希望它可以帮助坚持这一点的人。
关于android - startForeground() 不显示我的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8725909/