0

The following code has a thread that is responsible of connection to a server using a a specific socket. The idea of connecting works fine (in a separate thread). After the connection is established, I tried to update the main Activity using a Handler but it wouldn't updated!

Here is my code for the background thread:

public class SocketThread extends Thread {

private final Socket socket;
private final InputStream inputStream;
private final OutputStream outputStream;
byte[] buffer = new byte[32];
int bytes;


public SocketThread(Socket sock) {
    socket = sock;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    }
    catch (IOException e) {}
    inputStream = tmpIn;
    outputStream = tmpOut;
    EntryActivity.connected = true;
    buffer = "connect".getBytes();
    EntryActivity.UIupdater.obtainMessage(0, buffer.length, -1, buffer).sendToTarget();

}


public void run() {
    try {
        while (!Thread.currentThread().isInterrupted()) {
            bytes = inputStream.read(buffer);
            EntryActivity.UIupdater.obtainMessage(0, bytes, -1, buffer).sendToTarget();
        }
    } catch (IOException e) {
    }
}
}

And here is the handler:

static Handler UIupdater = new Handler() {
    public void handleMessage(Message msg) {
        int numOfBytes = msg.arg1;
        byte[] buffer = (byte[]) msg.obj;
        strRecieved = new String(buffer);
        strRecieved = strRecieved.substring(0, numOfBytes);
        if (strRecieved.equals("connect"))
                        // Update a TextView
            status.setText(R.string.connected);
        else 
                        // Do something else;
    }
};

I verified that the connection was established (using my Server code), but the TextView was not modified!

Malik
  • 13
  • 4

2 Answers2

0

Try using handler.sendMessage( handler.obtainMessage(...) ) instead of handler.obtainMessage(...).sendToTarget().

Since obtainMessage() retrieve a Message from the global message pool, it may be that the target is not set properly.

type-a1pha
  • 1,891
  • 13
  • 19
0

Try using below sample code. I hope it will help you.

byte[] buffer = new byte[32];
static int REFRESH_VIEW = 0;

public void onCreate(Bundle saveInstance)
{
    RefreshHandler mRefreshHandler = new RefreshHandler();

    final Message msg = new Message();
    msg.what = REFRESH_VIEW; // case 0 is calling
    final Bundle bData = new Bundle();
    buffer = "connect".getBytes();
    bData.putByteArray("bytekey", buffer);
    msg.setData(bData);
    mRefreshHandler.handleMessage(msg); // Handle the msg with key and value pair.
}

/**
 * RefreshHandler is handler to refresh the view.
 */
static class RefreshHandler extends Handler
{
    @Override
    public void handleMessage(final Message msg)
    {
        switch(msg.what)
        {
        case REFRESH_VIEW:
            final Bundle pos = msg.getData();
            final byte[] buffer = pos.getByteArray("bytekey");
            String strRecieved = new String(buffer);
            //Update the UI Part.
            status.setText("Set whatever you want. " + strRecieved);
            break;
        default:
            break;
        }
    }
};