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

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.xdnf.cn/news/18387.html

相关文章:

  • AI与大数据驱动下的食堂采购系统源码:供应链管理平台的未来发展
  • Git#cherry-pick
  • QT示例 基于Subdiv2D的Voronoi图实现鼠标点击屏幕碎裂掉落特效
  • Day22 顺序表与链表的实现及应用(含字典功能与操作对比)
  • 服务器无公网ip如何对外提供服务?本地网络只有内网IP,如何能被外网访问?
  • Vue.prototype 的作用
  • JUC之CompletableFuture【中】
  • Redis Reactor 模型详解【基本架构、事件循环机制、结合源码详细追踪读写请求从客户端连接到命令执行的完整流程】
  • FPGA 在情绪识别领域的护理应用(一)
  • 论文阅读系列(一)Qwen-Image Technical Report
  • 中和农信如何打通农业科技普惠“最后一百米”
  • 企业架构是什么?解读
  • 通过分布式系统的视角看Kafka
  • python黑盒包装
  • Matplotlib数据可视化实战:Matplotlib图表注释与美化入门
  • 抓取手机游戏相关数据
  • LWIP流程全解
  • java实现url 生成二维码, 包括可叠加 logo、改变颜色、设置背景颜色、背景图等功能,完整代码示例
  • 【运维进阶】Ansible 角色管理
  • 记一次 .NET 某自动化智能制造软件 卡死分析
  • 流程进阶——解读 49页 2023 IBM流程管理与变革赋能【附全文阅读】
  • Redis缓存加速测试数据交互:从前缀键清理到前沿性能革命
  • 微服务-07.微服务拆分-微服务项目结构说明
  • 236. 二叉树的最近公共祖先
  • 从密度到聚类:DBSCAN算法的第一性原理解析
  • 100202Title和Input组件_编辑器-react-仿低代码平台项目
  • git 创用操作
  • 【集合框架LinkedList底层添加元素机制】
  • Python网络爬虫全栈教程 – 从基础到实战
  • 网络编程day4