相关疑难解决方法(0)

Context.startForegroundService() 然后没有调用 Service.startForeground() - 仍然是一个问题

在检查了谷歌问题和很多其他问题之后,我找到了我在底部添加的解决方案。

我所关心的事情如下:

  • onCreateAND 中onStartCommand,我确保将服务移至前台(如果它尚未位于前台)
  • 我不是简单地停止服务,而是使用发送到onStartCommand处理程序的额外命令来执行此操作,以确保服务在启动时不会停止(最终在它完成进入前台之前)
  • 我从不直接停止服务 ( context.stopService(...)),我总是通过正在运行的服务本身的命令来停止服务 - 所以我确保它只能在它在前台运行时停止,而不是在启动期间停止
  • 一个服务只能停止一次
  • 我通过取消服务的前台通知来停止服务,然后才停止服务本身

我个人使用该服务进行覆盖,因此我不在类中处理活页夹,因为我不使用它们。

我得到的日志

2019-07-29 21:41:27,146 [[BaseOverlayService:62 onCreate]]: onCreate
2019-07-29 21:41:27,146 [[BaseOverlayService:142 b]]: BEFORE moveToForeground (called by onCreate)
2019-07-29 21:41:27,152 [[BaseOverlayService:159 b]]: AFTER moveToForeground (called by onCreate) - moved to foreground: true
2019-07-29 21:41:27,176 [[BaseOverlayService:79 onStartCommand]]: onStartCommand: isForeground: true | action: null | isStopping: false
2019-07-29 21:41:27,945 [[BaseOverlayService:142 b]]: BEFORE moveToForeground (called by updateNotification [OverlayService [onInitFinished]])
2019-07-29 21:41:27,947 [[BaseOverlayService:159 b]]: AFTER moveToForeground (called …
Run Code Online (Sandbox Code Playgroud)

service android foreground foreground-service

5
推荐指数
1
解决办法
1765
查看次数

标签 统计

android ×1

foreground ×1

foreground-service ×1

service ×1