Socket listener in IntentService
NickName:Raghav Ask DateTime:2015-06-11T05:06:28

Socket listener in IntentService

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

  1. 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?
  2. 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.

Copyright Notice:Content Author:「Raghav」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30767302/socket-listener-in-intentservice

Answers
David Wasser 2015-06-15T15:00:02

IntentService is the wrong choice for this behaviour. You should use a regular Service and manage the threads and connections yourself. \n\nYou can then send data from an Activity to the Service either by calling startService() with an Intent containing the data, or you can have your Activity bind to the Service and use AIDL (remote procedure calls).",


More about “Socket listener in IntentService” related questions

Socket listener in IntentService

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) { ...

Show Detail

Delayed (timed) unregister sensor listener inside IntentService (WakefulIntentService)

I've implemented an IntentService which, using SensorManager, reads data from a heart rate sensor. It gets periodically called using AlarmManager and a WakefulBroadcastReceiver and then, upon

Show Detail

It's correct to wait a socket answer in a intentService?

I've create a tcp socket with a thread and it works well. Now I need to send a request to server and to listen it. It could answer me after 100 seconds. So I've thought to use an asynctask, but 100

Show Detail

IntentService with Socket.io getting killed after clearing recents

I'm trying to make a notification app that's connected to a server via sockets. I want some service to run on the background whenever my app isn't opened by the user, similar to many email/chat app...

Show Detail

remote socket listener

I have a question. I would like to create a socket listener, but the listen address is from a remote server. So say SERVER A has the socket listener. SERVER B (ex. IP = 123.456.78.23:1970) has the

Show Detail

Socket exception in tcp listener

I trying to run this code: listener = new TcpListener(IPAddress.Any,port: 80); listener.Start(); socket = listener.AcceptSocket(); but it throws the following exception: An

Show Detail

How to stop an IntentService when it has a SensorListener running

I'm running an IntentService which has a SensorListener running. It seems impossible to stop the service from another activity within the app. Is there anyway at all to stop an IntentService from

Show Detail

IntentService : How to enqueue correctly?

In my code i'm using an IntentService to listen to location updates (either GPS or network updates) and this IntentService is triggered when an event is received, so it is started with startService()

Show Detail

Having Listeners in Service or IntentService?

I am designing a chat application with xmpp, in order to replace my AsyncTask with a better solution (which doesnt memory leak, configuration changes have no effect, manages job queue's, running more

Show Detail

python socket listener not receiving data

I am programming a decentralised script to track the IPs of other computers running the script, to explore decentralisation. This script isolates the problem. The code consists of 2 scripts, one main

Show Detail