1、Message相关方法
1-1、Message相关成员变量
每个 Message 都持有 Handler 实例。如果 Handler 持有Activity的引用,Activity onDestroy 后 Message 却仍然在队列中,因为 Handler 与Activity的强关联,会造成 Activity 无法被 GC 回收,导致内存泄露。 因此在Activity onDestroy 时,与Activity关联的Handler应清除它的队列由Activity产生的任务,避免内存泄露
/**
* 储存消息数据
*/
public int what;
public int arg1;
public int arg2;
public Object obj;
/**
* 每个消息持有Handler实例
*/
Handler target;
/**
* 消息自带的callback
*/
Runnable callback;
/**
* 下一个消息,所有消息组成一个链表
*/
android.os.Message next;
1-2、回收消息
/**
* 释放Message避免引用Handler导致内存泄漏
*/
public void recycle() {
if (isInUse()) {
if (gCheckRecycle) {
throw new IllegalStateException("This message cannot be recycled because it "
+ "is still in use.");
}
return;
}
recycleUnchecked();
}
void recycleUnchecked() {
// Mark the message as in use while it remains in the recycled object pool.
// Clear out all other details.
flags = FLAG_IN_USE;
what = 0;
arg1 = 0;
arg2 = 0;
obj = null;
replyTo = null;
sendingUid = UID_NONE;
workSourceUid = UID_NONE;
when = 0;
target = null;
callback = null;
data = null;
synchronized (sPoolSync) {
if (sPoolSize < MAX_POOL_SIZE) {
next = sPool;
sPool = this;
sPoolSize++;
}
}
}
1-3、创建消息
获取实例最好的办法是调用Message.obtain()或Handler.obtainMessage()。这样可以从他们的可回收对象池中获取到消息实例
/**
* 从回收对象池中获取一个消息实例
* @return
*/
public static android.os.Message obtain() {
synchronized (sPoolSync) {
if (sPool != null) {
android.os.Message m = sPool;
sPool = m.next;
m.next = null;
m.flags = 0; // clear in-use flag
sPoolSize--;
return m;
}
}
return new android.os.Message();
}
public static android.os.Message obtain(Handler h) {
android.os.Message m = obtain();
m.target = h;
return m;
}
public static android.os.Message obtain(Handler h, Runnable callback) {
android.os.Message m = obtain();
m.target = h;
m.callback = callback;
return m;
}
1-4、发送消息
Message自己也有发送消息的方法,但其实还是调用了Handler的发送消息方法
/**
* 发送消息 target其实就是Handler
*/
public void sendToTarget() {
target.sendMessage(this);
}
2、Message简化源码
``` public final class Message implements Parcelable {
/**
储存消息数据 */ public int what; public int arg1; public int arg2; public Object obj;
/**
每个消息持有Handler实例 */ Handler target;
/**
消息自带的callback */ Runnable callback;
/**
- 下一个消息,所有消息组成一个链表 */ android.os.Message next;
/**
* 从回收对象池中获取一个消息实例
* @return
*/
public static android.os.Message obtain() {
synchronized (sPoolSync) {
if (sPool != null) {
android.os.Message m = sPool;
sPool = m.next;
m.next = null;
m.flags = 0; // clear in-use flag
sPoolSize--;
return m;
}
}
return new android.os.Message();
}
public static android.os.Message obtain(Handler h) {
android.os.Message m = obtain();
m.target = h;
return m;
}
public static android.os.Message obtain(Handler h, Runnable callback) {
android.os.Message m = obtain();
m.target = h;
m.callback = callback;
return m;
}
/**
* 释放Message避免引用Handler导致内存泄漏
*/
public void recycle() {
if (isInUse()) {
if (gCheckRecycle) {
throw new IllegalStateException("This message cannot be recycled because it "
+ "is still in use.");
}
return;
}
recycleUnchecked();
}
void recycleUnchecked() {
// Mark the message as in use while it remains in the recycled object pool.
// Clear out all other details.
flags = FLAG_IN_USE;
what = 0;
arg1 = 0;
arg2 = 0;
obj = null;
replyTo = null;
sendingUid = UID_NONE;
workSourceUid = UID_NONE;
when = 0;
target = null;
callback = null;
data = null;
synchronized (sPoolSync) {
if (sPoolSize < MAX_POOL_SIZE) {
next = sPool;
sPool = this;
sPoolSize++;
}
}
}
/**
* 发送消息 target其实就是Handler
*/
public void sendToTarget() {
target.sendMessage(this);
}
} ```