AMS & WMS, should be the service of the top 2 framwork layers of the app side.
Activitymanagerservice is a system process that Android provides to manage the operational status of the activity.
This series is divided into 3 parts, overview, Activitystatck & Activiy Task.
I. AMS overview
First, AMS is a service that is very similar to the service we develop, except that it manages activity.
So AMS is a process, and when the boot, it is resident in the system, the ServiceManager dispatch.
And after AMs starts, it begins to have a thread that listens to the customer's needs.
The code for android5.0:
\frameworks\base\services\java\com\android\server\systemservera.java
Private voidstartbootstrapservices () {//Wait for Installd-to-finish starting up so that it had a chance to//Create critical directories such as/data/user with the appropriate//permissions. We need this to complete before we initialize and other services.Minstaller = Msystemservicemanager.startservice (Installer.class); //Activity Manager runs the show.Mactivitymanagerservice =Msystemservicemanager.startservice (activitymanagerservice.lifecycle.class). GetService (); Mactivitymanagerservice.setsystemservicemanager (Msystemservicemanager); //Power Manager needs to is started early because other services need it. //Native daemons May is watching for it is registered so it must is ready//To handle incoming binder calls immediately (including being able to verify//The permissions for those calls).Mpowermanagerservice = Msystemservicemanager.startservice (powermanagerservice.class); //Now , the Power Manager have been started, let the activity manager//Initialize power management features.mactivitymanagerservice.initpowermanagement (); //display Manager is needed-provide Display metrics before package Manager//starts up.Mdisplaymanagerservice = Msystemservicemanager.startservice (displaymanagerservice.class); //We need the default display before We can initialize the package manager.msystemservicemanager.startbootphase (Systemservice.phase_wait_for_default_display); //Only Run "core" apps if we ' re encrypting the device.String cryptstate = Systemproperties.get ("Vold.decrypt"); if(Encrypting_state.equals (cryptstate)) {SLOG.W (TAG,"Detected encryption in Progress-only parsing core apps"); Monlycore=true; } Else if(Encrypted_state.equals (cryptstate)) {SLOG.W (TAG,"Device encrypted-only parsing core apps"); Monlycore=true; } //Start the package Manager.SLOG.I (TAG, "Package Manager")); Mpackagemanagerservice=Packagemanagerservice.main (Msystemcontext, Minstaller, Mfactorytestmode!=Factorytest.factory_test_off, Monlycore); Mfirstboot=Mpackagemanagerservice.isfirstboot (); Mpackagemanager=Msystemcontext.getpackagemanager (); SLOG.I (TAG,"User Service"); Servicemanager.addservice (Context.user_service, Usermanagerservice.getinstance ()); //Initialize attribute cache used to the cache resources from packages.Attributecache.init (Msystemcontext); //Set up the application instance for the system process and get started.mactivitymanagerservice.setsystemprocess (); }startbootstrapservices
You can see that AMS is started here. And this function startbootstrapservices is run in the Run method.
PublicActivitymanagerservice (Context systemcontext) {Mcontext=Systemcontext; Mfactorytest=Factorytest.getmode (); Msystemthread=Activitythread.currentactivitythread (); SLOG.I (TAG,"Memory class:" +Activitymanager.staticgetmemoryclass ()); Mhandlerthread=NewServicethread (TAG, Android.os.Process.THREAD_PRIORITY_FOREGROUND,false /*Allowio*/); Mhandlerthread.start (); Mhandler=NewMainHandler (Mhandlerthread.getlooper ()); Mfgbroadcastqueue=NewBroadcastqueue ( This, Mhandler,"Foreground", Broadcast_fg_timeout,false); Mbgbroadcastqueue=NewBroadcastqueue ( This, Mhandler,"Background", Broadcast_bg_timeout,true); mbroadcastqueues[0] =Mfgbroadcastqueue; mbroadcastqueues[1] =Mbgbroadcastqueue; Mservices=NewActiveservices ( This); Mprovidermap=NewProvidermap ( This); //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); Mbatterystatsservice.getactivestatistics (). readlocked (); Mbatterystatsservice.getactivestatistics (). writeasynclocked (); Monbattery= Debug_power?true: Mbatterystatsservice.getactivestatistics (). Getisonbattery (); Mbatterystatsservice.getactivestatistics (). Setcallback ( This); Mprocessstats=NewProcessstatsservice ( This,NewFile (Systemdir, "Procstats")); Mappopsservice=NewAppopsservice (NewFile (Systemdir, "Appops.xml"), Mhandler); Mgrantfile=NewAtomicfile (NewFile (Systemdir, "Urigrants.xml")); //User 0 is the first, and only user, runs at boot.Mstartedusers.put (0,NewUserstartedstate (NewUserhandle (0),true)); Muserlru.add (Integer.valueof (0)); Updatestarteduserarraylocked (); Gl_es_version= Systemproperties.getint ("Ro.opengles.version", configurationinfo.gl_es_version_undefined); Mconfiguration.settodefaults (); Mconfiguration.setlocale (Locale.getdefault ()); Mconfigurationseq= MCONFIGURATION.SEQ = 1; Mprocesscputracker.init (); Mcompatmodepackages=NewCompatmodepackages ( This, Systemdir, Mhandler); Mintentfirewall=NewIntentfirewall (Newintentfirewallinterface (), Mhandler); Mstacksupervisor=NewActivitystacksupervisor ( This); Mtaskpersister=NewTaskpersister (Systemdir, mstacksupervisor); Mprocesscputhread=NewThread ("Cputracker") {@Override Public voidrun () { while(true) { Try { Try { synchronized( This) { Final Longnow =Systemclock.uptimemillis (); LongNextcpudelay = (Mlastcputime.get () +monitor_cpu_max_time)-Now ; LongNextwritedelay = (mlastwritetime+battery_stats_time)-Now ; //slog.i (TAG, "Cpu delay=" + nextcpudelay//+ ", write delay=" + nextwritedelay); if(Nextwritedelay <Nextcpudelay) {Nextcpudelay=Nextwritedelay; } if(Nextcpudelay > 0) {Mprocesscpumutexfree.set (true); This. Wait (Nextcpudelay); } } } Catch(Interruptedexception e) {} updatecpustatsnow (); } Catch(Exception e) {slog.e (TAG,"Unexpected exception collecting process stats", E); } } } }; Mlocktoapprequest=NewLocktoapprequestdialog (Mcontext, This); Watchdog.getinstance (). Addmonitor ( This); Watchdog.getinstance (). Addthread (Mhandler); }Activitymanagerservice
New Activitystacksupervisor (this); New Taskpersister (Systemdir, mstacksupervisor);
These two sentences are Activitystack & activitytask settings.
public void setsystemprocess () is relatively straightforward, not only registering itself as a server, but also registering other servers.
Servicemanager.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));
Two. Activit State Management---Activitystack
1.ActivityState
Defines the following states:
State Change chart.
Three: StartActivity
[Email protected]
[Email protected]
[Email protected]
[Email protected]
[Email protected]
These 5 function relations, is the above order.
Public Final int startactivity (iapplicationthread caller, String callingpackage, int Requestcode, int startflags, Profilerinfo profilerinfo, Bundle options) { return Startactivityasuser (caller, callingpackage, intent, Resolvedtype, Resultto, resultwho, Requestcode, Startflags, Profilerinfo, Options, Userhandle.getcallinguserid ()); }
One more.
Userhandle.getcallinguserid ()
The UserID value of the caller, obtained through the bind mechanism.
@Override Public Final intStartactivityasuser (iapplicationthread caller, String callingpackage, Intent Intent, String resolvedtype, IBinder Resultto, String resultwho,intRequestcode,intStartflags, Profilerinfo profilerinfo, Bundle options,intuserId) { Enforcenotisolatedcaller("StartActivity"); UserId=Handleincominguser (Binder.getcallingpid (), Binder.getcallinguid (), UserId,false, Allow_full_only, "startactivity",NULL); //Todo:switch to the user app stacks here. returnMstacksupervisor.startactivitymaywait(Caller, 1, Callingpackage, Intent, Resolvedtype,NULL,NULL, Resultto, resultwho, Requestcode, Startflags, Profilerinfo,NULL,NULL, Options,falseUseridNULL,NULL); }
The purpose of the Enforcenotisolatedcaller is to confirm whether the current user belongs to the quarantined object.
Next up is startactivitymaywait.
Android Activity Manager AMS----Overview