编程语言
首页 > 编程语言> > java – Android处理程序,执行post()不在ui线程中

java – Android处理程序,执行post()不在ui线程中

作者:互联网

从一开始我虽然Handler的方法post()和postDelayed()在与UI线程不同的线程中做了一些事情,我决定在它上面创建一个TCP Socket,但它不起作用.

我正在接收NetworkOnMainThreadException.

有没有办法执行不在UI线程中的post()和postDelayed()任务?

public class ServerHandler extends Handler {

private Socket serverSocket;   

public ServerHandler(){
    super(new MyHandlerThread("MyHandlerThread").getLooper());
}

@Override
public void handleMessage(Message msg) {
    super.handleMessage(msg);
}

public void connectServer()
{
    post(serverConnection);
}

public void writeMessage(String msg){
    try {
        serverSocket.getOutputStream().write(msg.getBytes());
        Log.d("MyLog","Message sent!");
    }
    catch (IOException io){
        Log.d("MyLog","SendMessage error");
    }
}

private Runnable serverConnection = new Runnable() {
    @Override
    public void run() {

        try {
            serverSocket = new Socket("152.168.21.24", 5001);

            Log.d("MyLog","Server connected!");

            while(true){
                Log.d("MyLog","Listening server");
                byte[] buffer = new byte[256];
                int bytesReceived = serverSocket.getInputStream().read(buffer);
                if(bytesReceived==-1) {
                    throw new IOException("Server disconnected");
                }
                else {
                  String msg = new String(buffer,0,bytesReceived);
                  Log.d("MyLog","Message received: " + msg);

                }
            }
        }

        catch (IOException io){
            Log.d("MyLog","Server connection error " + io.getMessage());
            connectServer();
        }
    }
};

}

解决方法:

Handler#post()在Looper线程上发布消息和Runnables. Handler的默认构造函数将自己绑定到创建它的Looper线程.如果它不在Looper线程上,那么它将抛出异常.因此,要在工作线程上使用Handler,必须首先创建一个线程并使用Looper循环它.然后将给定的Looper传递给Handler的构造函数.

幸运的是,已经有一个实用程序类可以为您完成大部分工作.它被称为HandlerThread.只需创建一个HandlerThread,并在需要线程的Looper时调用getLooper.

编辑:默认处理程序构造函数不选择主线程.如果它已经是Looper线程,它会选择当前线程.

标签:java,android,multithreading,android-handler
来源: https://codeday.me/bug/20190608/1197652.html