Android MessageQueue Mechanism

来源:互联网 发布:淘宝修改中差评 编辑:程序博客网 时间:2024/05/01 17:03

Android MessageQueue Mechanism

Core classes:

    Handler – the operator, user apps use this to operate the  Messages: send messagewith postRunnable, sendMessage etc. and process Messages in handleMessage.

   Message – the container for the data andinformation

   MessageQueue – the internal containers forMessages. User apps are not allowed to operate it directly. It is operatedinternally by Handler

   Looper – the rules which let thread rununtil stop() is called. This is much like use a while (true) in the thread tomake it run as long as you wish

   The general Message processing would be like this:

        1. make the thread (either main thread or user-created thread) can loop,with calling Looper.prepare();

        2. Send the Message with Handler.

        3. OverrideHandler.handleMessage to process the Message;

        4. Stop the looper by callingLooper.stop(), then thread will exit.