看到Handler中的dump方法:
public final void dump(Printer pw, String prefix) {
pw.println(prefix + this + " @ " + SystemClock.uptimeMillis());
if (mLooper == null) {
pw.println(prefix + "looper uninitialized");
} else {
mLooper.dump(pw, prefix + " ");
}
}
可见,如果mLooper不为空,会调用他的dump方法:
public void dump(Printer pw, String prefix) {
pw = PrefixPrinter.create(pw, prefix);
pw.println(this.toString());
pw.println("mRun=" + mRun);
pw.println("mThread=" + mThread);
pw.println("mQueue=" + ((mQueue != null) ? mQueue : "(null"));
if (mQueue != null) {
synchronized (mQueue) {
long now = SystemClock.uptimeMillis();
Message msg = mQueue.mMessages;
int n = 0;
while (msg != null) {
pw.println(" Message " + n + ": " + msg.toString(now));
n++;
msg = msg.next;
}
pw.println("(Total messages: " + n + ")");
}
}
}
这里面都是使用Printer对象pw的方法println去打印:mRun,mThread,mQueue以及queue中包含的
message个数。
看来这个dump函数只是使用了Printer对象进行了打印,打印出Handler以及Looper和Queue中的一些信息。
那么来研究一下Printer吧。
/**
* Simple interface for printing text, allowing redirection to various
* targets. Standard implementations are {@link android.util.LogPrinter},
* {@link android.util.StringBuilderPrinter}, and
* {@link android.util.PrintWriterPrinter}.
*/
public interface Printer {
/**
* Write a line of text to the output. There is no need to terminate
* the given string with a newline.
*/
void println(String x);
}
原来只是个接口,如果使用的话必须使用具体的实现类才行,那么从他的注释中看,实现类有:
1.android.util.LogPrinter
2.android.util.StringBuilderPrinter
3.android.util.PrintWriterPrinter
4.android.util.PrintStreamPrinter(注释中没有,文档中有,查看Printer的实现类,选中Printer按Ctrl+T即可查看到)
看起来LogPrinter比较脸熟,那么我们就看看这个吧。
/**
* Implementation of a {@link android.util.Printer} that sends its output
* to the system log.
*/
public class LogPrinter implements Printer {
private final int mPriority;
private final String mTag;
private final int mBuffer;
/**
* Create a new Printer that sends to the log with the given priority
* and tag.
*
* @param priority The desired log priority:
* {@link android.util.Log#VERBOSE Log.VERBOSE},
* {@link android.util.Log#DEBUG Log.DEBUG},
* {@link android.util.Log#INFO Log.INFO},
* {@link android.util.Log#WARN Log.WARN}, or
* {@link android.util.Log#ERROR Log.ERROR}.
* @param tag A string tag to associate with each printed log statement.
*/
public LogPrinter(int priority, String tag) {
mPriority = priority;
mTag = tag;
mBuffer = Log.LOG_ID_MAIN;
}
/**
* @hide
* Same as above, but buffer is one of the LOG_ID_ constants from android.util.Log.
*/
public LogPrinter(int priority, String tag, int buffer) {
mPriority = priority;
mTag = tag;
mBuffer = buffer;
}
public void println(String x) {
Log.println_native(mBuffer, mPriority, mTag, x);
}
}
代码很精简,实现了println方法,调用了Log.println_native()方法,从名字上看,肯定是native的方法。那么具体Log这个类也是我们搞android的经常用的,它的源码里面最终也是调用了这个native方法做了打印工作。原来是这样,殊途同归!这个函数定义如下:
/** @hide */ public static native int println_native(int bufID,
int priority, String tag, String msg);
hide的,我们不能在sdk中直接使用,原来如此,在jni中打印log只不过是直接使用了这个函数而已。
那么这个PrintLog怎么用呢?就这样就可以了:
Printer printer = new LogPrinter(Log.ERROR, “tag”);
// print handler and looper info , contain messages count.
// 打印出Handler和Looper对象的信息,包含Looper中的Message个数等。
myHandler.dump(printer, "DR prefix");
这样就可以打印出来了。效果不错,大家试试。