Why use HandlerThread in Android

Daniel picture Daniel · May 23, 2012 · Viewed 15.5k times · Source

In android , Handler can be used to post / handle message, if I don't use a HandlerThread (pass its Looper to Handler), does that mean in this case Handler use MainThread (UI Thread) 's Looper ?

What result will get if Handler uses MainThread's Looper ? May cause mainThread blocked ?

Answer

oznus picture oznus · Oct 13, 2013

You would use HandlerThread in case that you want to perform background tasks one at a time and you want that those tasks will run at the order of execution.

For example if you want to make several network background operations one by one.

Yes, the HandlerThread has it's own looper and handlers could be created and post it, (so it would not block the main thread).