Fatal Exception: android.app.RemoteServiceException: can't deliver broadcast at android.os.Handler.dispatchMessage
Asked Answered
R

3

40

I am using broadcast messages on my android application (From io.socket I am sending broadcast messages to my Activity page). On some devices Samsung SM-G950F and SM-A520F I got an error "Fatal Exception: android.app.RemoteServiceException: can't deliver broadcast". I got this error on Fabric crashlytics also I was not able to reproduce this issue. Here is the log I got from Fabric,

   Fatal Exception: android.app.RemoteServiceException: can't deliver broadcast
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1813)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6776)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Reclamation answered 27/10, 2017 at 8:55 Comment(1)
It seems your broadcast receiver activity/fragment is being destroyed by the system while the service is trying to send a broadcast. Look at the source code android.googlesource.com/platform/frameworks/base/+/master/… line 498. This seems to be a random issue. A possible workaround can be to register broadcast receiver in onResume() and unregister the same in onPause() of the fragment/activity.Gallicanism
A
18

I was facing the same issue with my app, what I do is use LocalBroadcastManager instead of context. Android documents also suggest using LocalBroadcastManager for sending in-app broadcast receivers.

//register your receiver like this
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
          new IntentFilter("custom-event-name"));

// unregister  like this
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);

// broadcastlike this
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

Hope this will help. Thanks! :)

Asphyxiant answered 19/2, 2018 at 12:13 Comment(10)
I don't even have usages of LocalBroadcastManager, and yet I got this error recently. Very rare error though. Happened only once so far, on OnePlus 5T with Android 9... Any other idea why this could happen?Mathews
@android developer same thing happening with me.. did you get any solution ?Deland
@Kamwave No idea why it occurs. I just chose to ignore it, just like other weird cases I've seen that are very very rare. I think that in general, if you see issues with Chinese-only devices, you can ignore them because there is almost never anything you could do about it. See here: dontkillmyapp.com , and here: issuetracker.google.com/issues/122098785 . Also please consider starring this issue. It's really bad that Chinese manufacturers ruin apps (and they do it by default).Mathews
Also seeing these (although they are rare), but only on OnePlus devices running Android Pie. It's unfixable.Windy
I only got this crash once with Google Pixel 5 with Android 13 in Google Play Console. Not sure whether it was a real user or a device from the Play pre-launch report.Autoplasty
I'm having this issue specifically on Google Pixel devices running Android 12. Always happens while the app is in the background. Weird thing is were not sending or receiving any broadcasts while in that state...Roxi
@AdamSousa I'm running into the exact same issue where it only happens on Pixels, Android 12, and in the background. Have you found anything?Ildaile
@TommyJackson We're still investigating the issue. It's possible that one of our imported libraries might be causing the issue. We've added more logging to make sure its not our codebase.Roxi
@AdamSousa, I'm having the same issue. I wonder if you were able to find anything?Aloisia
@androiddeveloper for me it happens on many Samsung devices (Android 12-13 for now, didn't notice crashes for 14 or earlier versions)Idette
S
5

I experienced the exact same thing, around the exact same time, with the same devices. The problem was ultimately related to the app I'm supporting, but I think Samsung pushed out some type of update that started triggering the problem. Before the latter part of Oct, the app never had this issue. It was driving me nuts because I couldn't figure out which broadcast was triggering the problem.

Based on user feedback, I finally narrowed it down and made the following changes:

1) I went through the app and made sure that all custom "action" strings used for Intents included the app's package name.

2) I switched from using Context::sendBroadcast() to LocalBroadcastManager::sendBroadcast().

You can see my full answer on a different post here

Salaam answered 9/11, 2017 at 13:29 Comment(0)
B
3

I had the same problem. i was sending very long json string. when i just replaced string with "hello" (means very short string for testing if my code has problem) then it worked without error.

so the problem is with what you are sending. try changing that and it will work.

Barred answered 6/3, 2022 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.