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

pro*_*m85 5 service android foreground foreground-service

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

我所关心的事情如下:

  • 在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 by updateNotification [OverlayService [onInitFinished]]) - moved to foreground: false
Run Code Online (Sandbox Code Playgroud)

这是一份崩溃报告的日志 - 正如您所看到的,服务在第 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)
Run Code Online (Sandbox Code Playgroud)

有人看到我的基类有任何问题吗?

代码

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
    }
}
Run Code Online (Sandbox Code Playgroud)

JBA*_*JBA 0

我有同样的问题,可以这样解决:

startForeground从 Android 9 Pie 开始,如果您的服务在使用命令启动后 5 秒内没有调用startForegroundService......那么它会产生 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...
}
Run Code Online (Sandbox Code Playgroud)