I see a lot examples in code were we see the next thing:
HandlerThread thread = new HandlerThread("Thread1");
thread.start();
mLoadHandler = new Handler(thread.getLooper())
mLoadHandler.post(new Runnable() {
public void run() {
// run some code
//methodA();
}
});
you can find it also in:
Why they create a HandlerThread
for only one purpose: to pass it's Looper
to a new Handler
. Why don't just extend the HandlerThread
and do the all code (methodA()
) there? OR create a Handler instance and call there to:
Looper.prepare();
Looper.loop();
If you want to get the Handler which is associated with the ThreadHandler class you create, you must pass the Looper associated with that ThreadHadler, otherwise the Handler will be associated to the current thread in which it was instantiated. The purpose of creating such a HandlerThread is generally when you want to do same type of job multiple times, you can simply use this Handler object to post a message to the new thread.
I think your question is why would you want to create an inner class rather than implement a separate independent class. If you are creating an inner class then you can access directly members of the the containing class. If you implement a separate class, you would need to pass the required variables/view values to the new class.