In my program, I am receiving data via socket in an IntentService class and then sending the same as a broadcast to activity. Activity processes the input and prepares an output.
while (true) {
socket = serverSocket.accept();
//Read data from socket
//Publishing as a broadcast }
Now my question is
- How can I send the output from activity to the intentService? I know that I can fire a fresh intent to the intentService but since my while loop is already running in an infinite loop, won't this be queued and possibly never executed?
- Even if I manage to get data from activity to the intentService, how can I use the same socket connection to write back to the client or should I create a separate thread for the same?
Any insight would be helpful.
IntentService
is the wrong choice for this behaviour. You should use a regularService
and manage the threads and connections yourself.You can then send data from an Activity to the Service either by calling
startService()
with anIntent
containing the data, or you can have your Activity bind to the Service and use AIDL (remote procedure calls).