1

I know this is a duplicate problem but still, none of the answers worked for me. There are some devices in crashlytics that are causing this Fatal crash.

Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{e16921e u0 com.abc.MyService}
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1760)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:201)
   at android.app.ActivityThread.main(ActivityThread.java:6810)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

I have researched and seen so many answers regarding calling the start foreground(2, notification) in create of the Service.

I have done that already but that crash is not getting resolved. I am not able to replicate this crash in available devices.

Here is My Code.

MyService.kt

    class MyService : IntentService("MyService") {
    init {
        setIntentRedelivery(true)
    }

    private val wakeLock : PowerManager.WakeLock by lazy {
        (getSystemService(Context.POWER_SERVICE) as PowerManager)
            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyService:Wakelock")
    }

    override fun onCreate() {
        super.onCreate()
        Timber.i("service created")
        wakeLock.acquire(30 * 60 * 60)
        Timber.i("Wake lock acquired")

        // show notification
        val notification = NotificationUtil.newNotification
        startForeground(2, notification)
    }

    override fun onHandleIntent(intent: Intent?) {
       //>>>>>>>>> Code Here <<<<
    }

    private fun finish() {
        if(wakeLock.isHeld) {
            wakeLock.release()
        }
        stopSelf()
    }

    override fun onDestroy() {
        if(wakeLock.isHeld) {
            wakeLock.release()
        }
        Timber.i("Wakelock released")
        super.onDestroy()
        Timber.i("Service finished")
    }
}

Starting the service in Application Class's onCreate.

    val intent = Intent(this, MyService::class.java)
    ContextCompat.startForegroundService(appContext, intent)
Happy Singh
  • 1,334
  • 9
  • 23
  • There is a good chance that `IntentService` will be deprecated in the next version of Android. So, rather than use an `IntentService`, consider using `WorkManager` instead. Then, you do not need to deal with the service, foreground notifications, etc. yourself. However, in general, I do not think that there is a solution for your problem, in that there is no guarantee that `onCreate()` will be called in time for you to call `startForeground()`, particularly in your scenario (starting the service from `Application#onCreate()`. – CommonsWare Dec 23 '19 at 12:08
  • Okay, I got that so I need to go with WorkManager. Thanks. – Happy Singh Dec 23 '19 at 12:12
  • Which version of Android OS your device have in which you are testing? – Sandeep dhiman Dec 23 '19 at 12:16
  • I have tested on All API's from 23-29 API level, but it is happening most of on the OS 8,9. – Happy Singh Dec 23 '19 at 12:21
  • @CommonsWare if I will start Service in Launcher Activity then it will help? – Happy Singh Dec 23 '19 at 12:41
  • It might decrease the frequency of this error, but I doubt that it will eliminate the error entirely. It is also a different business rule: your current algorithm is "start the service every time the process starts", which is different than "every time my `MainActivity` is created". – CommonsWare Dec 23 '19 at 12:43
  • Yes, You are right. can JobIntentService help me to get rid of this? – Happy Singh Dec 23 '19 at 12:48

0 Answers0