【问题标题】:how to update interface to MainActivity from thread activity in different class?如何从不同类中的线程活动更新 MainActivity 的接口?
【发布时间】:2012-11-06 05:33:38
【问题描述】:

现在我正在建立套接字连接以将多个客户端连接到一台服务器。一切都很好,我在不同的班级做线程。但是当我用线程活动中的字符串填充 textView 时,我不能。请帮忙

这个 MainActivity:

公共类 MainActivity 扩展 Activity {

private static int port = 6000;
TextView txt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt = (TextView) findViewById(R.id.txtCommand);
    txt.setText("Server : ");
    ServerSocket server1 = null;
    Server gameServer = new Server();
    try {
        server1 = new ServerSocket(port);
        // .. server setting should be done here
    } catch (IOException e) {
        System.out.println("Could not start server!");
       // return ;
    }

    while (true) {
        Socket client = null;
        try {
            client = server1.accept();
            gameServer.handleConnection(client);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class Server  {
    private ExecutorService executor = Executors.newCachedThreadPool();

    public void handleConnection(Socket client) throws IOException {    
        PlayerConnection newPlayer = new PlayerConnection(this, client);
        txt.setText(newPlayer.getuname());
        this.executor.execute(newPlayer);
    }

    // add methods to handle requests from PlayerConnection
}

还有这个线程活动:

公共类 PlayerConnection 实现 Runnable { 私有服务器父级;

private Socket socket;
private PrintWriter out;
private BufferedReader in;
String line;

protected PlayerConnection(Server parent, Socket socket) throws IOException {
    this.parent = parent;
    this.socket = socket;

    this.in = new BufferedReader(new InputStreamReader(socket
            .getInputStream()));
    this.out = new PrintWriter(new OutputStreamWriter(
            socket.getOutputStream()));     
}

public void run() {
    while(!this.socket.isClosed()) {                        
        try {
            //int nextEvent = this.in.readInt();
            line = in.readLine();
            System.out.println("Server Receive : "
                    + line);
            out.println("Server Sent :" +line);
            System.out.println("SEND : "
                    + line);
            out.flush();
             if (line == null){
                 this.socket.isClosed();
                 break;
             }
        } catch (IOException e) {}
    }

【问题讨论】:

  • 你能发布一些你的代码吗?我可能要问你是否使用内部类来确定是否可以访问要填充的 textView。谢谢

标签: android


【解决方案1】:

我意识到您有一个永远不会在 onCreate 方法中结束的 while 循环。尝试将该循环移动到您的线程中。此循环会阻止您的应用程序的输入。

while (true) {
    Socket client = null;
    try {
        client = server1.accept();
        gameServer.handleConnection(client);
    } catch (IOException e) {
        e.printStackTrace();
    }

【讨论】:

    【解决方案2】:

    你可以使用 handler 或 runOnUiThread 方法。

    【讨论】:

      猜你喜欢
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多