I am trying to have a bidirectional connection between 2 devices, one will be the server and the other one the client. Since it is bidirectional, both can send a receive data.
I am creating them in an Activity that i have, that lists the installed apps that i have on my phone (filtered the system apps). Then i have an adapter for that list, where i define a listener for the items of that list. Basically if i press one of the items from the list, i want to send a message to the other device.
So, if inside the method onHandleIntent() i am waiting for messages, where do I put the code to SEND messages ?
EDIT: I could create another service for the send part but that would make me create 4 services, 2 for the client (send and receive) and another 2 for the server (send and receive again).
An IntentService has the following behavior:
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
from the docs. The same IntentService isn't supposed to do 2 things at the same time- if it is listening (I guess a while loop in onHandleIntent?), then any messages you send to that IntentService while it is listening will not be handled. They will be queued until listening is done, and sent then. You can either create two IntentServices (one for sending, one for receiving), or create a new Service that will spin up its own Threads which can send and listen concurrently.
I'd imagine that having send and receive be done by different Services would be easier.