Looper这个东东很重要,线程之间的通信全靠它,Activity能运行起来也是因为它
下面是启动时的方法调用:
Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1079
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2023
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2084
ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 130
ActivityThread$H.handleMessage(Message) line: 1195
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 4745
ThradActivity:
public static void main(String[] args) {
SamplingProfilerIntegration.start();
// CloseGuard defaults to true and can be quite spammy. We
// disable it here, but selectively enable it later (via
// StrictMode) on debug builds, but using DropBox, not logs.
CloseGuard.setEnabled(false);
Process.setArgV0("<pre-initialized>");
Looper.prepareMainLooper();
if (sMainThreadHandler == null) {
sMainThreadHandler = new Handler();
}
//这里的线程应该就是所谓的UI线程,每个线程有自己的Looper, 用Handler的sendMessage可以向Looper的
//消息队列加消息,Handler在初始化里会找到当前的线程并加与之对应的Looper的消息队列取到,
//这样其它线程就可以给它发送消息,而Looper里面的loop方法会不停的执行处理消息,这样来处理线程之间的通信
ActivityThread thread = new ActivityThread();
thread.attach(false);//这里完成部分初始化
AsyncTask.init();
if (false) {
Looper.myLooper().setMessageLogging(new
LogPrinter(Log.DEBUG, "ActivityThread"));
}
Looper.loop();//这是个死循环,它里面有个消息队列,它不停的遍历并处理消息,它会触发主Activity的加载
throw new RuntimeException("Main thread loop unexpectedly exited");
}
public void handleMessage(Message msg) {
if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
switch (msg.what) {
case LAUNCH_ACTIVITY: {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
ActivityClientRecord r = (ActivityClientRecord)msg.obj;
r.packageInfo = getPackageInfoNoCheck(
r.activityInfo.applicationInfo, r.compatInfo);
handleLaunchActivity(r, null);//处理启动Activity
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
} break;
performLaunchActivity会完成Activity的加载
Activity activity = null;
try {
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
//加载Activity
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
StrictMode.incrementExpectedActivityCount(activity.getClass());
r.intent.setExtrasClassLoader(cl);
if (r.state != null) {
r.state.setClassLoader(cl);
}
}
attach方法会完成Activity主窗口的创建
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config);
activity.mCalled = false;
mInstrumentation.callActivityOnCreate(activity, r.state);会触发Activity的onCreate事件
Activity:
mWindow = PolicyManager.makeNewWindow(this);
View之类的组件是在setContentView时才开始绘的
下面以PhoneWindow为例:
public void setContentView(View view, ViewGroup.LayoutParams params) {
if (mContentParent == null) {
installDecor();//这里才是用于显示的窗,decor它是一个FrameLayout,可以看下里面的chrild
} else {
mContentParent.removeAllViews();
}
mContentParent.addView(view, params);
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
}