赞
踩
samgr组件是OpenHarmony的核心组件,提供OpenHarmony系统服务启动、注册、查询等功能。
图 1 系统服务管理系统架构图
/foundation/distributedschedule
├── samgr
│ ├── bundle.json # 部件描述及编译文件
│ ├── frameworks # 框架实现存在目录
│ ├── interfaces # 接口目录
│ ├── services # 组件服务端目录
│ ├── test # 测试代码存放目录
│ ├── utils # 工具类目录
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; }
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; }
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 调用samgr提供的动态加载接口LoadSystemAbility(int32_t systemAbilityId, const sptr& callback)。
// 构造步骤1的SystemAbilityLoadCallbackStub子类的实例
sptr<OnDemandLoadCallback> loadCallback_ = new OnDemandLoadCallback();
// 调用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;
}
说明:
1.LoadSystemAbility方法调用成功后,指定SystemAbility加载成功后会触发回调OnLoadSystemAbilitySuccess,加载失败触发回调OnLoadSystemAbilityFail。
2.动态加载的进程cfg文件不能配置为开机启动,需指定"ondemand" : true, 示例如下:
{
"services" : [{
"name" : "listen_test",
"path" : ["/system/bin/sa_main", "/system/profile/listen_test.xml"],
"ondemand" : true,
"uid" : "system",
"gid" : ["system", "shell"]
}
]
}
3.LoadSystemAbility方法适用于动态加载场景,其他获取SystemAbility场景建议使用CheckSystemAbility方法。
4.cfg里进程名称需要与SA的配置xml文件里进程名保持一致
为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05
https://qr21.cn/FV7h05
https://qr21.cn/FV7h05
https://qr21.cn/FV7h05
https://qr18.cn/F781PH
https://qr18.cn/F781PH
1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。