hi mate in all tutorial or example the handler used in a looper is created inside the looper example:
class LooperThread extends Thread {
public Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop();
}
}
but if a create the handler before the thread, at example
public Handler mHandler=new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
is possible associate him at the thread in run() method in a looper or i must create him only in run method inside the looper ?
The constructor in
Handlerbasically looks like this:The field,
mLooperis a package-privatefinalfield – and, of course, there’s no setter available for you to change it later on.In short, the
Handler()constructor will associate itself withLooper.myLooper(), there are other constructors that allow you to pass theLooper– but these would also require you to create theLooperprior to creating yourHandler.