Stephen
Stephen

Reputation: 762

Posting a Message to a running Thread using Handlers

I have code which calls a new Thread that connects to an IRC server. The thread has loop to listen for response from the IRC server and calls a method 'ProcessData' to action the response.

On my UI I want to be able to 'QUIT' the IRC server in onStop and onPause. The trouble I have is that when I use a Handler to post a message to my IRC thread which sends a QUIT command to the IRC server it tells me that I am performing network operations on the UI thread.

The handler is setup in my IRCManager class (this class extends Thread and is the class I run on a separate thread.

public Handler networkHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {

        try {
            processData((String) msg.obj);
        } catch (IOException e) {
            Log.e(TAG, "network handler given an object NOT of type String");
        }
        super.handleMessage(msg);
    }

};

I use the handler from the main activity and instantiate it just after starting the network thread

irc.start();
networkHandler = irc.networkHandler;

In the onPause event I send a message to the handler

Message msg = new Message();
msg.obj = IRCManager.QUIT;
networkHandler.sendMessage(msg);

EDIT: Here is the processData method

void processData(String data) throws IOException {

    if (data.contains("PING")) {
        String pingId = data.substring(6, data.length());
        sendMessage(pong + pingId + "\n");
        isConnected = true;

        Message msg = new Message();
        msg.what = 1;
        msg.obj = "test";
        handler.sendMessage(msg);

    } else if (data.contains("Welcome")) {
        sendMessage("PRIVMSG " + BOT_NAME + " JOIN " + siteId + "\n");

    } else if (data.contains(IRCManager.QUIT)) {
        disconnect();

    } else if (isClientConnected()) {
        Message msg = new Message();
        msg.what = 2;
        msg.obj = "test";
        handler.sendMessage(msg);
    }
}

It seems that the handler isn't linking properly to the thread. Can anyone shed any light on how I can do this?

My thread actually spends 99% of it's time in a while loop checking the inputstream from the IRC server. This may also have something to do with it.

Upvotes: 0

Views: 156

Answers (2)

Trevor
Trevor

Reputation: 10993

You're creating the instance of the Handler, networkHandler, here:

public Handler networkHandler = new Handler() {

It'll be therefore associated with the UI thread.

And, when you say:

I use the handler from the main activity and instantiate it just after starting the network thread

irc.start();
networkHandler = irc.networkHandler;

You're not creating the instance of the Handler there; you're just grabbing a reference to it.

You actually need to create the instance of the Handler in the run() method of your non-UI Thread.

Upvotes: 2

Yahor10
Yahor10

Reputation: 2132

Try to use another constructor with new Handler(new Handler.Callback() ) inside

Upvotes: 1

Related Questions