1. 基本概念
1.1 Instrumentation是什么?
顾名思义,仪器仪表,用于在应用程序中进行“测量”和“管理”工作。一个应用程序中只有一个Instrumentation实例对象,且每个Activity都有此对象的引用。Instrumentation将在任何应用程序运行前初始化,可以通过它监测系统与应用程序之间的所有交互,即类似于在系统与应用程序之间安装了个“窃听器”。
当ActivityThread 创建(callActivityOnCreate)、暂停、恢复某个Activity时,通过调用此对象的方法来实现,如:
1) 创建: callActivityOnCreate
2) 暂停: callActivityOnPause
3) 恢复: callActivityOnResume
Instrumentation和ActivityThread的关系,类似于老板与经理的关系,老板负责对外交流(如与Activity Manager Service<AMS>),Instrumentation负责管理并完成老板交待的任务。
它通过以下两个成员变量来对当前应用进程中的Activity进行管理:
- private List<ActivityWaiter> mWaitingActivities;
- private List<ActivityMonitor> mActivityMonitors;
其功能函数下表所示:
功能 |
函数 |
增加删除Monitor |
addMonitor(ActivityMonitor monitor) removeMonitor(ActivityMonitor monitor) |
Application与Activity生命周期控制 |
newApplication(Class<?> clazz, Context context) newActivity(ClassLoader cl, String className,Intent intent) callActivityOnCreate(Activity activity, Bundle icicle) callActivityOnDestroy(Activity activity) callActivityOnStart(Activity activity) callActivityOnRestart(Activity activity) callActivityOnResume(Activity activity) callActivityOnStop(Activity activity) callActivityOnPause(Activity activity) |
Instrumentation生命周期控制 |
onCreate(Bundle arguments) start() onStart() finish(int resultCode, Bundle results) onDestroy() |
发送用户操控信息到当前窗口 |
sendCharacterSync(int keyCode) sendPointerSync(MotionEvent event) sendTrackballEventSync(MotionEvent event) sendTrackballEventSync(MotionEvent event) |
同步操作 |
startActivitySync(Intent intent) //它调用Context.startActivity runOnMainSync(Runnable runner) waitForIdle() |
2. Android应用程序启动过程(MainActivity)
即MainActivity的启动过程,在此过程中,将创建一个新的进程来执行此MainActivity。
Android应用程序从Launcher启动流程如下所示:
3. ActivityManagerService
3.1 类中关键信息
- public final class ActivityManagerService extends ActivityManagerNative
- implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
- ...
-
- static final int MAX_RECENT_TASKS = 20;
-
- public ActivityStack mMainStack;
-
-
-
-
- private boolean mShowDialogs = true;
-
-
-
-
-
- static class PendingActivityLaunch {
- ActivityRecord r;
- ActivityRecord sourceRecord;
- int startFlags;
- }
-
-
-
-
-
- ActivityRecord mFocusedActivity = null;
-
-
-
-
- final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>();
-
-
-
-
- final ProcessList mProcessList = new ProcessList();
-
-
-
-
-
-
-
- final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>();
-
-
-
-
- final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>();
- ...
-
- public static final Context main(int factoryTest) {
- AThread thr = new AThread();
- thr.start();
-
- synchronized (thr) {
- while (thr.mService == null) {
- try {
- thr.wait();
- } catch (InterruptedException e) {
- }
- }
- }
-
- ActivityManagerService m = thr.mService;
- mSelf = m;
- ActivityThread at = ActivityThread.systemMain();
- mSystemThread = at;
- Context context = at.getSystemContext();
- context.setTheme(android.R.style.Theme_Holo);
- m.mContext = context;
- m.mFactoryTest = factoryTest;
- m.mMainStack = new ActivityStack(m, context, true);
-
- m.mBatteryStatsService.publish(context);
- m.mUsageStatsService.publish(context);
-
- synchronized (thr) {
- thr.mReady = true;
- thr.notifyAll();
- }
-
- m.startRunning(null, null, null, null);
-
- return context;
- }
- }
3.2 家族图谱
4. ActivityStack-真正做事的家伙
ActivityManagerService使用它来管理系统中所有的Activities的状态,Activities使用stack的方式进行管理。它是真正负责做事的家伙,很勤快的,但外界无人知道!
4.1 类中关键信息
-
-
-
- final class ActivityStack {
- final ActivityManagerService mService;
- final boolean mMainStack;
- final Context mContext;
-
- enum ActivityState {
- INITIALIZING,
- RESUMED,
- PAUSING,
- PAUSED,
- STOPPING,
- STOPPED,
- FINISHING,
- DESTROYING,
- DESTROYED
- }
-
-
-
-
-
- final ArrayList<ActivityRecord> mHistory = new ArrayList<ActivityRecord>();
-
-
-
-
- final ArrayList<IBinder> mValidateAppTokens = new ArrayList<IBinder>();
-
-
-
-
-
-
- final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<ActivityRecord>();
-
-
-
-
-
-
- final ArrayList<ActivityRecord> mWaitingVisibleActivities
- = new ArrayList<ActivityRecord>();
-
-
-
-
-
-
- final ArrayList<ActivityRecord> mStoppingActivities
- = new ArrayList<ActivityRecord>();
-
-
-
-
- final ArrayList<ActivityRecord> mGoingToSleepActivities
- = new ArrayList<ActivityRecord>();
-
-
-
-
- ActivityRecord mPausingActivity = null;
-
-
-
-
-
-
- ActivityRecord mLastPausedActivity = null;
-
-
-
-
- ActivityRecord mResumedActivity = null;
-
-
-
-
-
-
- ActivityRecord mLastStartedActivity = null;
-
-
-
-
-
- boolean mUserLeaving = false;
-
- ActivityStack(ActivityManagerService service, Context context, boolean mainStack) {
- mService = service;
- mContext = context;
- mMainStack = mainStack;
- ...
- }
- ...
- }
4.2 家族图谱
5. ProcessRecord
记录了一个进程的相关信息。
5.1 类中关键信息
-
-
-
-
- class ProcessRecord {
- final ApplicationInfo info;
- final boolean isolated;
- final int uid;
- final int userId;
- final String processName;
-
- IApplicationThread thread;
-
-
-
-
-
- int pid;
-
-
- ApplicationInfo instrumentationInfo;
-
- BroadcastRecord curReceiver;
-
-
- final ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
-
-
- final HashSet<ServiceRecord> services = new HashSet<ServiceRecord>();
-
-
- final HashSet<ServiceRecord> executingServices
- = new HashSet<ServiceRecord>();
-
-
- final HashSet<ConnectionRecord> connections
- = new HashSet<ConnectionRecord>();
-
-
- final HashSet<ReceiverList> receivers = new HashSet<ReceiverList>();
-
-
- final HashMap<String, ContentProviderRecord> pubProviders
- = new HashMap<String, ContentProviderRecord>();
-
-
- final ArrayList<ContentProviderConnection> conProviders
- = new ArrayList<ContentProviderConnection>();
-
- boolean persistent;
- boolean crashing;
- Dialog crashDialog;
- boolean notResponding;
- Dialog anrDialog;
- boolean removed;
- boolean debugging;
- boolean waitedForDebugger;
- Dialog waitDialog;
-
- ProcessRecord(BatteryStatsImpl.Uid.Proc _batteryStats, IApplicationThread _thread,
- ApplicationInfo _info, String _processName, int _uid) {
- batteryStats = _batteryStats;
- info = _info;
- isolated = _info.uid != _uid;
- uid = _uid;
- userId = UserHandle.getUserId(_uid);
- processName = _processName;
- pkgList.add(_info.packageName);
- thread = _thread;
- maxAdj = ProcessList.HIDDEN_APP_MAX_ADJ;
- hiddenAdj = clientHiddenAdj = emptyAdj = ProcessList.HIDDEN_APP_MIN_ADJ;
- curRawAdj = setRawAdj = -100;
- curAdj = setAdj = -100;
- persistent = false;
- removed = false;
- }
- ...
- }
5. 2 家族图谱
6. IApplicationThread接口AMS->Application
IApplicationThread为AMS作为客户端访问Application服务器端的Binder接口。当创建Application时,将把此Binder对象传递给AMS,然后AMS把它保存在mProcessNames.ProcessRecord.thread中。当需要通知Application工作时,则调用IApplicationThread中对应的接口函数。
其相互关系如下图所示: