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.

}

});

}

}

更多相关文章

  1. Android开发常用代码片段(一)
  2. 安卓页面技巧片段 - 1
  3. Android开发常用代码片段(二)
  4. Android有用代码片段(四)
  5. Mybatis if, set, where 动态sql和sql片段的使用
  6. 分享8个PHP开发常用代码片段
  7. jquery常用代码片段

随机推荐

  1. sqlserver Union和SQL Union All使用方法
  2. 通过SQL语句直接把表导出为XML格式
  3. sqlserver 模糊查询常用方法
  4. sql根据表名获取字段及对应说明
  5. SQL语句优化方法30例(推荐)
  6. 关于存储过程的编写的一些体会
  7. SQLSERVER 表分区操作和设计方法
  8. ASP.NET下向SQLServer2008导入文件实例操
  9. sqlserver中distinct的用法(不重复的记录
  10. 用sql脚本创建sqlserver数据库触发器范例