如何在后台线程中执行 运行() 之外的方法?

How to execute a method outside of run() in a backgroud thread?

我已建立套接字连接。我有服务 运行,它从 activity 接收到启动 SO Connection 的意图。我知道 socket 连接应该在后台线程中。我的插座连接正确。现在我想使用 outputstream 发送一些字节。这就是我 运行 遇到问题的地方。

public class myLocalThread extends Thread {

    private InetAddress serverAddress;
    private int serverPort;

    Socket clientSocket;
    BufferedInputStream inputStream;
    BufferedOutputStream outputStream;

    @Override
    public void run(){

        try {
            clientSocket = new Socket(serverAddress, serverPort);
            outputStream = new BufferedOutputStream(clientSocket.getOutputStream());
            inputStream = new BufferedInputStream(clientSocket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Start thread for receiving messages
        new Thread() {
            @Override
            public void run() {

                int bytes_read = 0;
                byte[] buffer = new byte[1024];

                while(true) {
                    try {
                        if ((bytes_read = inputStream.read(buffer, 0, buffer.length)) > 0){

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
           }
        }.start();
    }

  public void outputMessage(byte[] message) throws IOException {
        outputStream.write(new byte[]{
                (byte) 0x01
        });
        outputStream.flush();
    }
}

我有 outputMessage 的设置意图,每当我访问此方法时,我都会再次 运行 进入 Mainthread。我不明白(这是后台线程,不是吗?)。我怎样才能在我的线程中访问这个方法?提前致谢。

编辑:我按如下方式使用 broadcastReceiver 访问 Intent

 public class MyReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        myLocalThread thread;
        String action = intent.getAction();

        if(action.equals("com.example.app.START")){
         thread = new myLocalThread();
         thread.start(); 
        } else if (action.equals("com.example.app.SEND")){
            try {
                thread.sendMessage(new byte[]{
                        (byte) 0x02
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

我建议从您的线程的 run 方法调用它,正如您在 post 的评论中提到的那样。

您的代码中还有一些我不明白的奇怪之处,为什么要在 run 方法内部创建一个线程。这是你的目的吗?因为每次您在 myLocalThread 上调用 start 时都会创建 2 个线程。

在下面的代码中,我删除了 运行 方法中第二个线程的使用,并添加了调用 outputMessage 方法,我没有尝试过这段代码,所以我只是为了说明钱包。

像这样

public class myLocalThread extends Thread {

    private InetAddress serverAddress;
    private int serverPort;

    Socket clientSocket;
    BufferedInputStream inputStream;
    BufferedOutputStream outputStream;

    @Override
    public void run(){

        try {
            clientSocket = new Socket(serverAddress, serverPort);
            outputStream = new BufferedOutputStream(clientSocket.getOutputStream());
            inputStream = new BufferedInputStream(clientSocket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }


        int bytes_read = 0;
        byte[] buffer = new byte[1024];

        while(true) {
             try {
                   if ((bytes_read = inputStream.read(buffer, 0, buffer.length)) > 0){

                   }
              } catch (IOException e) {
                  e.printStackTrace();
              }
        }
        outputMessage(new byte[]{(byte) 0x01});

    }

    public void outputMessage(byte[] message) throws IOException {
        outputStream.write(message);
        outputStream.flush();
    }
}