赞
踩
…
//加载libandroid_server.so文件,初始化本地服务
System.loadLibrary(“android_servers”);
…
// 初始化系统上下文环境
createSystemContext();
//创建系统服务管理器.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart);
//向系统本地服务添加系统服务管理器
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
//获取线程池执行初始化任务
SystemServerInitThreadPool.get();
} finally {
traceEnd(); // InitBeforeStartServices
}
// Start services.
try {
traceBeginAndSlog(“StartServices”);
//[1]主干 -> 启动程序引导服务其中包括(AMS Activity管理器、PMS package管理器 [包管理器]、PMS power管理器[电源管理器])
startBootstrapServices();
startCoreServices();// 启动核心服务
startOtherServices();//启动其它服务
SystemServerInitThreadPool.shutdown(); //关闭初始化任务线程池
} catch (Throwable ex) {
…
} finally {
traceEnd();
}
…
}
[3] 接下来我们去 startBootstrapServices函数
中看看AMS启动流程
private void startBootstrapServices() {
//读取配置信息
final String TAG_SYSTEM_CONFIG = “ReadingSystemConfig”;
traceBeginAndSlog(TAG_SYSTEM_CONFIG);
//在系统服务器初始化期间使用的线程池,提交任务
SystemServerInitThreadPool.get().submit(SystemConfig::getInstance, TAG_SYSTEM_CONFIG);
traceEnd();
…
//[1]启动AMS
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
//设置AMS系统服务管理器
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
traceEnd();
traceBeginAndSlog(“StartPowerManager”);
//启动PMS服务
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
traceEnd();
traceBeginAndSlog(“InitPowerManagement”);
//初始化AMS相关的PMS服务
mActivityManagerService.initPowerManagement();
traceEnd();
…
traceBeginAndSlog(“SetSystemProcess”);
//[2]向ServiceManager中注册Binder服务
mActivityManagerService.setSystemProcess();
traceEnd();
…
}
[4] 可以看到 此处调用了**startService函数
方法,以ActivityManagerService.Lifecycle.class
**作为参数进行传递
public T startService(Class serviceClass) {
try {
final String name = serviceClass.getName(); //获取类型
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);
// 创建服务
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
}
final T service;
try {
//通过反射创建 Lifecycle类型的service对象
Constructor constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
}
…
//调取startService多态
startService(service);
return service;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
}
public void startService(@NonNull final SystemService service) {
//服务注册,将创建的service添加到mServices集合中
mServices.add(service);
// Start it.
long time = SystemClock.elapsedRealtime();
try {
//开启服务
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + service.getClass().getName()
}
warnIfTooLong(SystemClock.elapsedRealtime() - time, service, “onStart”);
}
ActivityServiceManager类
**中的看看AMS
是如何执行启动操作的在SystemService启动AMS
时我们以**ActivityManagerService.Lifecycle.class
**作为参数进行传递的,我们知晓Lifecycle类
是ActivityManagerService的内部类
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
public Lifecycle(Context context) {
super(context);
//[1] 创建AMS的实例
mService = new ActivityManagerService(context);
}
@Override
public void onStart() {
//[2] 启动AMS
mService.start();
}
@Override
public void onCleanupUser(int userId) {
mService.mBatteryStatsService.onCleanupUser(userId);
}
//获取AMS服务
public ActivityManagerService getService() {
return mService;
}
}
[2] 创建AMS实例
public ActivityManagerService(Context systemContext) {
LockGuard.installLock(this, LockGuard.INDEX_ACTIVITY);
mInjector = new Injector();
mContext = systemContext; //获取系统上下文环境
mFactoryTest = FactoryTest.getMode();
mSystemThread = ActivityThread.currentActivityThread();//获取当前ActivityThread线程
mUiContext = mSystemThread.getSystemUiContext();
Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass());
mPermissionReviewRequired = mContext.getResources().getBoolean(
com.android.internal.R.bool.config_permissionReviewRequired);
mHandlerThread = new ServiceThread(TAG,
THREAD_PRIORITY_FOREGROUND, false /allowIo/); //创建前台线程
mHandlerThread.start(); //启动前台线程
mHandler = new MainHandler(mHandlerThread.getLooper());
mUiHandler = mInjector.getUiHandler(this);//创建UI线程
mConstants = new ActivityManagerConstants(this, mHandler);
…
//创建前台广播接收器,设置10秒超时
mFgBroadcastQueue = new BroadcastQueue(this, mHandler,
“foreground”, BROADCAST_FG_TIMEOUT, false);
//创建后台广播接收器,设置60秒超时
mBgBroadcastQueue = new BroadcastQueue(this, mHandler,
“background”, BROADCAST_BG_TIMEOUT, true);
//放入广播接收器数组中
mBroadcastQueues[0] = mFgBroadcastQueue;
mBroadcastQueues[1] = mBgBroadcastQueue;
//[1.1] 创建活动服务
mServices = new ActiveServices(this);
mProviderMap = new ProviderMap(this);
mAppErrors = new AppErrors(mUiContext, this);
File dataDir = Environment.getDataDirectory();
File systemDir = new File(dataDir, “system”);
systemDir.mkdirs();
//创建电池状态服务
mBatteryStatsService = new BatteryStatsService(systemContext, 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”));
//CUP执行状况
mProcessCpuThread = new Thread(“CpuTracker”) {
@Override
public void run() {
synchronized (mProcessCpuTracker) {
mProcessCpuInitLatch.countDown();
mProcessCpuTracker.init();
}
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;
//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(); //更新CUP状态
} catch (Exception e) {
Slog.e(TAG, “Unexpected exception collecting process stats”, e);
}
}
}
};
Watchdog.getInstance().addMonitor(this);
Watchdog.getInstance().addThread(mHandler);
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(资料价值较高,非无偿)
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。
高级进阶篇——高级UI,自定义View(部分展示)
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。
[外链图片转存中…(img-8GtDWJnb-1711531464677)]
高级进阶篇——高级UI,自定义View(部分展示)
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
[外链图片转存中…(img-VbDRdvpF-1711531464677)]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。