I. Activitymanagerservice (AMS) Start-up process analysis
Start Activitymanagerservice in Systemserver
If you want to know Systemserver startup process can read this article: Android Source Code Analysis (vi) systemserver process
Frameworks\base\services\java\com\android\server\systemserver.java // Activity Manager runs the show. Tracebeginandslog ("startactivitymanager"); = Msystemservicemanager.startservice ( activitymanagerservice.lifecycle. class ). GetService (); Mactivitymanagerservice.setsystemservicemanager (Msystemservicemanager); Mactivitymanagerservice.setinstaller (installer);
Frameworks\base\services\core\java\com\android\server\am\activitymanagerservice.java
Public classActivitymanagerservice extends Iactivitymanager.stub implements Watchdog.monitor, Batterystatsimpl.batterycall Back {/** All system services*/Systemservicemanager Msystemservicemanager; /** Run all activitystacks through this*/ //Manage Activityfinal Activitystacksupervisor Mstacksupervisor; Final Activitystarter Mactivitystarter; Final Taskchangenotificationcontroller Mtaskchangenotificationcontroller; Final Instrumentationreporter Minstrumentationreporter=NewInstrumentationreporter (); Final ArrayList<ActiveInstrumentation> mactiveinstrumentation =NewArraylist<>(); //Whether We should use Sched_fifo for UI and Renderthreads. PrivateBoolean musefifouischeduling =false; //broadcast broadcast, foreground broadcast queue and background broadcast queueBroadcastqueue Mfgbroadcastqueue; Broadcastqueue Mbgbroadcastqueue; //Convenient for easy iteration over the queues. Foreground is first//So that dispatch of foreground broadcasts gets precedence.Final broadcastqueue[] Mbroadcastqueues =Newbroadcastqueue[2]; Broadcaststats Mlastbroadcaststats; Broadcaststats Mcurbroadcaststats; Broadcastqueue broadcastqueueforintent (Intent Intent) {Final Boolean ISFG= (Intent.getflags () & intent.flag_receiver_foreground)! =0; if(Debug_broadcast_background) slog.i (Tag_broadcast,"Broadcast Intent"+ Intent +" on"+ (ISFG?)"foreground":"background") +"Queue"); return(ISFG)?Mfgbroadcastqueue:mbgbroadcastqueue; } //Activity Stack /** * The last resumed activity. This was identical to the current resumed activity most * of the time but could was different when we ' re pausing one act Ivity before we resume * another activity. */ PrivateActivityrecord mlastresumedactivity; /** If Non-null, we are tracking the time of the user spends in the currently focused app. */ PrivateApptimetracker Mcurapptimetracker; //The ANR? The last ANR state, can you record the app ANR? /** Dump of the activity state at the time of the last ANR. Cleared after * {@link windowmanagerservice#last_anr_lifetime_duration_msecs}*/String mlastanrstate; //Service and Provider managementfinal activeservices mservices; Final Providermap Mprovidermap; //Storage System Data Directory//todo:move creation of battery stats service outside of Activity Manager service.File DataDir =environment.getdatadirectory (); File Systemdir=NewFile (DataDir,"system"); Systemdir.mkdirs (); Mbatterystatsservice=NewBatterystatsservice (Systemdir, Mhandler); //Apply Rights ManagementMappopsservice = Minjector.getappopsservice (NewFile (Systemdir,"Appops.xml"), Mhandler); //Acitivitymanager Add inServicemanager.addservice (Context.activity_service, This,true); Servicemanager.addservice (Processstats.service_name, mprocessstats); Servicemanager.addservice ("Meminfo",NewMembinder ( This)); Servicemanager.addservice ("Gfxinfo",NewGraphicsbinder ( This)); Servicemanager.addservice ("Dbinfo",NewDbbinder ( This)); if(monitor_cpu_usage) {Servicemanager.addservice ("Cpuinfo",NewCpubinder ( This)); } servicemanager.addservice ("Permission",NewPermissioncontroller ( This)); Servicemanager.addservice ("ProcessInfo",NewProcessinfoservice ( This)); //finally using watchdog monitoringWatchdog.getinstance (). Addmonitor ( This); Watchdog.getinstance (). Addthread (Mhandler); }
Some of the code snippets from the above can be learned that the AMS creation process involves the initialization of four Android component management. and Activitymanagerservice extends Iactivitymanager.stub, so the communication between Acitivitymanagerservice and Acitivitymanager is also using the binder mechanism.
Go inside and see Activitymanager.
// Servicemanager.addservice with Activitymanagerservice (Context.activity_service, this, true); @SystemService (context.activity_service)publicclass Activitymanager { ...}
Two. Activitymanager and Activitymanagerservice relations
If you want to understand activity is the process of calling Activitymanagerservice through Activitymanager you can look at this article.
Android Source Analysis (ii) Activity start analysis
Activitymanager (Frameworks/base/core/java/android/app/activitymanager.java)
Activitymanager is the client used to manage all the activity that is running on the system.
The top-level app passes the information to AMS through Activitymanager using binder mechanism, which is returned to the Activitymanager by the binder mechanism after AMS completes the interaction and dispatch work, returning the result to the upper app.
A picture of the location of Activitymanager and Activitymanagerservice during communication throughout the Android system.
Reference:
[1]https://www.cnblogs.com/bastard/p/5770573.html
Android Source code Analysis (13) Activitymanagerservice Service analysis