启动新线程冻结 UI

Starting new Thread freezes UI

我正在开发一个实现 Web 套接字服务器的应用程序。我指的是这个图书馆 - https://github.com/TooTallNate/Java-WebSocket

问题是线程撑起了整个UI。这是代码 -

package com.example.websocket;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Collections;

import java.nio.ByteBuffer;

import org.java_websocket.WebSocket;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.framing.FrameBuilder;
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import android.os.Bundle;
import android.provider.Settings.Global;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    EditText port, msg;
    Button listener, send;
    TextView status;
    int p;
    int count = 0;
    boolean connect = false;
    boolean listen = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        msg = (EditText)findViewById(R.id.editText2);
        listener = (Button)findViewById(R.id.button1);
        send = (Button)findViewById(R.id.button2);
        status = (TextView)findViewById(R.id.textView1);

        listener.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            SimpleServer server = new SimpleServer();
                            server.start();
                            status.setText("Working inside Thread");
                        } catch (UnknownHostException e) {
                            e.printStackTrace();
                        }
                    }   
                });

                t.start();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public class SimpleServer extends WebSocketServer {

        public SimpleServer() throws UnknownHostException {
            super(new InetSocketAddress(9998));
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onClose(WebSocket arg0, int arg1, String arg2, boolean arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onError(WebSocket arg0, Exception arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onMessage(WebSocket arg0, String arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onOpen(WebSocket arg0, ClientHandshake arg1) {
            status.setText("Working");

        }

    }

}

您无法从其他线程更新 ui:

 status.setText("Working inside Thread");

使用运行OnUiThread方法activity

  runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        status.setText("Working inside Thread");
                    }
                });

顺便说一下,您的代码会导致内存泄漏和崩溃。您不能在 activity 上下文中开始长期运行的操作。您应该 运行 服务,或者在应用程序上下文中创建此线程,结果 ui 您可以使用 EventBus 传递。