当前位置: 代码迷 >> Android >> android起动源码研究
  详细解决方案

android起动源码研究

热度:71   发布时间:2016-05-01 20:29:21.0
android启动源码研究

?记得公司同事也提过这个启动过程,共4个步骤:

1 .init进程启动

2 .Native服务启动

3 .SystemSeverAndroid服务Init2

4 .Home启动

现在我们按照执行顺序挨个来看:

?1?init进程启动

Init.c[1047]:main函数可知,他在解析init.rc/init.%s.rc脚本

/* -----------------init.rc-----------------------------*/

INFO("reading config file\n");parse_config_file("/init.rc");

qemu_init();import_kernel_cmdline(0); get_hardware_name();

?/* -----------------init.%s.rcinit.%s.rc-----------------------------*/

?snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware);parse_config_file(tmp);

Init.rc[383]:这是一个Android自定义格式的脚本【详细我将用一片文档类别的日志粘贴上去】有4中类型:Action,Commands,Service,Options

那么他的启动如下:

???Init.c打开rc文件解析服务,存于service_list/*具体怎么解析不做研究*/

简单的解释下:int parse_config_file(const char *fn)

{

????char *data;

????data = read_file(fn, 0);

????if (!data) return -1;

????parse_config(fn, data);

????DUMP();

/* DUMP();它解析内容:struct service *svc;struct action *act;struct command *cmd;struct listnode *node;struct listnode *node2;struct socketinfo *si;

是list_for_each()这个方法进行具体解析的,这里就不作更深的代码黏贴了,读者可以自己去看*/

????int n;

????return 0;

}

那么他是如何开启NativeAndroid的服务的呢?

?Service的启动靠的是serivce_startmain函数启动,在死循环中:?

for(;;) {

????????int nr, i, timeout = -1;

????????for (i = 0; i < fd_count; i++)

????????????ufds[i].revents = 0;

?? ? ? ?drain_action_queue();

??????? restart_processes();//?会遍历Servicerestart_service_if_needed,调用serivce_start()

????????if (process_needs_restart) {

????????????timeout = (process_needs_restart - gettime()) * 1000;

????????????if (timeout < 0)

????????????????timeout = 0;

????????}

serivce_start()做的时期就是初始化好srv[service],并调用execve()方法,从而建立Service进程。

//具体代码请作者自己去看,这里就不黏贴了。

启动ZygoteZygote的职责就是产生Activity,(接受ActivityManagerService请求)将单独写文章理解

启动System Server:在ZygoteInit这个类中,他启动了SystemServer进程:

ZygoteInit这个类的main函数中 :有这样一段代码

if (argv[1].equals("true")) {

??????????????? startSystemServer();

//Zygote.forkSystemServe->启动了SystemServer进程

????????????}

至于系统的Native服务和Android服务的启动过程,上一次日志,我们已经写过了,这里就不在缀诉。在每个服务启动后了之后,都会回调相应服务的onSystemReady()来执行回调的代码,同样的ActivityManager也是又这个回调函数:,

??//?如下服务是构建回调ActivityManagerService所要的服务,我们单独列出来

????????final BatteryService batteryF = battery;

????????final ConnectivityService connectivityF = connectivity;

????????final DockObserver dockF = dock;

????????final ThrottleService throttleF = throttle;

????????final UiModeManagerService uiModeF = uiMode;

????????final AppWidgetService appWidgetF = appWidget;

????????final WallpaperManagerService wallpaperF = wallpaper;

????????final InputMethodManagerService immF = imm;

????????final RecognitionManagerService recognitionF = recognition;

????????final LocationManagerService locationF = location;

???????//我们告诉ActivityManagerService现在可以运行第三方代码了,他们一旦获得状态就会回调我们的CallBack来完成整个初始化过程。原文注释如下:

?

?? ? ? ?// We now tell the activity manager it is okay to run third party

?? ? ? ?// code. ?It will call back into us once it has gotten to the state

?? ? ? ?// where third party code can really run (but before it has actually

?? ? ? ?// started launching the initial applications), for us to complete our

?? ? ? ?// initialization.

????????((ActivityManagerService)ActivityManagerNative.getDefault())

??????????????? .systemReady(new Runnable()?{

????????????public void run() {

????????????????if (batteryF != null) batteryF.systemReady();

????????????????if (connectivityF != null) connectivityF.systemReady();

????????????????if (dockF != null) dockF.systemReady();

????????????????if (uiModeF != null) uiModeF.systemReady();

????????????????if (recognitionF != null) recognitionF.systemReady();

????????????????Watchdog.getInstance().start();

?????????????????if (appWidgetF != null) appWidgetF.systemReady(safeMode);

????????????????if (wallpaperF != null) wallpaperF.systemReady();

????????????????if (immF != null) immF.systemReady();

????????????????if (locationF != null) locationF.systemReady();

????????????????if (throttleF != null) throttleF.systemReady();

????????????}

????????});

这样就调用了ActivityManagerServicesystemReady(final Runnable?goingCallback)。

ActivityManagerServicesystemReady(),当判断为mSystemReadytrue的时候,便会回调goingCallback.run。这种多线程的代码的组织技巧也是我们应该学习的。我们甚至看到如果主题是CategoryHome,那么系统是不会回收的。

言归正传:在systemReady()里调用这么个方法:resumeTopActivityLocked(null),他引导了Home的启动。

在这个resumeTopActivityLocked?(HistoryRecord prev)有这样一个代码

if (next == null) {//?HistoryRecord next =?topRunningActivityLocked(null);

????????????// 没有其他Activity,只要启动Home就好

????????????return startHomeActivityLocked();

????????}

付解释代码:

private final HistoryRecord topRunningActivityLocked(HistoryRecord notTop) {

????????int i = mHistory.size()-1;

????????while (i >= 0) {

????????????HistoryRecord r = (HistoryRecord)mHistory.get(i);

????????????if (!r.finishing && r != notTop) {

????????????????return r;

????????????}

????????????i--;

????????}

????????return null;

????}

在这个函数里便启动了HomeActivity?? ??

  相关解决方案