Android 9(Pie),Context.startForegroundService()然后未调用Service.startForeground():ServiceRecord [英] Android 9 (Pie), Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord

查看:92
本文介绍了Android 9(Pie),Context.startForegroundService()然后未调用Service.startForeground():ServiceRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先, 我看着这些;

First of all, I looked at these;

  • Context.startForegroundService() did not then call Service.startForeground()
  • Context.startForegroundService() did not then call Service.startForeground
  • Android 9 (Pie) Only: Context.startForegroundService() did not then call Service.startForeground() - Works fine on Oreo
  • RemoteServiceException Context.startForegroundService() did not then call Service.startForeground()
  • https://issuetracker.google.com/issues/76112072

我有将近一百万人使用的流媒体应用程序.我正在为播放器使用前台服务.我还没有实现MediaSession.我有99.95%的无崩溃会话. 因此,该应用可以在所有版本上使用,但是我开始使用android 9收到崩溃报告(ANR).此崩溃仅发生在Samsung手机上,尤其是s9, s9+, s10, s10+, note9机型.

I have a streaming application used by almost a million people. I'm using foreground service for the player. I didn't implement MediaSession yet. I have a 99.95% crash-free sessions. So this app works on all versions but I started getting crash reports(ANR) with android 9. This crash occurs only Samsung phones, especially s9, s9+, s10, s10+, note9 models.

我尝试过这些,

  • onCreate()
  • 中调用startForeground()方法
  • Context.stopService()之前呼叫Service.startForeground()
  • 其他stackoverflow回答类似的问题
  • Calling startForeground() method in onCreate()
  • Calling Service.startForeground() before Context.stopService()
  • Other stackoverflow answers to similar questions

我阅读了Google开发人员的一些评论,他们说这只是Intended Behavior.我想知道这是由三星的系统还是Android OS引起的.有人对此有意见吗?我该如何解决?

I read some comments from developers of Google, they said it's just Intended Behavior. I wonder is it occurred by Samsung's system or Android OS. Does anyone have an opinion about this? How can I fix this?

推荐答案

我正在等待崩溃报告以共享解决方案.近20天我没有发生任何崩溃或ANR.我想分享我的解决方案.它可以帮助遇到此问题的人.

I was waiting my crash report to share the solution. I didn't get any crash or ANR almost 20 days. I want to share my solution. It can help those who encounter this problem.

onCreate()方法中

  • 首先,我的应用是媒体应用.我还没有实现mediasession.我正在onCreate()的顶部创建一个通知频道. 官方文档
  • 我要在Context.startForegroundService()方法之后调用Service.startForeground()方法.在我的prepareAndStartForeground()方法中.
  • First of all, my app is a media application. I didn't implement the mediasession yet. I'm creating a notification channel in the top of onCreate(). Official doc
  • I'm calling Service.startForeground() method after Context.startForegroundService() method. In my prepareAndStartForeground() method.

注意:我不知道为什么,但是 ContextCompat.startForegroundService()无法正常工作.

因此,我将相同的函数手动添加到了服务类中,而不是调用ContextCompat.startForegroundService()

For this reason, I've added manually same function to my service class instead of calling ContextCompat.startForegroundService()

private fun startForegroundService(intent: Intent) {
    if (Build.VERSION.SDK_INT >= 26) {
        context.startForegroundService(intent)
    } else {
        // Pre-O behavior.
        context.startService(intent)
    }
}


prepareAndStartForeground()方法

private fun prepareAndStartForeground() {
    try {
        val intent = Intent(ctx, MusicService::class.java)
        startForegroundService(intent)

        val n = mNotificationBuilder.build()
        // do sth
        startForeground(Define.NOTIFICATION_ID, n)
    } catch (e: Exception) {
        Log.e(TAG, "startForegroundNotification: " + e.message)
    }
}


这是我的onCreate()

override fun onCreate() {
    super.onCreate()
    createNotificationChannel()
    prepareAndStartForeground()
}


我的onStartCommand()

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    if (intent == null) {
        return START_STICKY_COMPATIBILITY
    } else {
        //....
        //...
    }
    return START_STICKY
}


onRebindonBindonUnbind这样的方法


onRebind, onBind, onUnbind methods like these

internal var binder: IBinder? = null

override fun onRebind(intent: Intent) {
    stopForeground(true) // <- remove notification
}

override fun onBind(intent: Intent): IBinder? {
    stopForeground(true) // <- remove notification
    return binder
}

override fun onUnbind(intent: Intent): Boolean {
    prepareAndStartForeground() // <- show notification again
    return true
}


当onDestroy()调用时,我们需要清除一些东西


We need to clear something when onDestroy() calling

   override fun onDestroy() {
    super.onDestroy()
    releaseService()
   }


private fun releaseService() {
    stopMedia()
    stopTimer()
    // sth like these
    player = null
    mContext = null
    afChangeListener = null
    mAudioBecomingNoisy = null
    handler = null
    mNotificationBuilder = null
    mNotificationManager = null
    mInstance = null
}

我希望该解决方案对您来说正确.

I hope this solution works properly for you.

这篇关于Android 9(Pie),Context.startForegroundService()然后未调用Service.startForeground():ServiceRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆