首页 > 学院 > 开发设计 > 正文

ActivityManagerService

2019-11-09 13:44:22
字体:
来源:转载
供稿:网友

引言: 由于上节我们知道SystemServer启动了很多系统服务,当前也启动了ActivityManagerService服务 所以我们这节内容就分析AMS的启动过程以及在系统中承担的角色.

@(晚辈目前还是在校大学生,所以可能又很多地方理解有偏差,所以希望前辈们不吝赐教)

ActivityManagerService的创建1startService2ActivityManagerServiceLifecycleclassAMSAMSonStartsetSystemPRocess

ActivityManagerService的创建

衔接上文: 在SystemServer的run方法中调用startBootstrapServices(),在其中:

// Activity manager runs the show. mActivityManagerService = mSystemServiceManager.startService( ActivityManagerService.Lifecycle.class).getService();//[1][2] //给AMS设置SystemServiceManager mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); ... mActivityManagerService.setSystemProcess();

1startService()

@(SystemServiceManager.java->startService())

通过反射调用构造 调用service.onStart()方法 public <T extends SystemService> T startService(Class<T> serviceClass) { try { final String name = serviceClass.getName(); ... final T service; try { Constructor<T> constructor = serviceClass.getConstructor(Context.class); service = constructor.newInstance(mContext); } ... mServices.add(service); // Start it. try { service.onStart();//针对AMS,则调用AMS的onStart()方法 } ... return service; } finally { ... } }

2ActivityManagerService.Lifecycle.class

@(SystemService的派生类)

内部持有AMS的对象,所以到目前为止AMS就创建出来了

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(); } public ActivityManagerService getService() { return mService; } }

AMS

@(AMS的构造方法)

创建出一些管理4大组件的管理对象.

// public ActivityManagerService(Context systemContext) { mContext = systemContext; mFactoryTest = FactoryTest.getMode(); //获取ActivityThread对象的引用 mSystemThread = ActivityThread.currentActivityThread(); //创建一个HandlerThread mHandlerThread = new ServiceThread(TAG, android.os.Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/); mHandlerThread.start(); mHandler = new MainHandler(mHandlerThread.getLooper()); //创建UI线程 mUiHandler = new UiHandler(); ... //创建用于管理广播的数据结构 mFgBroadcastQueue = new BroadcastQueue(this, mHandler, "foreground", BROADCAST_FG_TIMEOUT, false); mBgBroadcastQueue = new BroadcastQueue(this, mHandler, "background", BROADCAST_BG_TIMEOUT, true); mBroadcastQueues[0] = mFgBroadcastQueue; mBroadcastQueues[1] = mBgBroadcastQueue; mServices = new ActiveServices(this);//创建管理Service的组件 mProviderMap = new ProviderMap(this);//创建管理provider的组件 mAppErrors = new AppErrors(mContext, this); // 创建一个/data/system的目录 File dataDir = Environment.getDataDirectory(); File systemDir = new File(dataDir, "system"); systemDir.mkdirs(); //有关电池相关的服务操作 mBatteryStatsService = new BatteryStatsService(systemDir, mHandler); mBatteryStatsService.getActiveStatistics().readLocked(); mBatteryStatsService.scheduleWriteToDisk(); mOnBattery = DEBUG_POWER ? true : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); mBatteryStatsService.getActiveStatistics().setCallback(this); //有关进程统计的服务 mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats")); mAppOpsService = new AppOpsService(new File(systemDir, "appops.xml"), mHandler); mAppOpsService.startWatchingMode(AppOpsManager.OP_RUN_IN_BACKGROUND, null, new IAppOpsCallback.Stub() { @Override public void opChanged(int op, int uid, String packageName) { if (op == AppOpsManager.OP_RUN_IN_BACKGROUND && packageName != null) { if (mAppOpsService.checkOperation(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) { runInBackgroundDisabled(uid); } } } }); ... //有关cpu信息的初始化 mProcessCpuTracker.init(); mCompatModePackages = new CompatModePackages(this, systemDir, mHandler); mIntentFirewall = new IntentFirewall(new IntentFirewallInterface(), mHandler); //创建管理Activity的对象 mStackSupervisor = new ActivityStackSupervisor(this); mActivityStarter = new ActivityStarter(this, mStackSupervisor); mRecentTasks = new RecentTasks(this, mStackSupervisor); //创建一个线程名称为CpuTracker,主要是做一些CPU有关的操作 mProcessCpuThread = new Thread("CpuTracker") { @Override public void run() { while (true) { try { try { synchronized(this) { final long now = SystemClock.uptimeMillis(); long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; 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); } } } }; ... }

AMS.onStart()

@(onStart())

private void start() { Process.removeAllProcessGroups();//Remove all process groups mProcessCpuThread.start();//Runtime CPU use collection thread //启动cpu相关 mBatteryStatsService.publish(mContext); mAppOpsService.publish(mContext); LocalServices.addService(ActivityManagerInternal.class, new LocalService());}

setSystemProcess()

@(源于SystemService中startBootstrapServices()调用)

mActivityManagerService.setSystemProcess(); 主要是向ServiceManager注册了一些服务,最后看出向AMS中把SystemServer自己添加到其中,所以AMS是管理这个系统进程的类.

public void setSystemProcess() { try { //将AMS添加到ServiceManager 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)); //db状态服务 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)); ... synchronized (this) { //把SystemServer进程本身添加到process管理中 ProcessRecord app = newProcessRecordLocked(info, info.processName, false, 0); app.persistent = true; app.pid = MY_PID; app.maxAdj = ProcessList.SYSTEM_ADJ; app.makeActive(mSystemThread.getapplicationThread(), mProcessStats); synchronized (mPidsSelfLocked) { mPidsSelfLocked.put(app.pid, app); } updateLruProcessLocked(app, false, null); updateOomAdjLocked(); } } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException( "Unable to find android system package", e); } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表