In my projects I am using BroadcastReceiver
s as a callback from a long running thread (eg. notify the activity that a download was finished and send some response data from a Worker Thread
so that the activity can display the appropriate message to the user..). To use BroadcastReceiver
s I have to be careful to register and unregister the broadcast receiver each time I am using it and also have to care of what messages to send esspecialy when I am using this method for more different actions(like downloading, making WebService calls etc..). And also to send custom Objects through Broadcast's intent I need also to make the objects Parcelable
.
Unlike this approach, I have seen also the callback methods approach which appears to be simpler than the method I use. Callback methods are simple Interface methods implementation that can be used to achieve the same effect like the BroadcastRecaiver's in app messaging. This approach doesn't need Parcelable implementation to return complex objects and it doesn't use keys like BroadcastReceiver
.. I think the bad part is that I need to check the callback object for null value before I want to call a callback method.. and also to make sure I am running the code from the implementation on the UI thread so I can update the UI without errors.
Ok, I hope you understood what I meant to say :).
Now the question is do you think that the callback method is better (lighter, cleaner, faster..) than the BroadcastReceiver
approach when are used just inside of a single application? (Note that I am not using Android Service
for background work.. just AsyncTask
and Thread
s)
Thank you!
Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.
A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.
Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.
As a general rule, broadcast receivers are allowed to run for up to 10 seconds before they system will consider them non-responsive and ANR the app.
This is a very interesting question and I ran into the same problem. In my opinion both mechanisms can be used altogether and the right approach to use depends on your use case. Here are some points to be taken into account before deciding.
Using the callback-mechanism has some benefits, but there are also limitations:
PRO
CONTRA
Observer
/ Observable
mechanism.null
before invoking callback functions if the callback may be optional.Some points regarding the BroadcastReceiver
-approach:
PRO
onReceive()
method is always executed on the main thread.CONTRA
Intent
is an additional error source.Intent
's actions unique (e.g. by prepending the package name) if you want to eliminate correlations with other apps, as their original purpose is to do broadcasts between applications.BroadcastReceiver
-registration and unregistration. If you want to do this in a more comfortable way, you can implement a custom annotation to annotate your Activity with the actions that should be registered and implement a base Activity
class that does registration and unregistration with IntentFilter
s in its onResume()
resp. onPause()
methods.Intent
has to implement the Parcelable
interface, but furthermore there is a strict size limitation and it will cause performance issues if you transport a large amount of data with your Intent
. See http://code.google.com/p/android/issues/detail?id=5878 for a discussion on that. So if you want to send images for example you have to store them temporary in a repository and send a corresponding ID or URL to access the image from the receiver of your Intent
that deletes it from the repository after usage. This leads to further problems if there are several receivers (when should the image be removed from the repository and who should do that?).Intent
s to understand what has triggered a specific error or why this notification chain is broken at some point.In my opinion, even a mobile app should have an architecture base on at least 2 layers: the UI-layer and the core layer (with business logic, etc.). In general, long running tasks are executed in an own thread (maybe via AsyncTask
or HandlerThread
if using MessageQueue
s) inside the core layer and the UI should be updated once this task has been finished. In general with callbacks you achieve a tight coupling between your components, so I would prefer using this approach only within a layer and not for communication across layer boundaries. For message broadcasting between UI- and core-layer I would use the BroadcastReceiver
-approach that lets you decouple your UI layer from the logic layer.
I don't see what you gain by using BroadcastReceiver
in your case. Callbacks or, better probably, Handlers
would be the way to do it. BroadcastReceiver
is good when you do not know who the subscribers are.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With