android system UI 基础的基础

发布于:2024-06-19 ⋅ 阅读:(108) ⋅ 点赞:(0)

Android 系统中的 SystemUI 是一种特殊的应用程序,它负责管理和显示设备的用户界面组件,例如状态栏、导航栏和最近任务列表等。SystemUI 是在 Android 启动过程中由 Zygote 进程启动的。以下是 SystemUI 启动过程的详细步骤:

SystemUI 启动过程

  • 1.启动 init 进程

    • Android 启动时,init 进程是第一个运行的用户空间进程。它会读取初始化脚本(通常是 /init.rc)来启动其他系统服务。
  • 2.启动 Zygote 进程

    • init 进程会启动 Zygote 进程。Zygote 是 Android 的应用程序进程启动器,所有的应用程序进程都是由 Zygote 派生出来的。
    • 在启动过程中,Zygote 会预加载一些核心类和资源,以加快应用程序的启动速度。
  • 3.启动 SystemServer 进程

    • Zygote 进程会启动 SystemServer 进程。SystemServer 是一个关键的系统进程,负责启动各种系统服务,包括 Activity Manager、Package Manager、Window Manager 等。
  • 4.启动 SystemUI 服务

    • SystemServer 进程会启动 SystemUI 应用程序。具体地,SystemUI 的启动是由 SystemServer 中的 SystemUIService 类来处理的。
    • SystemUI 的启动代码位于 com.android.systemui.SystemUIApplication 类中,该类会初始化各种系统 UI 组件。

具体启动代码示例 

以下是一些关键代码段,展示了 SystemUI 是如何启动的:

SystemServer.java 中启动 SystemUI 的代码

 

private void startOtherServices() {
    // ... other service starts ...

    // Start SystemUI
    traceBeginAndSlog("StartSystemUI");
    try {
        startSystemUi(context);
    } catch (Throwable e) {
        reportWtf("starting System UI", e);
    }
    traceEnd();
    
    // ... other service starts ...
}

 startSystemUi 方法

 

private void startSystemUi(Context context) {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.android.systemui",
            "com.android.systemui.SystemUIService"));
    context.startServiceAsUser(intent, UserHandle.SYSTEM);
}

SystemUIApplication.java 初始化

public class SystemUIApplication extends Application {
    private List<SystemUI> mServices;

    @Override
    public void onCreate() {
        super.onCreate();
        mServices = new ArrayList<>();
        // Add different SystemUI components here, such as StatusBar, NavigationBar, etc.
        mServices.add(new StatusBar(this));
        mServices.add(new NavigationBar(this));
        // Initialize all services
        for (SystemUI service : mServices) {
            service.start();
        }
    }
}

总结

SystemUI 是在 Android 启动过程中由 SystemServer 进程通过 Zygote 进程启动的。SystemServer 通过调用 startSystemUi 方法来启动 SystemUI 应用程序,该应用程序的入口是 SystemUIApplication 类,它会初始化和启动各种系统 UI 组件。


网站公告

今日签到

点亮在社区的每一天
去签到