Ahmad Ali
Ahmad Ali

Reputation: 23

Fatal Exception: android.app.RemoteServiceException: Bad notification for startForeground

I am using foreground services in my application and getting this crash logs in crashlytics. Please guide me how can i resolve this issue

Stack Trace:

Fatal Exception: java.lang.RuntimeException: bad array lengths at android.os.Parcel.readTypedArray(Parcel.java:3157) at android.view.IWindowSession$Stub$Proxy.addToDisplayAsUser(IWindowSession.java:1557) at android.view.ViewRootImpl.setView(ViewRootImpl.java:1556) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:509) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:133) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:5322) at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54) at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2438) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8663) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

Code (how i am starting the foreground service)

val recordCommand = gson.fromJson(command, RecordCommand::class.java)
            val intent = Intent(applicationContext, RecordCommandService::class.java)
            intent.putExtra(RecordCommandProcessingBaseI.EXTRAS, recordCommand)
            if (!AppUtils.isServiceRunning(this, RecordCommandService::class.java.name)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    applicationContext.startForegroundService(intent)
                } else {
                    applicationContext.startService(intent)
                }
            }



data class RecordCommand(
val duration: String?="1",
val id: String

)

Thanks in advance

Upvotes: 0

Views: 286

Answers (1)

LethalMaus
LethalMaus

Reputation: 956

To resolve this stacktrace, when sending data classes as an extra for an intent, the class needs to extend the Parcelable class

@Parcelize
data class RecordCommand(
    val duration: String?="1",
    val id: String
) : Parecelable

to send it do as follows

intent.putExtra(RecordCommandProcessingBaseI.EXTRAS, recordCommand as Parcelable)

to get it

val recordCommand = intent.getParcelableExtra(RecordCommandProcessingBaseI.EXTRAS)

Upvotes: 0

Related Questions