当前位置: 首页 > news >正文

Android SystemServer 中 Service 的创建和启动方式

今天导师给我将讲了一些如何新建一个系统服务,以及如何去初始化。

Android SystemServer 中 Service 的创建和启动方式

在 Android 系统中,SystemServer 是系统服务的核心进程,负责启动和管理各种系统服务。以下是 SystemServer 中服务创建和启动的详细方式:

1. SystemServer 概述

SystemServer 是 Android 系统启动过程中的关键组件,它运行在 system_server 进程中,负责启动和管理所有核心系统服务。

2. 服务启动流程

2.1 主要启动阶段

SystemServer 的服务启动分为几个主要阶段:

// frameworks/base/services/java/com/android/server/SystemServer.java
private void run() {// 1. 启动引导服务 (Bootstrap Services)startBootstrapServices();// 2. 启动核心服务 (Core Services)startCoreServices();// 3. 启动其他服务 (Other Services)startOtherServices();// 4. 系统就绪后的回调startSystemUi(context);systemReady();
}

2.2 引导服务启动

// frameworks/base/services/java/com/android/server/SystemServer.java
private void startBootstrapServices() {// 启动 ActivityManagerServicemActivityManagerService = ActivityManagerService.Lifecycle.startService(mSystemServiceManager, atm);mActivityManagerService.setSystemProcess();// 启动 PowerManagerServicemPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);// 启动 PackageManagerServicemPackageManagerService = PackageManagerService.main(mSystemContext, installer,mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);// 更多引导服务...
}

2.3 核心服务启动

private void startCoreServices() {// 启动 BatteryServicemSystemServiceManager.startService(BatteryService.class);// 启动 UsageStatsServicemSystemServiceManager.startService(UsageStatsService.class);mActivityManagerService.setUsageStatsManager(LocalServices.getService(UsageStatsManagerInternal.class));// 启动 WebViewUpdateServicemWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);// 更多核心服务...
}

2.4 其他服务启动

private void startOtherServices() {// 启动 WindowManagerServicewm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false);ServiceManager.addService(Context.INPUT_SERVICE, inputManager);// 启动 NotificationManagerServicemSystemServiceManager.startService(NotificationManagerService.class);// 启动自定义服务(如你的 MDM 服务)try {traceBeginAndSlog("StartMdmPolicyManager");mdmPolicyManager = new MdmPolicyManagerService(context);ServiceManager.addService(Context.MDM_POLICY_SERVICE, mdmPolicyManager);traceEnd();} catch (Throwable e) {reportWtf("starting Mdm Policy Manager", e);}// 更多其他服务...
}

3. 服务创建方式

3.1 使用 SystemServiceManager 启动

这是推荐的方式,适用于继承自 SystemService 的服务:

// 在 SystemServer 中
mSystemServiceManager.startService(YourService.class);// 服务类定义
public class YourService extends SystemService {public YourService(Context context) {super(context);}@Overridepublic void onStart() {// 服务启动逻辑publishBinderService(Context.YOUR_SERVICE, new YourBinder());}@Overridepublic void onBootPhase(int phase) {if (phase == SystemService.PHASE_BOOT_COMPLETED) {// 启动完成后的操作}}
}

3.2 直接实例化并注册

对于不继承 SystemService 的服务:

// 创建服务实例
YourService yourService = new YourService(context);// 添加到 ServiceManager
ServiceManager.addService(Context.YOUR_SERVICE, yourService);// 或者使用带权限的添加方式
ServiceManager.addService(Context.YOUR_SERVICE, yourService, false, ServiceManager.DUMP_FLAG_PRIORITY_DEFAULT);

3.3 使用静态 main 方法

某些服务有静态的 main() 方法:

// 服务类中的静态方法
public static YourService main(Context context) {YourService service = new YourService(context);ServiceManager.addService(Context.YOUR_SERVICE, service);return service;
}// 在 SystemServer 中调用
YourService.main(mSystemContext);

4. 服务生命周期管理

4.1 启动阶段(Boot Phases)

系统服务可以在不同的启动阶段执行初始化:

public class YourService extends SystemService {// ...@Overridepublic void onBootPhase(int phase) {if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {// 第三方应用可以启动时的初始化} else if (phase == PHASE_BOOT_COMPLETED) {// 系统启动完成后的操作}}
}

4.2 系统就绪回调

private void systemReady() {// 通知所有服务系统已就绪mActivityManagerService.systemReady(() -> {// 系统就绪后的操作}, BOOT_TIMINGS_TRACE_LOG);
}

5. 自定义服务示例

以下是在 SystemServer 中添加自定义服务的完整示例:

5.1 服务接口定义 (AIDL)

// frameworks/base/core/java/android/app/IMyCustomService.aidl
package android.app;interface IMyCustomService {void doSomething(int param);int getSomething();
}

5.2 服务实现

// frameworks/base/services/core/java/com/android/server/MyCustomService.java
package com.android.server;import android.app.IMyCustomService;
import android.content.Context;
import android.os.IBinder;
import android.util.Slog;public class MyCustomService extends IMyCustomService.Stub {private static final String TAG = "MyCustomService";private final Context mContext;public MyCustomService(Context context) {mContext = context;Slog.i(TAG, "MyCustomService created");}@Overridepublic void doSomething(int param) {Slog.d(TAG, "doSomething called with param: " + param);// 实现具体功能}@Overridepublic int getSomething() {Slog.d(TAG, "getSomething called");return 42; // 示例返回值}
}

5.3 在 SystemServer 中启动服务

// frameworks/base/services/java/com/android/server/SystemServer.java
public final class SystemServer {// ...private void startOtherServices() {// ...// 启动自定义服务try {traceBeginAndSlog("StartMyCustomService");MyCustomService myCustomService = new MyCustomService(context);ServiceManager.addService(Context.MY_CUSTOM_SERVICE, myCustomService);traceEnd();} catch (Throwable e) {reportWtf("starting My Custom Service", e);}// ...}
}

5.4 在 Context 中定义服务常量

// frameworks/base/core/java/android/content/Context.java
public abstract class Context {// ...public static final String MY_CUSTOM_SERVICE = "my_custom_service";// ...
}

6. 注意事项

  1. 启动顺序:服务的启动顺序很重要,依赖其他服务的服务应该在依赖服务之后启动
  2. 异常处理:使用 try-catch 块捕获服务启动过程中的异常
  3. 性能考虑:避免在服务启动过程中执行耗时操作
  4. 权限控制:确保服务有适当的权限检查
  5. 进程间通信:如果服务需要跨进程访问,确保正确实现 Binder 接口

7. 调试技巧

  1. 使用 dumpsys 命令检查服务状态:

    adb shell dumpsys my_custom_service
    
  2. 查看服务列表:

    adb shell service list
    
  3. 检查系统日志:

    adb logcat -s SystemServer
    

通过以上方式,你可以在 Android SystemServer 中成功创建和启动自定义系统服务。

http://www.lryc.cn/news/625940.html

相关文章:

  • 代码随想录Day56:图论(冗余连接、冗余连接II)
  • CLIK-Diffusion:用于牙齿矫正的临床知识感知扩散模型|文献速递-深度学习人工智能医疗图像
  • 心路历程-启动流程的概念
  • 如何让你的知识分享更有说服力?
  • RNN如何将文本压缩为256维向量
  • AC内容审计技术
  • 单一职责原则(SRP)深度解析
  • django生成迁移文件,执行生成到数据库
  • CNN-LSTM-Attention、CNN-LSTM、LSTM三模型多变量时序光伏功率预测
  • 开源 GIS 服务器搭建:GeoServer 在 Linux 系统上的部署教程
  • Scikit-learn通关秘籍:从鸢尾花分类到房价预测
  • Vim笔记:缩进
  • 从一个ctf题中学到的多种php disable_functions bypass 姿势
  • 重塑酒店投屏体验:私密投屏技术的革新应用
  • 基于单片机智能点滴输液系统
  • 24.早期目标检测
  • 2025年- H99-Lc207--32.最长有效括号(栈、动态规划)--Java版
  • strlen 函数的使用与模拟实现
  • 云原生俱乐部-mysql知识点归纳(2)
  • Java网络编程:TCP与UDP通信实现及网络编程基础
  • 无人机场景 - 目标检测数据集 - 山林野火烟雾检测数据集下载「包含VOC、COCO、YOLO三种格式」
  • FastAPI 请求详解:全面掌握各种请求类型处理
  • 《基于大数据的全球用水量数据可视化分析系统》用Python+Django开发,为什么导师却推荐用Java+Spring Boot?真相揭秘……
  • 实践项目-1
  • Matplotlib数据可视化实战:Matplotlib图表注释与美化入门
  • LeetCode100-560和为K的子数组
  • Rust学习笔记(七)|错误处理
  • 2025年渗透测试面试题总结-21(题目+回答)
  • 堆、方法区、虚拟机栈、本地方法栈、程序计数器
  • RabbitMQ:SpringAMQP 多消费者绑定同一队列