精华内容
下载资源
问答
  • AMS启动流程

    2019-01-27 04:32:49
    在SystemServer中简单分析了AMS流程,这篇就详细地讲解AMS启动流程 注意一点:AMS所在的进程是属于SystemServer中的 时序图 AMS流程 创建AMS对象 调用ams.setSystemProcess 调用ams.installSystemProviders 调用...

    在SystemServer中简单分析了AMS的流程,这篇就详细地讲解AMS的启动流程

    注意一点:AMS所在的进程是属于SystemServer中的

    时序图

    AMS流程

    • 创建AMS对象
    • 调用ams.setSystemProcess
    • 调用ams.installSystemProviders
    • 调用ams.systemReady

    ActivityThread创建

    在startBootstrapServices之前有一个流程是 createSystemContext,该过程会创建对象有ActivityThread,Instrumentation, ContextImpl,LoadedApk,Application,这些都是后面AMS要用到的对象,请注意它们是在这里创建的

        private void createSystemContext() {
            ActivityThread activityThread = ActivityThread.systemMain();
            mSystemContext = activityThread.getSystemContext();
            mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
    
            final Context systemUiContext = activityThread.getSystemUiContext();
            systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
        }
    复制代码

    创建AMS对象

    startBootstrapServices

    private void startBootstrapServices() {
        ...
        //启动AMS服务
        mActivityManagerService = mSystemServiceManager.startService(
                ActivityManagerService.Lifecycle.class).getService();
    
        //设置AMS的系统服务管理器
        mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
        //设置AMS的APP安装器
        mActivityManagerService.setInstaller(installer);
        //初始化AMS相关的PMS
        mActivityManagerService.initPowerManagement();
        ...
    
        //设置SystemServer
        mActivityManagerService.setSystemProcess();
    }
    复制代码

    startService

        public SystemService startService(String className) {
            final Class<SystemService> serviceClass;
            try {
                serviceClass = (Class<SystemService>)Class.forName(className);
            } catch (ClassNotFoundException ex) {
                ......
            }
            return startService(serviceClass);
        }
    
        public void startService(@NonNull final SystemService service) {
            // Register it.
            mServices.add(service);
            // Start it.
            long time = SystemClock.elapsedRealtime();
            try {
                service.onStart();
            } catch (RuntimeException ex) {
                .....
            }
            warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
        }
    复制代码

    这部分比较简单,主要是添加服务,并调用服务的 onStart 函数

    ActivityManagerService.Lifecycle.class

        public static final class Lifecycle extends SystemService {
            private final ActivityManagerService mService;
    
            public Lifecycle(Context context) {
                super(context);
                mService = new ActivityManagerService(context);
            }
    
            @Override
            public void onStart() {
                mService.start();
            }
    
            @Override
            public void onCleanupUser(int userId) {
                mService.mBatteryStatsService.onCleanupUser(userId);
            }
    
            public ActivityManagerService getService() {
                return mService;
            }
        }
    复制代码

    继承SystemService,代表是一个系统服务,里面主要是创建 ActivityManagerService

    ActivityManagerService对象创建

    public ActivityManagerService(Context systemContext) {
        mContext = systemContext;
        mFactoryTest = FactoryTest.getMode();
        mSystemThread = ActivityThread.currentActivityThread();
    
        //创建名为"ActivityManager"的前台线程,并获取mHandler
        mHandlerThread = new ServiceThread(TAG, android.os.Process.THREAD_PRIORITY_FOREGROUND, false);
        mHandlerThread.start();
    
        mHandler = new MainHandler(mHandlerThread.getLooper());
    
        //通过UiThread类,创建名为"android.ui"的线程
        mUiHandler = new UiHandler();
    
        ......
    
        //创建目录/data/system
        File dataDir = Environment.getDataDirectory();
        File systemDir = new File(dataDir, "system");
        systemDir.mkdirs();
    
        //创建服务BatteryStatsService
        mBatteryStatsService = new BatteryStatsService(systemDir, mHandler);
        mBatteryStatsService.getActiveStatistics().readLocked();
        ......
    
        //创建进程统计服务,信息保存在目录/data/system/procstats,
        mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
        ......
        //CPU使用情况的追踪器执行初始化
        mProcessCpuTracker.init();
        ......
        mRecentTasks = new RecentTasks(this);
        // 创建ActivityStackSupervisor对象
        mStackSupervisor = new ActivityStackSupervisor(this, mRecentTasks);
        mTaskPersister = new TaskPersister(systemDir, mStackSupervisor, mRecentTasks);
    
        //创建名为"CpuTracker"的线程
        mProcessCpuThread = new Thread("CpuTracker") {
            public void run() {
             ......
            }
        };
        ......
    }
    复制代码

    该过程共创建了3个线程

    • ActivityManager
    • android.ui
    • CpuTracker

    start

    mSystemServiceManager.startService 会调用服务的onStart,在AMS里onStart会调用他的start

    private void start() {
        Process.removeAllProcessGroups(); //移除所有的进程组
        mProcessCpuThread.start(); //开启CpuTracker线程
    
        mBatteryStatsService.publish(mContext); //启动电池统计服务
        mAppOpsService.publish(mContext);
        //创建LocalService,并添加到LocalServices
        LocalServices.addService(ActivityManagerInternal.class, new LocalService());
    }
    复制代码

    调用ams.setSystemProcess

    这个步骤还是在 startBootstrapServices 中执行的

        public void setSystemProcess() {
            try {
                ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
                ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
                ServiceManager.addService("meminfo", new MemBinder(this));
                ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
                ServiceManager.addService("dbinfo", new DbBinder(this));
                if (MONITOR_CPU_USAGE) {
                    ServiceManager.addService("cpuinfo", new CpuBinder(this));
                }
                ServiceManager.addService("permission", new PermissionController(this));
                ServiceManager.addService("processinfo", new ProcessInfoService(this));
    
                ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(
                        "android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
                mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
    
                ......
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException(
                        "Unable to find android system package", e);
            }
        }
    复制代码

    如果之前看过笔者写的binder系列,应该知道 ServiceManager.addService 主要就是添加服务

    该函数的主要作用是注册服务,以及为系统进程设置Application单例并启动,是由 installSystemApplicationInfo 函数完成的

    该函数最终会执行到 LoadedApk 的 installSystemApplicationInfo 函数,来设置包名为 'android' 的 package 信息

        /**
         * Sets application info about the system package.
         */
        void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
            assert info.packageName.equals("android");
            mApplicationInfo = info;
            mClassLoader = classLoader;
        }
    复制代码

    调用ams.installSystemProviders

    在执行完startBootstrapServices后,最后会运行 startOtherServices 函数

    private void startOtherServices() {
      ......
      //安装系统Provider 
      mActivityManagerService.installSystemProviders();
      ......
    
      mActivityManagerService.systemReady(new Runnable() {
         public void run() {
             ......
          }
      }
    }
    复制代码

    安装服务,就是在某某链表中添加

    installSystemProviders 中会调用 mSystemThread.installSystemProviders(providers)

     try {
            ActivityManager.getService().publishContentProviders(
                    getApplicationThread(), results);
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }
    复制代码

    那最终调用到 AMS的 publishContentProviders 函数,这个函数就是把providers放到map里等等操作

    调用ams.systemReady

    关于 systemReady 在SystemServer的启动流程中介绍过了

    AMS是最后一个启动的服务,会调用 mActivityManagerService.systemReady

    mActivityManagerService.systemReady(new Runnable() {
        public void run() {
            //启动WebView
          WebViewFactory.prepareWebViewInSystemServer();
          //启动系统UI
          startSystemUi(context);
    
          // 执行一系列服务的systemReady方法
          networkScoreF.systemReady();
          networkManagementF.systemReady();
          networkStatsF.systemReady();
    
          ......
    
          //执行一系列服务的systemRunning方法
          wallpaper.systemRunning();
          inputMethodManager.systemRunning(statusBarF);
          location.systemRunning();
    
        }
    });
    复制代码

    systemReady大致流程

    public final class ActivityManagerService{
    
        public void systemReady(final Runnable goingCallback) {
            ...//更新操作
            mSystemReady = true; //系统处于ready状态
            removeProcessLocked(proc, true, false, "system update done");//杀掉所有非persistent进程
            mProcessesReady = true;  //进程处于ready状态
    
            goingCallback.run(); //这里有可能启动进程
    
            addAppLocked(info, false, null); //启动所有的persistent进程
            mBooting = true;  //正在启动中
            startHomeActivityLocked(mCurrentUserId, "systemReady"); //启动桌面
            mStackSupervisor.resumeTopActivitiesLocked(); //恢复栈顶的Activity
        }
    }
    复制代码
    展开全文
  • AMS启动过程

    2019-09-12 13:40:26
    AMS启动过程 首先需要明白一点的是AMS的启动在SytemServer进程中启动的; public static void main(String[] args) { new SystemServer().run(); } 进入run()方法: private void run() { //1.创建消息...

    AMS启动过程

    首先需要明白一点的是AMS的启动在SytemServer进程中启动的;

     public static void main(String[] args) {
        new SystemServer().run();
    }
    

    进入run()方法:

    	  private void run() {
        
    
            
            //1.创建消息Looper
            Looper.prepareMainLooper();
    
            // Initialize native services.
            //2.加载了动态库libandroid_server.so
            System.loadLibrary("android_servers");
    
            // Check whether we failed to shut down last time we tried.
            // This call may not return.
            performPendingShutdown();
    
            // Initialize the system context.
            //3.创建系统的Context
            createSystemContext();
    
            // Create the system service manager.
            //4.创建SystemServiceManager
            mSystemServiceManager = new SystemServiceManager(mSystemContext);
           
       
    
        // Start services.
        try {
            traceBeginAndSlog("StartServices");
            //启动引导服务
            startBootstrapServices();
            //启动核心服务
            startCoreServices();
            //启动其他服务
            startOtherServices();
            SystemServerInitThreadPool.shutdown();
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        } finally {
            traceEnd();
        }
    
        
    
        // Loop forever.
        Looper.loop();
        throw new RuntimeException("Main thread loop unexpectedly exited");
    }
    

    这里会创建SystemServiceManager:用于系统服务创建、启动、管理生命周期;紧接着就会起启动系统服务;官方将系统服务分为三类:引导服务、核心服务、其他服务;这里的AMS属于引导服务:

    接着进入startBootstrapService():

    private void startBootstrapServices() {
       
    
       
        mActivityManagerService = mSystemServiceManager.startService(
        ActivityManagerService.Lifecycle.class).getService();
        mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
        mActivityManagerService.setInstaller(installer);
        
        
        		.......启动一系列系统服务
    
    		 
    }
    

    利用SystemServiceManager启动服务,进入startService(Class clas):

    public <T extends SystemService> T startService(Class<T> serviceClass) {
        
        final String name = serviceClass.getName();
           
     	 final T service;
    
        Constructor<T> constructor = serviceClass.getConstructor(Context.class);
        
        service = constructor.newInstance(mContext);
        startService(service);
        return service;
       
    }
    

    利用反射创建系统服务实例,进入startService(SystemServer service);

     public void startService(@NonNull final SystemService service) {
            // Register it.
            mServices.add(service);
            // Start it.
            long time = System.currentTimeMillis();
            try {
                service.onStart();
            } catch (RuntimeException ex) {
                throw new RuntimeException("Failed to start service " + service.getClass().getName()
                        + ": onStart threw an exception", ex);
            }
            
        }
    

    很简单就是service添加到ArrayList类型的mService完成注册,接着就是调用service的onstart()来完成启动,这里的Lifecycle继承自SystemServer,构造器内部会创建ActivityManagerService对象,调用其onstart()方法,实际上调用的是AMS的start()方法;

    展开全文
  • AMS 启动过程

    2018-03-17 15:13:12
    //mActivityManagerService 通过反射启动AMS 服务 // Activity manager runs the show. traceBeginAndSlog( "StartActivityManager" ) ; mActivityManagerService = mSystemServiceManager .startService ( ...

    SystemServer.java

      /**
         * The main entry point from zygote.
         */
        public static void main(String[] args) {
            new SystemServer().run();
        }

    创建SystemServer对象运行run()方法,点入run() 方法

    //启动服务的关键代码
    Start services.
            try {
                traceBeginAndSlog("StartServices");
                startBootstrapServices();
                startCoreServices();
                startOtherServices();
                SystemServerInitThreadPool.shutdown();
            } catch (Throwable ex) {
                Slog.e("System", "******************************************");
                Slog.e("System", "************ Failure starting system services", ex);
                throw ex;
            } finally {
                traceEnd();
            }

    进入startBootstrapServices() 可以看到启动ActivityManagerService的代码

    //mActivityManagerService 通过反射启动AMS 服务
      // Activity manager runs the show.
            traceBeginAndSlog("StartActivityManager");
            mActivityManagerService = mSystemServiceManager.startService(
                    ActivityManagerService.Lifecycle.class).getService();
            mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
            mActivityManagerService.setInstaller(installer);
            traceEnd();

    SystemServiceManager 启动服务的代码,可以看到到就是就是通过Class.forName()

    
        public SystemService startService(String className) {
            final Class<SystemService> serviceClass;
            try {
                serviceClass = (Class<SystemService>)Class.forName(className);
            } catch (ClassNotFoundException ex) {
                Slog.i(TAG, "Starting " + className);
                throw new RuntimeException("Failed to create service " + className
                        + ": service class not found, usually indicates that the caller should "
                        + "have called PackageManager.hasSystemFeature() to check whether the "
                        + "feature is available on this device before trying to start the "
                        + "services that implement it", ex);
            }
            return startService(serviceClass);
        }

    AMS 通过静态内部类启动

       public static final class Lifecycle extends SystemService {
            private final ActivityManagerService mService;
    
            public Lifecycle(Context context) {
                super(context);
                //创建AMS实例
                mService = new ActivityManagerService(context);
            }
    
            @Override
            public void onStart() {
            //启动AMS 服务对象
                mService.start();
            }
    
            public ActivityManagerService getService() {
            //获取实例对象
                return mService;
            }
        }

    我们的AMS已经启动了,接着我们点击桌面上的图标以后就要启动我们的app了。
    先上一张时序图
    这里写图片描述

    用户在Launcher程序里点击应用图标时,会通知ActivityManagerService启动应用的主Activity,ActivityManagerService发现这个应用还未启动,则会通知Zygote进程孵化出应用进程,然后在这个新孵化的应用进程里执行ActivityThread的main方法。

    让我们先来看看ActivityThread 这个线程干了什么

       public static void main(String[] args) {
    
            ····
            Looper.prepareMainLooper();
            ActivityThread thread = new ActivityThread();
            thread.attach(false);
            if (sMainThreadHandler == null) {
                sMainThreadHandler = thread.getHandler();
            }
    
           ···
            Looper.loop();
            ···
        }

    … 未完待续

    展开全文
  • 本文主要介绍 AMS启动流程和初始化过程AMS 在初始化的过程中,也伴随着了ATMS(ActivityTaskManagerService)的初始化。 AMS 创建流程简述: SystemServer:依次调用 main()、run()、...

    1 前言

            AMS 即 ActivityManagerService,负责 Activy、Service、Broadcast、ContentProvider 四大组件的生命周期管理。本文主要介绍 AMS 的启动流程和初始化过程。AMS 在初始化的过程中,也伴随着了ATMS(ActivityTaskManagerService)的初始化。

            AMS 创建流程简述:

    • SystemServer:依次调用 main()、run()、startBootstrapServices(),再调用 Lifecyle 的 startService() 方法;
    • Lifecyle:startService() 方法中调用 SystemServiceManager 的 startService() 方法,并将 Lifecyle.class 传入;
    • SystemServiceManager:startService() 方法通过反射调用 Lifecyle 的构造方法,生成 Lifecyle 对象;
    • Lifecyle:构造方法中调用 AMS 的构造方法创建 AMS 对象,并通过 getService() 方法返回 AMS 对象。

    2 AMS启动流程

            (1)main

            /frameworks/base/services/java/com/android/server/SystemServer.java

    public static void main(String[] args) {
        new SystemServer().run();
    }

            (2)run

            /frameworks/base/services/java/com/android/server/SystemServer.java

    private void run() {
    	try {
    		...
    		// 创建Looper
    		Looper.prepareMainLooper();
    		// 加载libandroid_servers.so
    		System.loadLibrary("android_servers");
    		// 创建系统的 Context:ContextImpl.createSystemContext(new ActivityThread())
    		createSystemContext();
    		// 创建 SystemServiceManager
    		mSystemServiceManager = new SystemServiceManager(mSystemContext);
    		LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
    		...
    	}
    	...
    	try {
    		//启动引导服务,ActivityManagerService、ActivityTaskManagerService、PackageManagerService、PowerManagerService、DisplayManagerService 等
    		startBootstrapServices(); 
    		//启动核心服务,BatteryService、UsageStatusService 等
    		startCoreServices(); 
    		//启动其他服务,InputManagerService、WindowManagerService、CameraService、AlarmManagerService 等
    		startOtherServices(); 
    		...
    	}
    	...
    	// 开启消息循环
    	Looper.loop();
    }

            (3)startBootstrapServices

            /frameworks/base/services/java/com/android/server/SystemServer.java

    private void startBootstrapServices() {
        ...
        Installer installer = mSystemServiceManager.startService(Installer.class);
    	...
        //启动 ATMS
    	ActivityTaskManagerService atm = mSystemServiceManager.startService(ActivityTaskManagerService.Lifecycle.class).getService();
        //启动 AMS,并注入 ATMS 
    	mActivityManagerService = ActivityManagerService.Lifecycle.startService(mSystemServiceManager, atm);
        //注入 mSystemServiceManager 和 installer
        mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
        mActivityManagerService.setInstaller(installer);
    	...
        //启动 PMS
        mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
        ...
        mActivityManagerService.initPowerManagement();
        ...
        mActivityManagerService.setSystemProcess();
        
    }

            (4)AMS.Lifecycle

            /frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.Lifecycle.java

    public static final class Lifecycle extends SystemService {
    	private final ActivityManagerService mService;
    	private static ActivityTaskManagerService sAtm;
    
    	public Lifecycle(Context context) {//被 SystemServiceManager 的 startService() 方法调用
    		super(context);
    		mService = new ActivityManagerService(context, sAtm);
    	}
    
    	public static ActivityManagerService startService(SystemServiceManager ssm, ActivityTaskManagerService atm) {
    		sAtm = atm;
    		return ssm.startService(ActivityManagerService.Lifecycle.class).getService();
    	}
    
        public void onStart() {
            mService.start(); //调用 AMS 的 start() 方法
        }
    
    	...
    
        public ActivityManagerService getService() {
            return mService;
        }
    }

            (5)startService

            /frameworks/base/services/core/java/com/android/server/SystemServiceManager.java

    public <T extends SystemService> T startService(Class<T> serviceClass) {
    	try {
    		final String name = serviceClass.getName();
    		...
    		final T service;
    		try { //通过反射调用 serviceClass 的构造方法 创建 Lifecycle 对象
    			Constructor<T> constructor = serviceClass.getConstructor(Context.class);
    			service = constructor.newInstance(mContext);
    		}
    		...
    		startService(service);
    		return service;
    	}
    	...
    }
    
    public void startService(SystemService service) {
    	mServices.add(service); //mServices: ArrayList<SystemService>
    	...
    	try {
    		service.onStart(); //调用 Lifecycle 的 onStart 方法
    	}
    	...
    }

            注意:startService() 方法中调用了 Lifecycle 的 onStart() 方法,进一步调用 AMS 的 start() 方法初始化(下文还会介绍)。

    3 AMS 初始化

            初始化过程如下: 

            (1)AMS 的构造方法

            /frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

    public ActivityManagerService(Context systemContext, ActivityTaskManagerService atm) {
    	...
    	mInjector = new Injector();
    	mContext = systemContext;
    	...
    	mSystemThread = ActivityThread.currentActivityThread();
    	mUiContext = mSystemThread.getSystemUiContext();
    	...
    	mHandlerThread = new ServiceThread(TAG, THREAD_PRIORITY_FOREGROUND, false);
    	mHandlerThread.start();
    	mHandler = new MainHandler(mHandlerThread.getLooper());
    	mUiHandler = mInjector.getUiHandler(this);
    	...
    	mProcStartHandlerThread = new ServiceThread(TAG + ":procStart", THREAD_PRIORITY_FOREGROUND, false);
    	mProcStartHandlerThread.start();
    	mProcStartHandler = new Handler(mProcStartHandlerThread.getLooper());
    	...
    	final ActiveUids activeUids = new ActiveUids(this, true);
        mPlatformCompat = (PlatformCompat) ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE);
        mProcessList.init(this, activeUids, mPlatformCompat);
    	...
    	mServices = new ActiveServices(this); //用于启动 Service
    	mProviderMap = new ProviderMap(this); //存储 ContentProvider
    	...
    	mUserController = new UserController(this);
    	mPendingIntentController = new PendingIntentController(mHandlerThread.getLooper(), mUserController);
    	...
    	mIntentFirewall = new IntentFirewall(new IntentFirewallInterface(), mHandler);
    	mActivityTaskManager = atm;
    	//进一步初始化 ATMS
    	mActivityTaskManager.initialize(mIntentFirewall, mPendingIntentController, DisplayThread.get().getLooper());
    	mAtmInternal = LocalServices.getService(ActivityTaskManagerInternal.class);
    	...
    }

            (2)start

            start() 方法被 Lifecycle 的 onStart() 方法调用,onStart() 方法又被 SystemServiceManager 的 startService() 方法调用。

    /frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java 

    private void start() {
    	...
    	LocalServices.addService(ActivityManagerInternal.class, new LocalService());
    	mActivityTaskManager.onActivityManagerInternalAdded();
    	mUgmInternal.onActivityManagerInternalAdded();
    	mPendingIntentController.onActivityManagerInternalAdded();
    	...
    }

            (3)initPowerManagement、setSystemProcess

            在 SystemServer 的 startBootstrapServices() 方法中,调用了 AMS 的 initPowerManagement() 和 setSystemProcess() 方法。

            /frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

    public void initPowerManagement() {
    	mActivityTaskManager.onInitPowerManagement();
    	mBatteryStatsService.initPowerManagement();
    	mLocalPowerManager = LocalServices.getService(PowerManagerInternal.class);
    }
    
    public void setSystemProcess() {
    	try {
    		...
    		synchronized (this) {
    			ProcessRecord app = mProcessList.newProcessRecordLocked(info, info.processName, false, 0, new HostingRecord("system"));
    			app.setPersistent(true);
    			app.pid = MY_PID;
    			app.getWindowProcessController().setPid(MY_PID);
    			app.maxAdj = ProcessList.SYSTEM_ADJ;
    			app.makeActive(mSystemThread.getApplicationThread(), mProcessStats);
    			mPidsSelfLocked.put(app);
    			mProcessList.updateLruProcessLocked(app, false, null);
    			updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE);
    		}
    	}
    	...
    }

            (4)setUsageStatsManager

            在 SystemServer 的 startCoreServices() 方法中,调用了 AMS 的 setUsageStatsManager()  方法。

            /frameworks/base/services/java/com/android/server/SystemServer.java

    private void startCoreServices() {
    	...
    	mSystemServiceManager.startService(UsageStatsService.class);
    	mActivityManagerService.setUsageStatsManager(LocalServices.getService(UsageStatsManagerInternal.class));
    	...
    }

             /frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

    public void setUsageStatsManager(UsageStatsManagerInternal usageStatsManager) {
    	mUsageStatsService = usageStatsManager;
    	mActivityTaskManager.setUsageStatsManager(usageStatsManager);
    }

            (5)installSystemProviders、setWindowManager、systemReady

            在 SystemServer 的 startOtherServices() 方法中,调用了 AMS 的 installSystemProviders()、setWindowManager()  和 systemReady() 方法。

            /frameworks/base/services/java/com/android/server/SystemServer.java

    private void startOtherServices() {
    	...
    	WindowManagerService wm = null;
    	...
    	try {
    		...
    		mActivityManagerService.installSystemProviders(); //1
    		...
    		wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore, new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
    		...
    		mActivityManagerService.setWindowManager(wm); //2
    		...
    	}
    	...
    	mActivityManagerService.systemReady(() -> { //3
    		...
    		mSystemServiceManager.startBootPhase(SystemService.PHASE_ACTIVITY_MANAGER_READY);
    		...
    		try {
    			mActivityManagerService.startObservingNativeCrashes();
    		}
    		...
    		try {
    			startSystemUi(context, windowManagerF);
    		}
    		...
    		mSystemServiceManager.startBootPhase(SystemService.PHASE_THIRD_PARTY_APPS_CAN_START);
    		...
    	}, BOOT_TIMINGS_TRACE_LOG);
    }

             /frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

    public final void installSystemProviders() {
    	List<ProviderInfo> providers;
    	synchronized (this) {
    		ProcessRecord app = mProcessList.mProcessNames.get("system", SYSTEM_UID);
    		providers = generateApplicationProvidersLocked(app);
    		...
    	}
    	if (providers != null) {
    		mSystemThread.installSystemProviders(providers);
    	}
    	...
    	mConstants.start(mContext.getContentResolver());
    	mCoreSettingsObserver = new CoreSettingsObserver(this);
    	mActivityTaskManager.installSystemProviders();
    	...
    }
    
    public void setWindowManager(WindowManagerService wm) {
    	synchronized (this) {
    		mWindowManager = wm;
    		mActivityTaskManager.setWindowManager(wm);
    	}
    }
    
    public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
        //添加 AMS 服务,方便跨进程调用
        ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true, DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PROTO);
    	...
    	synchronized(this) {
    		...
    		mLocalDeviceIdleController = LocalServices.getService(DeviceIdleController.LocalService.class);
    		mActivityTaskManager.onSystemReady();
    		mUserController.onSystemReady();
    		mAppOpsService.systemReady();
    		mSystemReady = true;
    	}
    	...
    }

            已通过 ServiceManager.addService() 将 Context.ACTIVITY_SERVICE 与 AMS 绑定,因此在其他进程中可以通过如下方式获取 AMS。

    IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
    IActivityManager am = IActivityManager.Stub.asInterface(b);

    展开全文
  • 安卓系统框架学习_AMS启动流程 本文参考https://blog.csdn.net/renshuguo123723/article/details/85332146 AMS的启动流程 1:SystemServer#main -> 2:SystemServer#run -> 3:SystemServiceManager#...
  • ActivityManagerService分析——AMS启动流程   完整Android学习路径 请戳我的Android学习之旅(持续更新中...)
  • Android是基于Linux系统的,Linux启动后用户空间创建的第一个进程是init进程...2、创建SystemServer进程,我们常见的AMS、PMS、WMS等都是SystemServer进程中的服务。而在SystemServer中会启动许多的服务,包括Activi...
  • Android解析ActivityManagerService(一)AMS启动流程和AMS家族 前言 此前在Android系统启动流程、应用进程以及深入四大组件这三个系列文章中,都提及到了AMS,但都没有系统的来讲解它,本文就以AMS为主来进行讲解...
  • //启动其他服务,AMS启动后,完成后续桌面启动等操作 ... } 1.2 System Context初始化 1.2.1 createSystemContext 在SystemServer的run函数中,在启动AMS之前,调用了createSystemContext函,主要用来是初始化 ...
  • 本次将讲解Launcher启动过程,将从AMS中启动Launcher开始说起一、SystemServiceManager启动服务过程在说AMS启动之前有必要说一下SSM是怎样启动service启动的,这个之前关系后面理解AMS启动过程中一些操作。...
  • 谨以此记录下学习流程,欢迎广大博友交流指正。 1.代码来源 2.启动流程

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 13,487
精华内容 5,394
关键字:

ams启动流程