所以我不确定在何处/如何实现此方法以使我的服务在前台运行.目前我在另一项活动中通过以下方式开始我的服务:
Intent i = new Intent(context, myService.class);
context.startService(i);
Run Code Online (Sandbox Code Playgroud)
然后在myServices的onCreate()中我尝试startForeground()......?
Notification notification = new Notification();
startForeground(1, notification);
Run Code Online (Sandbox Code Playgroud)
所以是的,我有点失落,不确定如何实现这一点.
在我的应用程序中,我将我的服务放在前台,以防止它被使用:
startForeground(NOTIFY_ID, notification);
Run Code Online (Sandbox Code Playgroud)
这也向用户显示通知(这很棒).问题是以后我需要更新通知.所以我使用代码:
notification.setLatestEventInfo(getApplicationContext(), someString, someOtherString, contentIntent);
mNotificationManager.notify(NOTIFY_ID, notification);
Run Code Online (Sandbox Code Playgroud)
接下来的问题是:这样做是否会使服务脱离其特殊的前景状态?
在这个答案中,CommonsWare表明这种行为是可行的,但他不确定.那么有谁知道实际答案?
注意:我知道解决这个问题的一个简单方法是startForeground()每次我想要更新通知时重复调用.我想知道这种替代方案是否也有效.