Android SystemServer 中 Service 的創(chuàng)建和啟動(dòng)流程
今天導(dǎo)師給我將講了一些如何新建一個(gè)系統(tǒng)服務(wù),以及如何去初始化。
Android SystemServer 中 Service 的創(chuàng)建和啟動(dòng)方式
在 Android 系統(tǒng)中,SystemServer 是系統(tǒng)服務(wù)的核心進(jìn)程,負(fù)責(zé)啟動(dòng)和管理各種系統(tǒng)服務(wù)。以下是 SystemServer 中服務(wù)創(chuàng)建和啟動(dòng)的詳細(xì)方式:
1. SystemServer 概述
SystemServer 是 Android 系統(tǒng)啟動(dòng)過(guò)程中的關(guān)鍵組件,它運(yùn)行在 system_server 進(jìn)程中,負(fù)責(zé)啟動(dòng)和管理所有核心系統(tǒng)服務(wù)。
2. 服務(wù)啟動(dòng)流程
2.1 主要啟動(dòng)階段
SystemServer 的服務(wù)啟動(dòng)分為幾個(gè)主要階段:
// frameworks/base/services/java/com/android/server/SystemServer.java
private void run() {
// 1. 啟動(dòng)引導(dǎo)服務(wù) (Bootstrap Services)
startBootstrapServices();
// 2. 啟動(dòng)核心服務(wù) (Core Services)
startCoreServices();
// 3. 啟動(dòng)其他服務(wù) (Other Services)
startOtherServices();
// 4. 系統(tǒng)就緒后的回調(diào)
startSystemUi(context);
systemReady();
}2.2 引導(dǎo)服務(wù)啟動(dòng)
// frameworks/base/services/java/com/android/server/SystemServer.java
private void startBootstrapServices() {
// 啟動(dòng) ActivityManagerService
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemProcess();
// 啟動(dòng) PowerManagerService
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
// 啟動(dòng) PackageManagerService
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
// 更多引導(dǎo)服務(wù)...
}2.3 核心服務(wù)啟動(dòng)
private void startCoreServices() {
// 啟動(dòng) BatteryService
mSystemServiceManager.startService(BatteryService.class);
// 啟動(dòng) UsageStatsService
mSystemServiceManager.startService(UsageStatsService.class);
mActivityManagerService.setUsageStatsManager(
LocalServices.getService(UsageStatsManagerInternal.class));
// 啟動(dòng) WebViewUpdateService
mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);
// 更多核心服務(wù)...
}2.4 其他服務(wù)啟動(dòng)
private void startOtherServices() {
// 啟動(dòng) WindowManagerService
wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false);
ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
// 啟動(dòng) NotificationManagerService
mSystemServiceManager.startService(NotificationManagerService.class);
// 啟動(dòng)自定義服務(wù)(如你的 MDM 服務(wù))
try {
traceBeginAndSlog("StartMdmPolicyManager");
mdmPolicyManager = new MdmPolicyManagerService(context);
ServiceManager.addService(Context.MDM_POLICY_SERVICE, mdmPolicyManager);
traceEnd();
} catch (Throwable e) {
reportWtf("starting Mdm Policy Manager", e);
}
// 更多其他服務(wù)...
}3. 服務(wù)創(chuàng)建方式
3.1 使用 SystemServiceManager 啟動(dòng)
這是推薦的方式,適用于繼承自 SystemService 的服務(wù):
// 在 SystemServer 中
mSystemServiceManager.startService(YourService.class);
// 服務(wù)類定義
public class YourService extends SystemService {
public YourService(Context context) {
super(context);
}
@Override
public void onStart() {
// 服務(wù)啟動(dòng)邏輯
publishBinderService(Context.YOUR_SERVICE, new YourBinder());
}
@Override
public void onBootPhase(int phase) {
if (phase == SystemService.PHASE_BOOT_COMPLETED) {
// 啟動(dòng)完成后的操作
}
}
}3.2 直接實(shí)例化并注冊(cè)
對(duì)于不繼承 SystemService 的服務(wù):
// 創(chuàng)建服務(wù)實(shí)例
YourService yourService = new YourService(context);
// 添加到 ServiceManager
ServiceManager.addService(Context.YOUR_SERVICE, yourService);
// 或者使用帶權(quán)限的添加方式
ServiceManager.addService(Context.YOUR_SERVICE, yourService,
false, ServiceManager.DUMP_FLAG_PRIORITY_DEFAULT);3.3 使用靜態(tài) main 方法
某些服務(wù)有靜態(tài)的 main() 方法:
// 服務(wù)類中的靜態(tài)方法
public static YourService main(Context context) {
YourService service = new YourService(context);
ServiceManager.addService(Context.YOUR_SERVICE, service);
return service;
}
// 在 SystemServer 中調(diào)用
YourService.main(mSystemContext);4. 服務(wù)生命周期管理
4.1 啟動(dòng)階段(Boot Phases)
系統(tǒng)服務(wù)可以在不同的啟動(dòng)階段執(zhí)行初始化:
public class YourService extends SystemService {
// ...
@Override
public void onBootPhase(int phase) {
if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
// 第三方應(yīng)用可以啟動(dòng)時(shí)的初始化
} else if (phase == PHASE_BOOT_COMPLETED) {
// 系統(tǒng)啟動(dòng)完成后的操作
}
}
}4.2 系統(tǒng)就緒回調(diào)
private void systemReady() {
// 通知所有服務(wù)系統(tǒng)已就緒
mActivityManagerService.systemReady(() -> {
// 系統(tǒng)就緒后的操作
}, BOOT_TIMINGS_TRACE_LOG);
}5. 自定義服務(wù)示例
以下是在 SystemServer 中添加自定義服務(wù)的完整示例:
5.1 服務(wù)接口定義 (AIDL)
// frameworks/base/core/java/android/app/IMyCustomService.aidl
package android.app;
interface IMyCustomService {
void doSomething(int param);
int getSomething();
}5.2 服務(wù)實(shí)現(xiàn)
// 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");
}
@Override
public void doSomething(int param) {
Slog.d(TAG, "doSomething called with param: " + param);
// 實(shí)現(xiàn)具體功能
}
@Override
public int getSomething() {
Slog.d(TAG, "getSomething called");
return 42; // 示例返回值
}
}5.3 在 SystemServer 中啟動(dòng)服務(wù)
// frameworks/base/services/java/com/android/server/SystemServer.java
public final class SystemServer {
// ...
private void startOtherServices() {
// ...
// 啟動(dòng)自定義服務(wù)
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 中定義服務(wù)常量
// frameworks/base/core/java/android/content/Context.java
public abstract class Context {
// ...
public static final String MY_CUSTOM_SERVICE = "my_custom_service";
// ...
}6. 注意事項(xiàng)
- 啟動(dòng)順序:服務(wù)的啟動(dòng)順序很重要,依賴其他服務(wù)的服務(wù)應(yīng)該在依賴服務(wù)之后啟動(dòng)
- 異常處理:使用 try-catch 塊捕獲服務(wù)啟動(dòng)過(guò)程中的異常
- 性能考慮:避免在服務(wù)啟動(dòng)過(guò)程中執(zhí)行耗時(shí)操作
- 權(quán)限控制:確保服務(wù)有適當(dāng)?shù)臋?quán)限檢查
- 進(jìn)程間通信:如果服務(wù)需要跨進(jìn)程訪問(wèn),確保正確實(shí)現(xiàn) Binder 接口
7. 調(diào)試技巧
使用 dumpsys 命令檢查服務(wù)狀態(tài):
adb shell dumpsys my_custom_service
查看服務(wù)列表:
adb shell service list
檢查系統(tǒng)日志:
adb logcat -s SystemServer
通過(guò)以上方式,你可以在 Android SystemServer 中成功創(chuàng)建和啟動(dòng)自定義系統(tǒng)服務(wù)。
到此這篇關(guān)于Android SystemServer 中 Service 的創(chuàng)建和啟動(dòng)流程的文章就介紹到這了,更多相關(guān)Android SystemServer Service創(chuàng)建啟動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android?app啟動(dòng)圖適配方法實(shí)例
大家在瀏覽app啟動(dòng)頁(yè)的時(shí)候,一定遇到過(guò)在部分機(jī)型中圖片變形、頁(yè)面不協(xié)調(diào)、文案被裁剪的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于Android?app啟動(dòng)圖適配的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
Android實(shí)現(xiàn)仿魅族日歷首頁(yè)功能
這篇文章主要介紹了Android實(shí)現(xiàn)仿魅族日歷首頁(yè)功能的實(shí)現(xiàn)過(guò)程以及相關(guān)代碼講解分享,對(duì)此有興趣的朋友參考下。2018-02-02
Android媒體通知欄多系統(tǒng)適配實(shí)例講解
對(duì)于Android來(lái)說(shuō)其中一項(xiàng)很方便的操作便是下拉菜單,下拉菜單欄可以快捷打開(kāi)某項(xiàng)設(shè)置,這篇文章主要給大家介紹了關(guān)于Android通知欄增加快捷開(kāi)關(guān)的功能實(shí)現(xiàn),需要的朋友可以參考下2023-04-04
使用Android studio查看Kotlin的字節(jié)碼教程
這篇文章主要介紹了使用Android studio查看Kotlin的字節(jié)碼教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android實(shí)現(xiàn)讓圖片在屏幕上任意移動(dòng)的方法(拖拽功能)
這篇文章主要介紹了Android實(shí)現(xiàn)讓圖片在屏幕上任意移動(dòng)的方法,實(shí)例分析了Android拖拽功能的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08
Android開(kāi)發(fā)手冊(cè)SeekBar拖動(dòng)條使用實(shí)例
這篇文章主要為大家介紹了Android開(kāi)發(fā)手冊(cè)SeekBar拖動(dòng)條使用實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android實(shí)現(xiàn)從網(wǎng)絡(luò)獲取圖片顯示并保存到SD卡的方法
這篇文章主要介紹了Android實(shí)現(xiàn)從網(wǎng)絡(luò)獲取圖片顯示并保存到SD卡的方法,涉及Android操作多媒體文件及系統(tǒng)硬件設(shè)備的相關(guān)技巧,需要的朋友可以參考下2015-12-12
Android編程計(jì)算函數(shù)時(shí)間戳的相關(guān)方法總結(jié)
這篇文章主要介紹了Android編程計(jì)算函數(shù)時(shí)間戳的相關(guān)方法,結(jié)合實(shí)例形式總結(jié)分析了Android Java、Native、Kernel時(shí)間戳計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2017-05-05

