We know that a normal thread doesn't maintain any message queue they runs one after another but in case we want to performs more thread then we used looper class to maintains all runnable messages in queue and runs them one by one .
Here is an example of looper class :
class MyLooper extends Thread {
private Handler mHandler;
@Override
public void run() {
Looper.prepare();
// Initialize the current thread as a looper that is used to processed message queue.
mHandler = new Handler() {
@Override
public void handleMessage(Message msgs) {
//your message here
}
};
// Run the message queue or create loop in this thread.
Looper.loop();
}
}
Note : If we are not using any looper here then it will throw exception like thread has not called Looper.prepare() .
0 Comment(s)