android.jar 源代码问题
小弟 刚学今天看到 Looper的源码
有几个问题 不明白 想请教大家一下
这个是 Looper的 loop方法
public static void loop() {
Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
MessageQueue queue = me.mQueue;
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
while (true) {
Message msg = queue.next(); // might block
if (msg != null) {
if (msg.target == null) {
// No target is a magic identifier for the quit message.
return;
}
long wallStart = 0;
long threadStart = 0;
// This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
wallStart = SystemClock.currentTimeMicro();
threadStart = SystemClock.currentThreadTimeMicro();
}
msg.target.dispatchMessage(msg);
if (logging != null) {
long wallTime = SystemClock.currentTimeMicro() - wallStart;
long threadTime = SystemClock.currentThreadTimeMicro() - threadStart;
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
if (logging instanceof Profiler) {
((Profiler) logging).profile(msg, wallStart, wallTime,
threadStart, threadTime);
}
}
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
}
msg.recycle();
}
}
}
问题1 msg.target.dispatchMessage(msg);
你看这句 msg是个 Message对象 Message类里面 target是个私有变量
这里何以能访问到? --------------------编程问答--------------------
学习,LOOPER干吗用的 --------------------编程问答--------------------
楼主干了三年以上程序员吧 --------------------编程问答-------------------- 貌似这样:
Message 类的成员 target 在定义的时候没有加 public protected private 限定,
这样同一个包 android.os 中的其他类就可以访问该成员,这叫做【友好】。 --------------------编程问答-------------------- 就像楼上说的,解类似c++的友元 --------------------编程问答-------------------- 你看这句 msg是个 Message对象 Message类里面 target是个私有变量
????
谁说target是私有变量了?在Message.java中
/*package*/ Handler target;
没有加修饰符,就是protected,包内可访问
补充:移动开发 , Android