準(zhǔn)備工作
分析源碼前锡宋,為了方便在Android Studio查看源碼,打開網(wǎng)址:https://github.com/anggrayudi/android-hidden-api下載對應(yīng)的API源碼
然后將下載的源碼儡湾,替換自己SDK下對應(yīng)PAI的android.jar
然后重啟Android Studio之后查看android.jar后就可以查看源碼
Activity啟動
應(yīng)用程序的入口類是ActivityThread,在ActivityThread中有performLaunchActivity來啟動Activity执俩,這個performLaunchActivity方法內(nèi)部會創(chuàng)建一個Activity徐钠。下面查看performLaunchActivity方法:
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
····省略代碼
Activity activity = null;
try {
//反射創(chuàng)建一個Activity
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
StrictMode.incrementExpectedActivityCount(activity.getClass());
r.intent.setExtrasClassLoader(cl);
r.intent.prepareToEnterProcess();
if (r.state != null) {
r.state.setClassLoader(cl);
}
} catch (Exception e) {
if (!mInstrumentation.onException(activity, e)) {
throw new RuntimeException(
"Unable to instantiate activity " + component
+ ": " + e.toString(), e);
}
}
try {
····省略代碼
//創(chuàng)建PhoneWindow和WindowManager
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window);
if (customIntent != null) {
activity.mIntent = customIntent;
}
r.lastNonConfigurationInstances = null;
activity.mStartedActivity = false;
int theme = r.activityInfo.getThemeResource();
if (theme != 0) {
activity.setTheme(theme);
}
activity.mCalled = false;
if (r.isPersistable()) {
//調(diào)用Activity的onCreate方法
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
····省略代碼
} catch (SuperNotCalledException e) {
throw e;
} catch (Exception e) {
if (!mInstrumentation.onException(activity, e)) {
throw new RuntimeException(
"Unable to start activity " + component
+ ": " + e.toString(), e);
}
}
return activity;
}
attach方法創(chuàng)建PhoneWindow和WindowManager
跟隨attach方法來到Activity里面查看:
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Window window) {
attachBaseContext(context);
mFragments.attachHost(null /*parent*/);
//創(chuàng)建PhoneWindow
mWindow = new PhoneWindow(this, window);
mWindow.setWindowControllerCallback(this);
mWindow.setCallback(this);
mWindow.setOnWindowDismissedCallback(this);
mWindow.getLayoutInflater().setPrivateFactory(this);
if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
mWindow.setSoftInputMode(info.softInputMode);
}
if (info.uiOptions != 0) {
mWindow.setUiOptions(info.uiOptions);
}
mUiThread = Thread.currentThread();
mMainThread = aThread;
mInstrumentation = instr;
mToken = token;
mIdent = ident;
mApplication = application;
mIntent = intent;
mReferrer = referrer;
mComponent = intent.getComponent();
mActivityInfo = info;
mTitle = title;
mParent = parent;
mEmbeddedID = id;
mLastNonConfigurationInstances = lastNonConfigurationInstances;
if (voiceInteractor != null) {
if (lastNonConfigurationInstances != null) {
mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;
} else {
mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,
Looper.myLooper());
}
}
//設(shè)置WindowManager
mWindow.setWindowManager(
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
if (mParent != null) {
mWindow.setContainer(mParent.getWindow());
}
mWindowManager = mWindow.getWindowManager();
mCurrentConfig = config;
}
上面代碼里有兩處很重要,第一個就是創(chuàng)建PhoneWindow,PhoneWindow是繼承Window的,PhoneWindow就是后面用于顯示的載體,因為要在PhoneWindow上添加,刪除,更新VIew都要通過WindowManager來操作,所以看看PhoneWindow里的setWindowManager方法,很遺憾,這個方法在父類Window里面:
public void setWindowManager(WindowManager wm, IBinder appToken, String appName,
boolean hardwareAccelerated) {
mAppToken = appToken;
mAppName = appName;
mHardwareAccelerated = hardwareAccelerated
|| SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);
if (wm == null) {
wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
}
//WindowManagerImpl里創(chuàng)建WindowManager
mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
}
上面的代碼里WindowManagerImpl是WindowManager 的實現(xiàn)類,去查看createLocalWindowManager方法創(chuàng)建WindowManager :
這樣一來PhoneWindow就持有了WindowManager ,就可以操作view了.既然PhoneWindow也已經(jīng)創(chuàng)建WindowManager 也有了,下面看如何添加視圖.
添加視圖
在ActivityThread中performLaunchActivity,方法中調(diào)用Activity的onCreate方法
Activity的onCreate中的setContentView方法跟隨進(jìn)去查看:
可知getWindow就是剛剛創(chuàng)建的PhoneWindow,跟隨PhoneWindow查看setContentView
@Override
public void setContentView(int layoutResID) {
// Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
// decor, when theme attributes and the like are crystalized. Do not check the feature
// before this happens.
if (mContentParent == null) {
//1.創(chuàng)建DecorView,以及DecorView中的mContentParent 布局
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
getContext());
transitionTo(newScene);
} else {
//2,將layoutResID布局加載到mContentParent和上
mLayoutInflater.inflate(layoutResID, mContentParent);
}
mContentParent.requestApplyInsets();
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
//3通知視圖改變回調(diào)
cb.onContentChanged();
}
mContentParentExplicitlySet = true;
}
在上面代碼1中進(jìn)去查看PhoneWindow中installDecor方法
可以看到1是創(chuàng)建DecorView,2是通過findviewById找到DecorView中的mContentParent并創(chuàng)建,如此一來,布局文件加載到了DecorView上面,接下來就是顯示了,在顯示之前發(fā)現(xiàn)WindowManager并沒有添加到Window上
上面代碼在handleLaunchActivity中1performLaunchActivity是創(chuàng)建Activity,2則是要將view添加到Window
上并顯示,在handleResumeActivity中有如下方法:
1
利用WindowManager講DecorView添加到Window
2
顯示視圖,這樣view的添加和顯示都清晰了.
總結(jié)
文章寫得只是大致流程,沒有具體到每個細(xì)節(jié),也可能有的地方描述不正確,謝謝指出,大家一起進(jìn)步.