cantona_7
cantona_7

Reputation: 1177

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

I have socket connection established. I have service running which receives the intent from activity to start the SO Connection. I understood that socket connection should be in background thread. My socket connects properly. Now I would like to send some bytes using outputstream. This is where I am running into problem.

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();
    }
}

I have setup intent for outputMessage, whenever I access this method, I am running into Mainthread again. I don't understand it(this is a background thread, isnt it ?). How can I acccess this method in my thread ? Thanks in advance.

Edit: I access the Intent using broadcastReceiver as follows

 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();
            }
        }
    }
}

Upvotes: 0

Views: 139

Answers (1)

Youssef NAIT
Youssef NAIT

Reputation: 1530

I would suggest to call it from the run method of your thread as mentionned in the comments on your post.

There is also something weird in your code that I don't understand, why do you create a thread inside the run method. Is this something you're doing in purpuse ? Beacause this will create 2 threads each time you call start on myLocalThread.

In the code bellow I deleted the use of the second thread inside the run method and added the call the outputMessage method, I haven't tried this code so I just for illustrating purses.

Something like this

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();
    }
}

Upvotes: 1

Related Questions