Android Startup Process
First, let's take a look at the Android startup flowchart:
One of the two most important indicators of a process is to start the Binder thread pool, that is, the Binder process can communicate with each other. The other is to start the Handler message loop, which can be used.
1. When will the systemserver Process implement the above two mechanisms? See the Code:
The Binder thread pool is started. Is the subthread pool.
public static final void zygoteInit(String[] argv) throws ZygoteInit.MethodAndArgsCaller { ...... zygoteInitNative(); ......}The Hander message loop mechanism is enabled, which is the Handler of the sub-thread:
public static final void init2() { Slog.i(TAG, Entered the Android system server!); Thread thr = new ServerThread(); thr.setName(android.server.ServerThread); thr.start(); }
Class ServerThread extends Thread {@ Override public void run () {EventLog. writeEvent (EventLogTags. BOOT_PROGRESS_SYSTEM_RUN, SystemClock. uptimeMillis (); logoff. prepare ();..... try {...... slog. I (TAG, Activity Manager); context = ActivityManagerService. main (factoryTest); // ActivityTask is initialized ...... slog. I (TAG, Package Manager); pm = PackageManagerService. main (context, factoryTest! = SystemServer. FACTORY_TEST_OFF); // The PackageMangerService ActivityManagerService. setSystemProcess (); ...... wm = WindowManagerService. main (context, power, factoryTest! = SystemServer. FACTORY_TEST_LOW_LEVEL); ServiceManager. addService (Context. WINDOW_SERVICE, wm );...... (ActivityManagerService) ActivityManagerNative. getDefault ()). systemReady (new Runnable () {public void run () {}}); loid. loop (); Slog. d (TAG, System ServerThread is exiting !); }}
2. When will the application process implement the above two mechanisms? See the Code:
The Binder thread pool is started, which is a sub-thread pool like above.
public static final void zygoteInit(String[] argv) throws ZygoteInit.MethodAndArgsCaller { ...... zygoteInitNative(); ......}The Hander message loop mechanism is enabled, which is the Handler of the main thread:
Public static final void main (String [] args) {SamplingProfilerIntegration. start (); Process. setArgV0 (); Looper.prepareMainLooper(); if (sMainThreadHandler == null) { sMainThreadHandler = new Handler(); } ActivityThread thread = new ActivityThread(); thread.attach(false); .... Looper.loop(); ...... }
3. Let's take a look at the program that starts the Laucher interface.
((ActivityManagerService)ActivityManagerNative.getDefault()) .systemReady(new Runnable() { public void run() { } });According to the source code scenario analysis book of the Android system, systemReady finally executed the following:
boolean startHomeActivityLocked() { if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL && mTopAction == null) { // We are running in factory test mode, but unable to find // the factory test app, so just sit around displaying the // error message and don't try to start anything. return false; } Intent intent = new Intent( mTopAction, mTopData != null ? Uri.parse(mTopData) : null); intent.setComponent(mTopComponent); if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { intent.addCategory(Intent.CATEGORY_HOME); } ActivityInfo aInfo = intent.resolveActivityInfo(mContext.getPackageManager(), STOCK_PM_FLAGS); if (aInfo != null) { intent.setComponent(new ComponentName( aInfo.applicationInfo.packageName, aInfo.name)); // Don't do this if the home app is currently being // instrumented. ProcessRecord app = getProcessRecordLocked(aInfo.processName, aInfo.applicationInfo.uid); if (app == null || app.instrumentationClass == null) { intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo, null, null, 0, 0, 0, false, false); } } return true; }This is executed in the sub-thread of SystemServer, mMainStack. for the Execution Code of startActivityLocked, see the source code scenario analysis book of the Android system. Different execution methods appear in resumeTopActivityLocked. Because mResumedActivity is NULL, startSpecificActivityLocked (next, true, true, true ). Finally, execute startProcessLocked, which is the key.
int pid = Process.start(android.app.ActivityThread, mSimpleProcessManagement ? app.processName : null, uid, uid, gids, debugFlags, null);
Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); msg.obj = app; mHandler.sendMessageDelayed(msg, PROC_START_TIMEOUT);
Here, mHander is final Handler mHandler = new Handler () {}, and this Handler is the handler in the sub-thread of ServerThread. When a message is sent, only lorule. prepare () is called. loop () is called ();
Process. start creates an application Process, and enables the Binder thread pool and Handler message loop mechanism. For details, refer to the 2nd.
1. thread. attach (false) is the communication between the Launcher main thread and the SystemServer sub-thread Binder process, sending ATTACH_APPLICATION_TRANCTION; SystemServer sub-thread (Binder thread pool) removes the ServerThread sub-thread (Handler message loop) in SystemServer) the messages in the Handler.
2. The SystemServer sub-thread sends SCHEDULE_LAUNCH_ACTIVITY_TARNSACTION to the Laumcher sub-thread. Then the sub-thread of SystemServer continues to wait cyclically and sends the returned data to the Launcher sub-thread.
3. The logoff. loop () is called after the return of the Laucher main thread, and the message loop mechanism of the main thread is formally created.
4. process the data (one-way) sent by the SystemServer subthread to the Lanncher subthread. The Launcher subthread finally calls the Launcher onCreate method in the main thread through the message processing mechanism.