今天導師給我將講了一些如何新建一個系統服務,以及如何去初始化。
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 中成功創建和啟動自定義系統服務。