2

I've been looking at the HandlerThread class as originally I was using a simple thread and handler, but came a cropper with the NetworkOnMainThreadException in android.

I can't seem to understand how I would be able to introduce things like sockets into a HandlerThread, something where you'd put the blocking code in run. But you cannot do that with a HandlerThread since you shouldn't override run as that is where the looper is.

And I cannot call the HandlerThread via messages to start listening on sockets as it would then block any other messages passed to it.

So is this HandlerThread class designed for tasks which are non blocking? As I cannot seem to understand how I can use such a thread so it can listen on a socket but can deal with requests Like when I was creating a normal thread from the main UI thread

I could, in a normal thread, call Looper.prepare() and Looper.loop() in run, then put the code between these two lines(With a check to create the handler) but then I've got problems of communicating with this new thread, messages aren't passed into the thread.

So how do I go about creating threads which can use handlers to communicate with each other and are doing tasks such as using sockets.

Please I'm not looking for use ASyncTask comments, I want to know how I can use threads myself, so threads can communicate with each other

NiffyDroid
  • 231
  • 2
  • 10
  • I did some more testing, In a seperate project with andengine. At OnCreate I create my the main class handler using onRunUIThread, I then create the thread again using onRunUIThread. Then if that thread creates another thread, the new inner thread should call looper. It seems to be working fine, So no idea why I was getting NetworkOnMainThreadException as the new project has the same targets and on the same phone, is a bit baffling. – NiffyDroid Feb 21 '13 at 16:00

1 Answers1

1

Think of HandlerThread as a worker thread that has a Looper waiting for messages to execute (which can be spawning a new Thread). So to communicate with it, just prepare a Message object and dispatch it to the handler (e.g. you can start a new thread for socket reading ) e.g.

Handler handler = new Handler(handlerThread.getLooper()){
   @Override
   public void handleMessage(Message msg) {
      // Do action based on this message
   }
}

Typically HandlerThread is handy when you are listening to events that require a Looper e.g. requestLocationUpdates.

You do not need to use a HandlerThread to avoid NetworkOnMainThreadException (I do not know how you get this exception if you are using Thread as per your question ) and to use it with sockets, just use a normal thread and do your socket operation in the run method

iTech
  • 18,192
  • 4
  • 57
  • 80
  • NetworkOnMainThreadException comes about because in order for a normal thread to loop it needs to be updated aka a looper, hence creating and starting on the ui thread sets up the looper for you. but post honeycomb you cannot do that, so you do the looper yourself or use handler thread. Normal thread I cannot send messages to despite having its handler, handlerthread you cannot put the code in run and cannot put code in a method as it would block the thread stopping messages. So passing a message to listen on a socket would block, so passing a message to send on the socket would be blocked. – NiffyDroid Feb 20 '13 at 11:47
  • Listening to a `socket` does *not* require `Looper`, so you should do it in a normal `thread. On the other hand, sending a message on Socket can be done via `HandlerThread` because it does not block as reading operation. I am not sure what are you trying to do exactly, but I do not see why you need a `Looper` to read from `socket` – iTech Feb 20 '13 at 17:57
  • Sorry, in a normal android Activity it seems fine. However, I'm using andengine so its opengl thread, so that's what is causing the problem. – NiffyDroid Feb 20 '13 at 18:03