In my projects, I use BroadcastReceiver as a callback from a long running thread (for example, notifies me that the operation is completed and send some response data from Worker Thread so that the activity can display the corresponding message to the user ..). In order to use BroadcastReceiver , I have to be careful to register and unregister the broadcast receiver each time I use it, and also take care which messages to send esspecialy when I use this method for more different actions (e.g. downloading, creating WebService calls, etc.). And also for sending custom objects via translation, I also need to make Parcelable objects.
Unlike this approach, I also looked at the callback methods approach, which looks simpler than the method I use. Callback methods are a simple implementation of front-end methods that can be used to achieve the same effect as BroadcastRecaiver in application messaging. This approach does not require a Parcelable implementation to return complex objects, and it does not use keys such as BroadcastReceiver .. I think the bad part is that I need to check the callback object for a null value before I want to call the callback method calling .., and also to make sure that I run the code from the implementation in the user interface thread, so I can update the interface without errors.
Well, I hope you understand what I wanted to say :).
Now the question is, how do you think the callback method is better (easier, cleaner, faster ..) than the BroadcastReceiver approach, when they are used only within one application? (Note that I do not use the Android Service for background work. Just AsyncTask and Thread s)
Thank!
android multithreading callback broadcastreceiver
Cata May 13 '12 at 21:03 2012-05-13 21:03
source share