If you have activities that communicate with a service, or another service communicating with a service, which "communication protocol" is faster to deliver it's message to the binding party?
Messages that are transmitted to/from the service are all strings or parcelable objects.
I am having difficulties deciding which method is faster if you want to send bulk data to another service or activity. I cannot find details about the processing speed of Intents or Messages sent by a Messenger.
Anybody experience with this?
An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object.
This example demonstrates how do I communicate between Activity and Service in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
You have to use BroadcastReceiver to receive intents, and when you want to communicate simply make an Intent with appropriate values. This way you should be able to make a 2-way communication between any component.
For Intent, IntentResolver required time to find appropriate target as it will search through list of receivers. This case performence will depend on your system and number of receivers, IntentResolver required to search to find appropriate receiver.
On the other side if you send Message through Handler, it will use the Message Queue of the same thread and that case performance will depend on available message on that Queue.
So seems like message going to be faster then Intent or at least theoretically, but you can always write a short program to see the output.
But, if you have lots of data from to send from service to Activity you can think of another alternative solution where you write the data to a persistence store and after just notify your Activity that data is ready. upon receiving the notification Activity read the data from data source. In this way, if user receive a phone call or get disrupted in between you data is safe.
You might want to consider LocalBroadcastManager.sendBroadcastSync() http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html#sendBroadcastSync(android.content.Intent)
If your activities and services are in the same process then it is is better to use LBM since it is faster and more secure. And it gives you the option of sending a broadcast 'sync' which means it is handled immediately, so it would probably qualify as 'fastest', even though messages/handler are otherwise faster then intents/receivers.
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