1.7 ActivityManagerService
1.7.1 启动流程
- startBootstrapService[SystemServer.java启动], (一)设置SystemServiceManager, (二)设置AMS的Installer, (三)初始化AMS相关的PMS, (四)设置SystemServer
- AMS创建, 创建ActivityManager前台进程, 创建UiThread, 创建前台后台广播接收器, 创建CPUTracker
- AMS#start -> AMS#setSystemProcess, 注册meminfo等各种服务
- ActivityThread#installSystemApplicationInfo, 最后调用LoadedApk的installSystemApplicationInfo
- startSystemUi, 启动homeActivity
- 调用一系列服务的systemReady
1.7.2 startActivity流程
- 流程
Activity#startActivityForResult()
->Instrumentation#execStartActivity()
->ActivityManagerNative#getDefault()#startActivity()
,ActivityManagerNative#getDefault()
即ActivityManagerProxy
->ActivityManagerProxy#startActivity()
->mRemote#transact(START_ACTIVITY_TRANSACTION, )
ActivityManagerProxy
经过binder IPC ->ActivityManagerNative#onTransact()
,case START_ACTIVITY_TRANSACTION
, ->ActivityManagerService#startActivity()
->ActivityStackSupervisor#startActivityMayWait()
其中startActivityMayWait()
包含ActivityStackSupervisor#resolveActivity()
收集intent所指向Activity信息, 多个可选则弹窗 ->AppGlobals#getPackageManager()#resolveIntent()
->PackageManagerService#resolveIntent()
->PackageManagerService#queryIntentActivities()
, 找到Activity并保存到Intent对象startActivityLocked()
包含创建ActivityRecord
startActivityUncheckedLocked()
找到新Activity所属的Task对象 ->ActivityStack#startActivityLocked()
->ApplicationThreadProxy#scheduleLaunchActivity()
经过Binder IPC ->ApplicationThreadNative#onTransact()
->ApplicationThread#scheduleLaunchActivity()
通过handler发送LAUNCH_ACTIVITY消息 ->ActivityThread#handleLaunchActivity()
回调Activity#onCreate()
等方法
- 流程图(引用Gityuan博客图)
AMS & PMS & WMS流程
- 直接看
ActivityThread#handleLaunchActivity
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
...
// 实例化Activity, 并调用Activity#onCreate
Activity a = performLaunchActivity(r, customIntent);
if (a != null) {
handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed);
}
...
}
ActivityThread#performLaunchActivity
, 实例化并并启动Activity, 并调用Activity#attach
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
...
// 创建实例化Activity
activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
...
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);
...
// 调用Activity#performCreate();
if (r.isPersistable()) {
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
...
}
Activity#attach
, 创建PhoneWindow
, 并设置WindowManagerImpl
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) {
mWindow = new PhoneWindow(this);
...
mWindow.setWindowManager( (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
...
}
ActivityThread.handleResumeActivity
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward, boolean reallyResume) {
// 调用Activity#onResume
ActivityClientRecord r = performResumeActivity(token, clearHide);
...
final Activity a = r.activity;
ViewManager wm = a.getWindowManager();
View decor = r.window.getDecorView();
...
if (a.mVisibleFromClient) {
a.mWindowAdded = true;
wm.addView(decor, l);
}
...
r.activity.makeVisible();
...
}
Activity.makeVisible
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager();
// 见6
wm.addView(mDecor, getWindow().getAttributes());
}
}
WindowManagerImpl.addView
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
// 见7
mGlobal.addView(view, params, mDisplay, mParentWindow);
}
WindowManagerGlobal.addView
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
...
// 见8
ViewRootImpl root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
// 见12
root.setView(view, wparams, panelParentView);
...
}
ViewRootImpl
publiv ViewRootImpl(Context context, Display display) {
mContext = context;
// 获取IWindowSession代理类
// 见9
mWindowSession = WindowManagerGlobal.getWindowSession();
mDisplay = display;
mThread = Thread.currentThread();
// 见11
mWindow = new W(this);
mChoreographer = Choreographer.getInstance();
...
}
WindowManagerGlobal.getWindowSession
public static IWindowSession getWindowSession() {
synchronized (WindowManagerGlobal.class) {
if (sWindowSession == null) {
try {
// 获取IMS的代理类
InputMethodManager imm = InputMethodManager.getInstance();
// 获取WMS的代理类
IWindowManager windowManager = getWindowManagerService();
// 经过Binder调用, 最终调用WMS
sWindowSession = windowManager.openSession(new IWindowSessionCallback.Stub(){...}, imm.getClient(), imm.getInputContext());
} catch (RemoteException e) {
...
}
}
}
return sWindowSession;
}
WindowManagerGlobal.openSession
public IWindowSession openSession(IWindowSessionCallback callback, IInputMethodClient client, IInputContext inputContext) {
// 创建Session对象
Session session = new Session(this, callback, client, inputContext);
return session;
}
ViewRootImpl::W
static class W extends IWindow.Stub {
private final WeakReference<ViewRootImpl> mViewAncestor;
private final IWindowSession mWindowSession;
W(ViewRootImpl viewAncestor) {
mViewAncestor = new WeakReference<ViewRootImpl>(viewAncestor);
mWindowSession = viewAncestor.mWindowSession;
}
...
}
ViewRootImpl.setView
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
...
res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(),
mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
mAttachInfo.mOutsets, mInputChannel);
...
}
Session.addToDisplay
final class Session extends IWindowSession.Stub implements IBinder.DeathRecipient {
public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs, int viewVisibility, int displayId, Rect outContentInsets, Rect outStableInsets, Rect outOutsets, InputChannel outInputChannel) {
// 见14
return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
outContentInsets, outStableInsets, outOutsets, outInputChannel);
}
}
WindowManagerService.addWindow
public int addWindow(Session session, IWindow client, int seq,
WindowManager.LayoutParams attrs, int viewVisibility, int displayId,
Rect outContentInsets, Rect outStableInsets, Rect outOutsets,
InputChannel outInputChannel) {
...
WindowToken token = mTokenMap.get(attrs.token);
WindowState win = new WindowState(this, session, client, token, attachedWindow, appOp[0], seq, attrs, viewVisibility, displayContent);
...
//调整WindowManager的LayoutParams参数
mPolicy.adjustWindowParamsLw(win.mAttrs);
res = mPolicy.prepareAddWindowLw(win, attrs);
addWindowToListInOrderLocked(win, true);
// 设置input
mInputManager.registerInputChannel(win.mInputChannel, win.mInputWindowHandle);
//
win.attach();
mWindowMap.put(client.asBinder(), win);
...
}
备注
- 在
ActivityStackSupervisor#startActivityLocked()
, 创建ActivityStack
和ActivityRecord
- 在
ActivityStackSupervisor#startActivityUncheckedLocked()
, 处理launchMode
- 进程启动
ActivityManagerService#startProcessLocked()
, 通过Process#start()
fork进程