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!
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.Broadcastreceivers should be used If you need to send broadcasts across applications while Callbacks (or Handlers as suggested by Alex) are better to use in your situation.
If you want to use other than these two, consider using Observer (Interface included in android) and delegate.
For delegate please consider this SO post.
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 baseActivity
class that does registration and unregistration withIntentFilter
s in itsonResume()
resp.onPause()
methods.Intent
has to implement theParcelable
interface, but furthermore there is a strict size limitation and it will cause performance issues if you transport a large amount of data with yourIntent
. 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 yourIntent
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
orHandlerThread
if usingMessageQueue
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 theBroadcastReceiver
-approach that lets you decouple your UI layer from the logic layer.not sure what the goal is , but if you wish to keep the same idea of using intent and broadcastReceiver , and want better performance and security than normal broadcastReceivers , you can try out this demo , available in the android support library :
http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/content/LocalServiceBroadcaster.html
if not , you can always use asyncTask , service , handlers , etc...
I'll just add another option to the other great answers you've received already...
You don't have to create a broadcast receiver to receive Intents. In your android manifest file you can register any activity to receive intents:
Then override the
onNewIntent(Intent)
method in your activity to receive it.To send the Intent, use the
Context.startActivity(Intent)
method. Most likely you'll want to add theFLAG_ACTIVITY_SINGLE_TOP
flag to your Intent so it doesn't create a new instance of your activity if one is already running.EDIT: I just noticed you are running within a single application. Therefore, a simple callback is probably best. The solution above does work in a single app, but is more appropriate for different applications. I'll leave this here just in case it helps someone. Good luck!