簡介
samgr組件是OpenHarmony的核心組件,提供OpenHarmony系統(tǒng)服務(wù)啟動、注冊、查詢等功能。
系統(tǒng)架構(gòu)
圖 1 系統(tǒng)服務(wù)管理系統(tǒng)架構(gòu)圖
說明
samgr服務(wù)接收到sa框架層發(fā)送的注冊消息,會在本地緩存中存入系統(tǒng)服務(wù)相關(guān)信息。
int32_t SystemAbilityManager::AddSystemAbility(int32_t systemAbilityId, const sptr< IRemoteObject >& ability, const SAExtraProp& extraProp) { if (!CheckInputSysAbilityId(systemAbilityId) || ability == nullptr) { HILOGE("AddSystemAbilityExtra input params is invalid."); return ERR_INVALID_VALUE; } { unique_lock< shared_mutex > writeLock(abilityMapLock_); auto saSize = abilityMap_.size(); if (saSize >= MAX_SERVICES) { HILOGE("map size error, (Has been greater than %zu)", saSize); return ERR_INVALID_VALUE; } SAInfo saInfo; saInfo.remoteObj = ability; saInfo.isDistributed = extraProp.isDistributed; saInfo.capability = extraProp.capability; saInfo.permission = Str16ToStr8(extraProp.permission); abilityMap_[systemAbilityId] = std::move(saInfo); HILOGI("insert %{public}d. size : %{public}zu", systemAbilityId, abilityMap_.size()); } RemoveCheckLoadedMsg(systemAbilityId); if (abilityDeath_ != nullptr) { ability- >AddDeathRecipient(abilityDeath_); } u16string strName = Str8ToStr16(to_string(systemAbilityId)); if (extraProp.isDistributed && dBinderService_ != nullptr) { dBinderService_- >RegisterRemoteProxy(strName, systemAbilityId); HILOGD("AddSystemAbility RegisterRemoteProxy, serviceId is %{public}d", systemAbilityId); } if (systemAbilityId == SOFTBUS_SERVER_SA_ID && !isDbinderStart_) { if (dBinderService_ != nullptr && rpcCallbackImp_ != nullptr) { bool ret = dBinderService_- >StartDBinderService(rpcCallbackImp_); HILOGI("start result is %{public}s", ret ? "succeed" : "fail"); isDbinderStart_ = true; } } SendSystemAbilityAddedMsg(systemAbilityId, ability); return ERR_OK; }
對于本地服務(wù)而言,samgr服務(wù)接收到sa框架層發(fā)送的獲取消息,會通過服務(wù)id,查找到對應(yīng)服務(wù)的代理對象,然后返回給sa框架。
sptr< IRemoteObject > SystemAbilityManager::CheckSystemAbility(int32_t systemAbilityId) { if (!CheckInputSysAbilityId(systemAbilityId)) { HILOGW("CheckSystemAbility CheckSystemAbility invalid!"); return nullptr; } shared_lock< shared_mutex > readLock(abilityMapLock_); auto iter = abilityMap_.find(systemAbilityId); if (iter != abilityMap_.end()) { HILOGI("found service : %{public}d.", systemAbilityId); return iter- >second.remoteObj; } HILOGI("NOT found service : %{public}d", systemAbilityId); return nullptr; }
動態(tài)加載系統(tǒng)服務(wù)進(jìn)程及SystemAbility, 系統(tǒng)進(jìn)程無需開機(jī)啟動,而是在SystemAbility被訪問的時候按需拉起,并加載指定SystemAbility。
3.1 繼承SystemAbilityLoadCallbackStub類,并覆寫OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject)、OnLoadSystemAbilityFail(int32_t systemAbilityId)方法。class OnDemandLoadCallback : public SystemAbilityLoadCallbackStub { public: void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr< IRemoteObject >& remoteObject) override; void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; }; void OnDemandLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr< IRemoteObject >& remoteObject) // systemAbilityId為指定加載的SAID,remoteObject為指定systemAbility的代理對象 { cout < < "OnLoadSystemAbilitySuccess systemAbilityId:" < < systemAbilityId < < " IRemoteObject result:" < < ((remoteObject != nullptr) ? "succeed" : "failed") < < endl; } void OnDemandLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) // systemAbilityId為指定加載的SAID { cout < < "OnLoadSystemAbilityFail systemAbilityId:" < < systemAbilityId < < endl; }
3.2 調(diào)用samgr提供的動態(tài)加載接口LoadSystemAbility(int32_t systemAbilityId, const sptr& callback)。
// 構(gòu)造步驟1的SystemAbilityLoadCallbackStub子類的實(shí)例 sptr< OnDemandLoadCallback > loadCallback_ = new OnDemandLoadCallback(); // 調(diào)用LoadSystemAbility方法 sptr< ISystemAbilityManager > sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (sm == nullptr) { cout < < "GetSystemAbilityManager samgr object null!" < < endl; return; } int32_t result = sm- >LoadSystemAbility(systemAbilityId, loadCallback_); if (result != ERR_OK) { cout < < "systemAbilityId:" < < systemAbilityId < < " load failed, result code:" < < result < < endl; return; }
鴻蒙開發(fā)知識更新,前往[qr23.cn/AKFP8k
]可參考
說明:
1.LoadSystemAbility方法調(diào)用成功后,指定SystemAbility加載成功后會觸發(fā)回調(diào)OnLoadSystemAbilitySuccess,加載失敗觸發(fā)回調(diào)OnLoadSystemAbilityFail。
2.動態(tài)加載的進(jìn)程cfg文件不能配置為開機(jī)啟動,需指定"ondemand" : true, 示例如下:
{ "services" : [{ "name" : "listen_test", "path" : ["/system/bin/sa_main", "/system/profile/listen_test.json"], "ondemand" : true, "uid" : "system", "gid" : ["system", "shell"] } ] }
3.LoadSystemAbility方法適用于動態(tài)加載場景,其他獲取SystemAbility場景建議使用CheckSystemAbility方法。
4.cfg里進(jìn)程名稱需要與SA的配置json文件里進(jìn)程名保持一致
審核編輯 黃宇
-
鴻蒙
+關(guān)注
關(guān)注
57文章
2392瀏覽量
43055 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3747瀏覽量
16585
發(fā)布評論請先 登錄
相關(guān)推薦
鴻蒙實(shí)戰(zhàn)開發(fā):【FaultLoggerd組件】講解
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>實(shí)戰(zhàn)</b><b class='flag-5'>開發(fā)</b>:【FaultLoggerd組件】講解](https://file1.elecfans.com/web2/M00/C4/EC/wKgaomXwC62AFcUeAADr4EKy7Mk552.jpg)
鴻蒙原生應(yīng)用/元服務(wù)實(shí)戰(zhàn)-AGC團(tuán)隊賬戶
鴻蒙實(shí)戰(zhàn)項目開發(fā):【短信服務(wù)】
鴻蒙Flutter實(shí)戰(zhàn):07混合開發(fā)
鴻蒙原生開發(fā)手記:01-元服務(wù)開發(fā)
鴻蒙Flutter實(shí)戰(zhàn):14-現(xiàn)有Flutter 項目支持鴻蒙 II
【專家問答】楊光明:鴻蒙系統(tǒng)研發(fā)工程師教你從0開發(fā)鴻蒙PCB開發(fā)板
《HarmonyOS原子化服務(wù)卡片原理與實(shí)戰(zhàn)》清華大學(xué)出版社李洋著
鴻蒙原生應(yīng)用/元服務(wù)開發(fā)-AGC分發(fā)如何下載管理Profile
鴻蒙系統(tǒng)是基于什么開發(fā)的
華為開發(fā)者大會2021年亮點(diǎn):鴻蒙系統(tǒng)的原子化服務(wù)開發(fā)
![華為<b class='flag-5'>開發(fā)</b>者大會2021年亮點(diǎn):<b class='flag-5'>鴻蒙</b><b class='flag-5'>系統(tǒng)</b>的原子化<b class='flag-5'>服務(wù)</b><b class='flag-5'>開發(fā)</b>](https://file.elecfans.com/web2/M00/18/F8/poYBAGFya_qAXiXbAAKhhoiZQOA095.png)
鴻蒙開發(fā)實(shí)戰(zhàn):【包管理子系統(tǒng)】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>開發(fā)</b><b class='flag-5'>實(shí)戰(zhàn)</b>:【包<b class='flag-5'>管理</b>子<b class='flag-5'>系統(tǒng)</b>】](https://file1.elecfans.com/web2/M00/C4/53/wKgZomXyo6KAN6TlAACm5srp6is788.jpg)
鴻蒙實(shí)戰(zhàn)開發(fā)學(xué)習(xí)【FaultLoggerd組件】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>實(shí)戰(zhàn)</b><b class='flag-5'>開發(fā)</b>學(xué)習(xí)【FaultLoggerd組件】](https://file1.elecfans.com/web2/M00/C4/EC/wKgaomXwC62AFcUeAADr4EKy7Mk552.jpg)
鴻蒙開發(fā)實(shí)戰(zhàn):【系統(tǒng)服務(wù)管理部件】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>開發(fā)</b><b class='flag-5'>實(shí)戰(zhàn)</b>:【<b class='flag-5'>系統(tǒng)</b><b class='flag-5'>服務(wù)</b><b class='flag-5'>管理</b><b class='flag-5'>部件</b>】](https://file1.elecfans.com/web2/M00/C5/E7/wKgaomX5QZCAfxw_AACzizMxEnU905.jpg)
鴻蒙開發(fā)實(shí)戰(zhàn):【系統(tǒng)服務(wù)框架部件】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>開發(fā)</b><b class='flag-5'>實(shí)戰(zhàn)</b>:【<b class='flag-5'>系統(tǒng)</b><b class='flag-5'>服務(wù)</b>框架<b class='flag-5'>部件</b>】](https://file1.elecfans.com/web2/M00/C5/46/wKgZomX74I6AIXj-AABMB5XL5b8920.jpg)
評論