36

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)
SHIDHIN TS
  • 1,557
  • 3
  • 26
  • 58
  • 2
    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 https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/am/BroadcastQueue.java 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. – Rahul Shukla Nov 03 '17 at 19:04

3 Answers3

16

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! :)

Asheesh
  • 565
  • 6
  • 21
  • 5
    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? – android developer Jan 09 '19 at 19:02
  • @android developer same thing happening with me.. did you get any solution ? – KamDroid Mar 05 '19 at 06:29
  • 3
    @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: https://dontkillmyapp.com/ , and here: https://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). – android developer Mar 05 '19 at 09:57
  • Also seeing these (although they are rare), but only on OnePlus devices running Android Pie. It's unfixable. – b0b Apr 13 '19 at 09:48
  • 2
    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. – Harry Jun 21 '22 at 15:30
  • 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... – Adam Sousa Jun 28 '22 at 13:46
  • @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? – Tommy Jackson Jul 07 '22 at 00:34
  • 1
    @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. – Adam Sousa Jul 11 '22 at 15:49
  • @AdamSousa, I'm having the same issue. I wonder if you were able to find anything? – Valeriy Kovalenko Jul 20 '22 at 17:19
4

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

klaust
  • 91
  • 4
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.

MbPCM
  • 457
  • 5
  • 12