启动新线程冻结UI

时间:2015-10-02 18:13:16

标签: android multithreading freeze ui-thread

我正在开发一个实现Web Socket服务器的应用程序。我指的是这个图书馆 - 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");

        }

    }

}

1 个答案:

答案 0 :(得分:1)

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

 status.setText("Working inside Thread");

使用 runOnUiThread 活动方法

  runOnUiThread(new Runnable() {

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

顺便说一下,你的代码导致内存泄漏和崩溃。您无法在活动环境中开始长期的生活操作。您应该运行服务,或者在应用程序上下文中创建此线程,结果可以通过使用EventBus传递给您。