时间:2010-07-26 11:22:53

标签: android multithreading android-activity extends runnable

1 个答案:

答案 0 :(得分:1)

您应该创建一个Handler对象。每个Handler实例都与一个线程相关联,并且根据您想要关联/处理消息的线程,您应该在其上下文中创建Handler。然后,您可以将消息推送到创建Message的线程的Handler队列。随着字符串的出现,您可以使用以下内容:

public Handler handler_use = new Handler(){

    // @Override
    public void handleMessage(Message msg) {
       //Do something with the message
    }
}

在try块中:

try {
    BufferedReader in = new BufferedReader(new  InputStreamReader(client.getInputStream()));
    String str = in.readLine();
    if(str.equals("test")) {
        //Construct the message Msg - based on the string
        handler_use.sendMessage(Msg);
    }
}