Android send received data from a socket service to Activity
<p>I am looking for a way to send received data (receive while flag is set) from a socket service to an activity that is bound to the service. What is the best way to do this ? Handlers , AsyncTask or something else ?</p>

<p>this is the SocketClass I am using to start a connection and handle the incoming messages :</p>

<pre><code>    class connectSocket implements Runnable {

    @Override
    public void run() {

        running= true;
        try {
            socket = new Socket(serverAddr, SERVERPORT);
            try {
                mBufferOut = new ObjectOutputStream(socket.getOutputStream());
                mBufferIn = new ObjectInputStream(socket.getInputStream());
                while(running){
                     msg = (Message) mBufferIn.readObject();
                }

            }
            catch (Exception e) {
                Log.e("TCP", "S: Error", e);
            }
        } catch (Exception e) {
            Log.e("TCP", "C: Error", e);
        }

    }

}
</code></pre>

<p><strong>Update :</strong></p>

<pre><code>public class SocketService extends Service {
private static final String TAG = "com.oos.kiesa.chat";
public static final String SERVERIP = "192.168.X.X";
public static final int SERVERPORT = 4444;
private ObjectOutputStream mBufferOut;
private ObjectInputStream mBufferIn;
Socket socket;
InetAddress serverAddr;
boolean mRun;
Object msg;
public Integer command;
@Override
public IBinder onBind(Intent intent) {
    return myBinder;
}

private final IBinder myBinder = new LocalBinder();

public class LocalBinder extends Binder {
    public SocketService getService() {
        return SocketService.this;

    }
}

@Override
public void onCreate() {
    super.onCreate();
}

public void IsBoundable(){
    Toast.makeText(this,"is boundable", Toast.LENGTH_LONG).show();
}


public void sendMessage(String message) throws IOException {
    if (mBufferOut != null) {
        mBufferOut.writeObject(message);
        mBufferOut.flush();
    }
}

public void sendMessage(Message message) throws IOException {
    if (mBufferOut != null) {
        mBufferOut.writeObject(message);
        mBufferOut.flush();
    }
}

public void sendMessage(User user) throws IOException {
    if (mBufferOut != null) {
        mBufferOut.writeObject(user);
        mBufferOut.flush();
    }
}

public void sendMessage(int command) throws IOException {
    if (mBufferOut != null) {
        mBufferOut.writeInt(command);
        mBufferOut.flush();
    }
}


@Override
public int onStartCommand(Intent intent,int flags, int startId){
    super.onStartCommand(intent, flags, startId);
    System.out.println("I am in on start");
    Runnable connect = new connectSocket();
    new Thread(connect).start();
    return START_STICKY;
}

public void stopClient() throws IOException{

    if (mBufferOut != null) {
        mBufferOut.flush();
        mBufferOut.close();
    }

    mBufferIn = null;
    mBufferOut = null;
    socket.close();
}

class connectSocket implements Runnable {

    @Override
    public void run() {

        mRun = true;
        try {
            socket = new Socket(serverAddr, SERVERPORT);
            try {
                mBufferOut = new ObjectOutputStream(socket.getOutputStream());
                mBufferIn = new ObjectInputStream(socket.getInputStream());
                while(mRun){
                    switch(command.intValue()){
                        case Constants.ADD_MESSAGE:
                            msg = (Message) mBufferIn.readObject(); // send message to Activity 
                            break;
                    }
                }

            }
            catch (Exception e) {
                Log.e("TCP", "S: Error", e);
            }
        } catch (Exception e) {
            Log.e("TCP", "C: Error", e);
        }

    }

}
@Override
public void onDestroy() {
    super.onDestroy();
    try {
        socket.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    socket = null;
}
</code></pre>

<p>}</p>

<p>Some guidance / examples would be greatly appreciated </p>