【问题标题】:Context.startForegroundService() did not then call Service.startForeground() - still a problemContext.startForegroundService() 然后没有调用 Service.startForeground() - 仍然是一个问题
【发布时间】:2019-12-07 21:04:03
【问题描述】:

在检查了 google 问题和许多其他关于 SO 的问题后,我来到了我在底部添加的解决方案。

我关心的是:

  • onCreateonStartCommand 中我确保将服务移动到前台(如果它还没有在前台)
  • 我不只是简单地停止服务,而是使用发送到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 by updateNotification [OverlayService [onInitFinished]]) - moved to foreground: false

这是一份崩溃报告的日志——如您所见,服务在第 3 行 (moved to foreground: true) 被移动到前台,在第 6 行它知道它已经在前台运行。

我在我的 android 9 设备上大量使用这个应用程序(24/7,它一直在运行)并且没有问题,而且由于我从下面使用基类,所以问题确实减少到每月发生几次崩溃全部的。尽管如此,上面的日志显示,我的服务在几毫秒内在前台运行,但它仍然可能崩溃,如下所示:

android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{86fa711 u0 com.my.app/com.my.app.service.OverlayService}
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1855)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:6986)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

有人发现我的基类有什么问题吗?

代码

abstract class BaseOverlayService<T : BaseOverlayService<T>>(
        val foregroundNotificationId: Int,
        val notificationCreator: ((service: T) -> Notification)
) : Service() {

    companion object {

        val DEBUG = true

        // helper function, simply checks if this service is already running by checking the ActivityManager
        inline fun <reified T : BaseOverlayService<T>> isRunning(context: Context): Boolean {
            return Tools.isServiceRunning(context, T::class.java)
        }

        inline fun <reified T : BaseOverlayService<T>> start(context: Context, checkIfServiceIsRunning: Boolean) {
            if (checkIfServiceIsRunning && isRunning<T>(context)) {
                L.logIf { DEBUG }?.d { "IGNORED start intent" }
                return
            }

            L.logIf { DEBUG }?.d { "send start intent" }
            val intent = Intent(context, T::class.java)
            ContextCompat.startForegroundService(context, intent)
        }

        inline fun <reified T : BaseOverlayService<T>> sendAction(context: Context, checkIfServiceIsRunning: Boolean, action: String, intentUpdater: ((Intent) -> Unit) = {}) {
            if (checkIfServiceIsRunning && !isRunning<T>(context)) {
                L.logIf { DEBUG }?.d { "IGNORED action intent - action: $action" }
                return
            }

            L.logIf { DEBUG }?.d { "send action intent - action: $action" }
            val intent = Intent(context, T::class.java)
            intent.action = action
            intentUpdater(intent)
            ContextCompat.startForegroundService(context, intent)
        }
    }

    protected var isForeground = false
        private set
    protected var isStopping: Boolean = false
        private set

    // ------------------------
    // service events
    // ------------------------

    final override fun onCreate() {

        L.logIf { DEBUG }?.d { "onCreate" }

        super.onCreate()

        if (foregroundNotificationId <= 0) {
            throw RuntimeException("foregroundNotificationId must be > 0!")
        }

        moveToForeground("onCreate")

        onCreateEvent()
    }

    final override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        val returnValue = START_STICKY

        L.logIf { DEBUG }?.d { "onStartCommand: isForeground: $isForeground | action: ${intent?.action} | isStopping: $isStopping" }

        // 1) if service is stopping, we ignore the event
        if (isStopping) {
            return returnValue
        }

        // 2) if service is not running in foreground we make it run in the foreground
        if (!isForeground) {
            moveToForeground("onStartCommand")
        }

        onStartCommandEvent(intent, flags, startId)

        return returnValue
    }

    final override fun onBind(intent: Intent): IBinder? {
        // overlay service is never bound!
        return null
    }

    // ------------------------
    // Forwarded abstract events
    // ------------------------

    abstract fun onCreateEvent()

    abstract fun onStartCommandEvent(intent: Intent?, flags: Int, startId: Int)

    abstract fun onStopEvent()

    // ------------------------
    // protected functions
    // ------------------------

    protected fun stopService() {

        L.logIf { DEBUG }?.d { "stopService | isStopping: $isStopping" }

        if (isStopping) {
            L.logIf { DEBUG }?.d { "IGNORED stopService" }
            return
        }

        onStopEvent()
        isStopping = true
        moveToBackground(true)
        stopSelf()

        L.logIf { DEBUG }?.d { "stopService finished" }
    }

    protected fun updateNotification(caller: String) {
        moveToForeground("updateNotification [$caller]")
    }

    // ------------------------
    // private foreground/background functions
    // ------------------------

    private fun moveToForeground(caller: String): Boolean {

        L.logIf { DEBUG }?.d { "BEFORE moveToForeground (called by $caller)" }

        // 1) Create notification
        val notification = notificationCreator(this as T)

        // 2.1) Create foreground notification
        val result = if (!isForeground) {
            isForeground = true
            startForeground(foregroundNotificationId, notification)
            true
        }
        // 2.2) Update foreground notification
        else {
            notificationManager.notify(foregroundNotificationId, notification)
            false
        }

        L.logIf { DEBUG }?.d { "AFTER moveToForeground (called by $caller) - moved to foreground: $result" }

        return result
    }

    private fun moveToBackground(cancelNotification: Boolean) {
        isForeground = false
        super.stopForeground(cancelNotification)
    }

    // ------------------------
    // private helper functions
    // ------------------------

    private val notificationManager by lazy {
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    }
}

【问题讨论】:

  • 如果您找到了问题的解决方案,请将其作为答案发布并接受
  • @matdev 从我的问题中尝试解决方案,这是我目前能找到的最好的解决方案,并且大部分时间都可以使用。如果我在 SO 之外解决它,我会发布答案
  • 这似乎只是谷歌作为“安全措施”积极引入的一个主要错误的又一个例子,他们完全无视开发人员的投诉。多年来,我一直在尝试解决这个问题,最终得到了 20 多个不同的崩溃报告,每个通常每月触发 3 到 10 次,它们都有这个潜在的错误。 [插入“这太疯狂了!”,“疯狂?这是 GOOOOGLE!”此处为 GIF]

标签: android service foreground foreground-service


【解决方案1】:

我遇到了同样的问题,可以这样解决:

在 Android 9 Pie 中,如果您的服务在使用命令 startForegroundService 启动后 5 秒内未调用 startForeground ... 那么它会产生 ANR + 崩溃。

解决方案是在前台服务的 onStartCommand 方法的开头添加一个 startForeground() 命令和通知,如下所示:

final override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int 
{
    startForeground(YOUR_FOREGROUND_NOTIFICATION_ID, Notification);
    // rest of your code here...
}

【讨论】:

  • 如果你检查我的问题,这已经是我解决方案的一部分
  • 对不起,你说得对。您可能会看一下是否可能发生代码中的条件 2) 未填充,然后 moveToForeground() 未调用...最后系统会终止您的服务?
  • 文档中规定的 5 秒规则在哪里?
  • 写成here,查看带有星号的蓝色背景注释,其结尾为:服务创建后,服务必须在五秒内调用其 startForeground() 方法。
猜你喜欢
  • 2019-01-09
  • 2018-03-29
  • 2018-03-04
  • 1970-01-01
  • 2017-11-09
  • 2018-05-16
  • 2021-04-10
  • 1970-01-01
相关资源
最近更新 更多