android.os.handler
/**
1. handler is associated with a thread and thread's message queue
* A Handler 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 and that thread's message
* queue. When you create a new Handler, it is bound to the thread /
* message queue of the thread that is creating it -- from that point on,
* it will deliver messages and runnables to that message queue and execute
* them as they come out of the message queue.
*
2. handler的使用者:发送message and 处理message
* There are two main uses for a Handler:
* (1) to schedule messages and runnables to be executed as some point in the future;
* (2) to enqueue an action to be performed on a different thread than your own.
*
3. scheduling message有两个版本
* Scheduling messages is accomplished with the
* {post}, {postAtTime(Runnable, long)},
* {postDelayed}, {endEmptyMessage},
* {sendMessage}, {sendMessageAtTime}, and
* {sendMessageDelayed} methods.
3.1 post versions (Runnable object使用是post?)
* The post versions allow
* you to enqueue Runnable objects to be called by the message queue when
* they are received;
*/
3.2 sendMessage versions (Message object使用sendMessage, 需要实现对应的HandleMessage)
* The sendMessage 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).
3.3 处理item的时间是可控的:立即执行或延时等
* When posting or sending to a Handler, you can either
* allow the item to be processed as soon as the message queue is ready
* to do so, or specify a delay before it gets processed or absolute time for
* it to be processed. The latter two allow you to implement timeouts,
* ticks, and other timing-based behavior.
4. 使用handler和主线程通信 (所说的通信就是发送消息到Handler 如下)
* When a process is created for your application, its main thread is dedicated to
* running a message queue that takes care of managing the top-level
* application objects (activities, broadcast receivers, etc) and any windows
* they create. You can create your own threads, and communicate back with
* the main application thread through a Handler.
*/
主线程收到的 Intent -> 调用 Handler到子线程进一步处理
android.os.Looper
/** 包括唤醒、睡眠等控制
* Class used to run a message loop for a thread. Threads by default do
* not have a message loop associated with them; to create one, call
* {@link #prepare} in the thread that is to run the loop, and then
* {@link #loop} to have it process messages until the loop is stopped.
*
* <p>Most interaction with a message loop is through the
* {@link Handler} class.
*/
adroid.os.Message
public final class Message implements Parcelable {
/**
* User-defined message code so that the recipient can identify
* what this message is about. Each Handler has its own name-space
* for message codes, so you do not need to worry about yours conflicting
* with other handlers.
*/
public int what;
/**
* arg1 and arg2 are lower-cost alternatives to using
* {setData(Bundle) setData()} if you only need to store a
* few integer values.
*/
public int arg1;
public int arg2;
/**
* An arbitrary object to send to the recipient. When using
* {Messenger} to send the message across processes this can only
* be non-null if it contains a Parcelable of a framework class (not one
* implemented by the application). For other data transfer use
* {setData}.
*/
public Object obj;
/** 回复该 message
* Optional Messenger where replies to this message can be sent. The
* semantics of exactly how this is used are up to the sender and
* receiver.
*/
public Messenger replyTo;
/*package*/ long when;
/*package*/ Bundle data;
/*package*/ Handler target;
/*package*/ Runnable callback;
}
public void setData(Bundle data) { this.data = data; }
那 Bundle是什么?
A mapping from String keys to various {@link Parcelable} values.
Messenger: Handler
/** 在server创建指向Handler的Messenger,在client端发送Messager
* Reference to a Handler, which others can use to send messages to it.
* This allows for the implementation of message-based communication across
* processes, by creating a Messenger pointing to a Handler in one process,
* and handing that Messenger to another process.
*
* Note: the implementation underneath is just a simple wrapper around
* a Binder that is used to perform the communication. This means
* semantically you should treat it as such: this class does not impact process
* lifecycle management (you must be using some higher-level component to tell
* the system that your process needs to continue running), the connection will
* break if your process goes away for any reason
*/