Android startup life cycle process

Table of contents

1. Brief introduction

2. Description of the start-up process (1)

3. Description of the start-up process (2)

4. Function introduction of common components of ServerThread


1. Brief introduction

Step 1: Start Linux:

1.Bootloader

2.kernel

Step 2: Android system startup: the entry is Init.rc(system/core/rootdir)

1./system/bin/servicemanager: Binder daemon process;

2.Runtime;

3.Zygote:app_process/App_main;

4.Start VM;

5.System Server;

6.Start Android Services:Register to ServiceManager;

7.Start Launcher;

Step Three: Application Startup:

1. Run Packager Manager


2. Description of the start-up process (1)

  1. Android's servicemanager file is located in /system/bin/servicemanager. At the beginning of the system startup, a Daemon Process will be instantiated through the file whose source code is ServiceManager.java, which is used to maintain the operation of the system in the background;
  2. When starting zygote under /system/bin, app_main.cpp is actually started, and the system file CPP is located in the frameworks/base/cmds/app_process/app_main.cpp file;
  3. AndroidRuntime.cpp is called in app_main.cpp, how does AndroidRuntime.cpp call ZygoteInit.java through JNI, and ZygoteInit.java calls SystemServer.java;
  4. SystemServer.java calls com_android_server_systemService.app through JNI, and then calls the init2() method in SystemServer.java by running system_init.cpp. At this time, runtime->callStatic("com/ android/server/SystemServer", "init2") to start the ServerThread thread.

3. Description of the start-up process (2)

The service thread ServerThread contains the following functions:

  1. PowerManagerService: power management;
  2. ActivityMangerService: component management;
  3. WindowManagerService: window management;
  4. PackageManagerService: The application is started through this Service.

4. Function introduction of common components of ServerThread

1. PowerManagerService: power management

PowerManagerService is responsible for managing power-related functions of the device, such as screen switching, sleep mode, battery management, etc.
Get the PowerManager object: Use the following code in Activity or Service to get the PowerManager object.

PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);

//1.控制屏幕亮度:可以使用PowerManager对象的方法来控制屏幕亮度,例如:
// 将屏幕设置为全亮
powerManager.setBrightness(PowerManager.BRIGHTNESS_MAX);
// 将屏幕设置为最暗
powerManager.setBrightness(PowerManager.BRIGHTNESS_OFF);


//2.控制休眠模式:可以使用PowerManager对象的方法来控制设备的休眠模式,例如:
// 进入休眠模式
powerManager.goToSleep(SystemClock.uptimeMillis());
// 唤醒设备
powerManager.wakeUp(SystemClock.uptimeMillis());


2.ActivityManagerService: component management

ActivityManagerService is responsible for managing components in the Android system, such as Activity, Service, Broadcast Receiver, etc. It is responsible for starting, managing, and destroying these components, and provides functions related to the component life cycle.
Start Activity: You can use the startActivity method of Intent and Context to start an Activity, for example:

Intent intent = new Intent(this, MyActivity.class);
startActivity(intent);

//1.启动Service:可以使用Intent和Context的startService方法启动一个Service,例如:
Intent intent = new Intent(this, MyService.class);
startService(intent);


//2.注册Broadcast Receiver:可以通过在代码中注册Broadcast Receiver来接收系统广播消息,例如:
BroadcastReceiver receiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
registerReceiver(receiver, filter);


3.WindowManagerService: window management

WindowManagerService is responsible for managing windows in the Android system, including application windows, system prompt boxes, floating windows, etc. It is responsible for window creation, display, movement, resizing, etc.
Create a custom window: You can create a custom window by creating a custom View and adding it to the WindowManager, for example:

WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
MyCustomView myView = new MyCustomView(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
// 设置窗口的位置、大小等参数
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
windowManager.addView(myView, params);


4. PackageManagerService: Application startup

PackageManagerService is responsible for managing the installation, uninstallation, update and other operations of the application. It provides functions such as obtaining application information, launching applications, and more.
Obtain application information: You can use the methods of the PackageManager object to obtain application information, for example:

PackageManager packageManager = getPackageManager();
ApplicationInfo appInfo = packageManager.getApplicationInfo("com.example.app", 0);
String appName = appInfo.loadLabel(packageManager).toString();
Drawable icon = appInfo.loadIcon(packageManager);

//1.启动应用程序:可以使用PackageManager对象的方法来启动一个应用程序,例如:
PackageManager packageManager = getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage("com.example.app");
if (intent != null) {
    startActivity(intent);
}

This article is just a brief introduction to the Android startup life cycle process, and there are inevitably deficiencies. If there are any mistakes or improvements, please feel free to correct and suggest.

Guess you like

Origin blog.csdn.net/weixin_44715733/article/details/131036973