Tell me what you were Hello, everybody, let's say on a gyrus is an example of Android in the Handler mechanism, this time we went to this example. Gossip Hugh mentioned, words Reformed turn. Let's Talk Android now!
Tell me who, we analyzed in the previous gyrus Looper
class source code that we will gyrus what Message
kind of source code, which is implemented in the frameworks/base/core/java/android/os/Message.java
file.
The main target is to create the ordinary use of the Message type, using its method is relatively small, so take a look at our main common member variables. Here is its source code, we only list the common members, other content omitted to write.
public final class Message implements Parcelable {
public int what; //用来存放消息类型
public int arg1; //用来存放int类型的消息内容
public int arg2; //用来存放int类型的消息内容
public Object obj; //用来存放Object类型的消息内容
Bundle data; //用来存放Bundle类型的消息内容
Handler target; //用来存放Handler对象
}
Tell me who, we have the role of each member by comments listed, I believe everyone can understand. Finally, we're talking about some of the considerations when using:
- If the message content is of type int arg1 and arg2 then preferably used to store the message content;
- If the message contents are various types of objects, you can use obj to store the message content, but pay attention to the type of conversion;
- If the message content is a key-value pairs, then the data may be used to store the message content, it is used in conjunction with the get / setData method. Its usage and normal class seter / same manner geter, not described in detail here.
Tell me who, with the Message
class as well as the relevant MessageQueue
category, it is mainly the background work, we treat it as Queue can be used, both to understand Handler
the mechanism, but also does not require too much attention to the details, so we no longer analysis MessageQueue
of the source code, if there Tell me who are interested, you can go your own analysis.
Tell me, on Android Handler mechanism in case we described here, and there are any examples For, Let's hear next decomposition!