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. 注意事项
- 启动顺序:服务的启动顺序很重要,依赖其他服务的服务应该在依赖服务之后启动
- 异常处理:使用 try-catch 块捕获服务启动过程中的异常
- 性能考虑:避免在服务启动过程中执行耗时操作
- 权限控制:确保服务有适当的权限检查
- 进程间通信:如果服务需要跨进程访问,确保正确实现 Binder 接口
7. 调试技巧
-
使用
dumpsys
命令检查服务状态:adb shell dumpsys my_custom_service
-
查看服务列表:
adb shell service list
-
检查系统日志:
adb logcat -s SystemServer
通过以上方式,你可以在 Android SystemServer 中成功创建和启动自定义系统服务。