Talking about Android Message Queue

Talking about Android Message Queue

Android does not implement a global message queue to allow cross-process communication through message like Windows. Actually if you need cross-process communication, the official method in Android is intent. Android only supports in-process communication through message.

I wonder in the feature Android will add supporting cross-process message, because you can see some code snippet(片段) for IMessager in Messager.java and Handler.java. But you have no way to get an instance of IBinder proxy(代理人) object for IMessager.

Looper.java is the class used to run a MessageQueue for a thread. Threads by default do not have a MessageQueue associated with them. To create one, the following code shows a typical example of the implementation of a Looper thread and an initial Handler to communication with the Looper. One thread can have only one Looper instance.

// Example 1:

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();

    }

}

When a process is created for your JAVA application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, intent receivers, etc) and any windows they create, so that you don’t need to create a Looper object for the main thread. Here shows the code snippets in ActivityThread.java which creates the Looper object for the main thread.

    public static final void main(String[] args) {

        Looper.prepareMainLooper();

        ActivityThread thread = new ActivityThread();

        thread.attach(false);

        Looper.loop();

        if (Process.supportsProcesses()) {

            throw new RuntimeException("Main thread loop unexpectedly exited");

        }

        thread.detach();

    }

Handler.java allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread. When you create a new Handler, it is bound to the thread that is creating it – from that point on, it will deliver messages and runnables to that thread’s message queue and execute them as they come out of the message queue.

Scheduling message APIs in Handler can be divided into two types: send and post. The send versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler’s handleMessage method (requiring that you implement a subclass of Handler). Please refer to the previous Example 1 as an example. The post versions allow you to enqueue a Runnable object to be called by the message queue when it is received. The following code snippets show an example.

// Example 2:

class MyActivity extends Activity {

    Handler mHandler = new Handler();

    public void XXX() {

        // post a runnable object to run on main message queue

mHandler.post(new Runnable() {

            public void run() {

                // now it’s run on the thread corresponding to mHandler.

            }

        });

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值