简体   繁体   中英

Calling child thread method from parent thread

I have a Chat program (running on android) that runs a ui (Chat_client) and then creates an ChatThread that runs communication. I have managed making the client thread call a parent method (using post) but not vsv. I want the parent to call the send method on the client side after the user pushes send button. Help greatly appreciated!

//BUTTON SEND METHOD in parent class. (Chat-thread is running already)

public void send (View view) {
    //THIS IS NOT WORKING
    chatThread.sendLine(userInput.getText().toString());

}

//Method in child class

public void sendLine (String line){
        to_server.println(line);
 }

Ok so after some research I arrived at the following solution.

The child thread opens the socket and then creates another thread that reads from socket. The child thread has handler and looper (message que) to which the UI thread sends messages to send through the socket.

Voila!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM