views:

148

answers:

1

Hi,

I am developing an application which is communicating with the server. Tha application can perform log-in and get different parameters from server.

The application consists of a RESTful client (custom class for making requests), Communication Service (the service which runs in the background) and the main activity.

For now I created multiple broadcast messages and multiple broadcast receivers in the main activity so when the application performs login operation a receiver (loginBroadcastReceiver) in the main activity receives a message and when another parameter is received from the server different message is broadcasted and another receiver handles the message.

This way however the application performance is poor but I am not sure whether it is due to multiple broadcast receivers.

Does anyone know what is the best way to exchange data between service and main activity - is it better to create a single broadcast receiver and retrieve all parameters from message or is it better to initialize multiple broadcast receivers for multiple parameters?

I would appreciate if you could provide any useful resource about the topic because I'm writing the thesis and it would be good if the solution could be explained.

A: 

Does anyone know what is the best way to exchange data between service and main activity - is it better to create a single broadcast receiver and retrieve all parameters from message or is it better to initialize multiple broadcast receivers for multiple parameters?

If the service and activity are local (i.e., same app, same process), using BroadcastReceiver and Intent objects will perform much worse than simply having your components talk to each other. Have the activity register a callback object with multiple event methods with the service, and have the service call the appropriate event method on the callback when the events occur.

The BroadcastReceiver-and-Intent pattern is designed for broadcasting across the device, not just between application components, and so will go through lots of overhead to deal with finding receivers, marshaling and unmarshaling across process boundaries, and whatnot.

You can even use the callback technique across processes via AIDL-defined callbacks.

CommonsWare