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. Android开发常用代码片段(一)
  3. Android有用代码片段(四)
  4. Android有用代码片段(四)
  5. Android(安卓)Studio 指定DEBUG和RELEASE版本打包时的APK文件名
  6. Android开发常用代码片段(二)
  7. Android有用代码片断(五)
  8. Android有用代码片段(四)
  9. Android(安卓)中常用代码片段

随机推荐

  1. Android引路蜂地图开发示例:概述
  2. android权限之一——uses-permission
  3. Android(安卓)Gallery 3D 特效精华
  4. Android(安卓)移动终端camera 防偷*拍设
  5. Debug Android(安卓)and Linux suspend a
  6. Android课设之校园二手交易app
  7. Android(安卓)AIDL服务学习笔记
  8. 实现简易的android 直播技术
  9. Android活动Acitivity启动模式之singleTo
  10. Android高手进阶教程(十一)之----Android