#include "myappproxy.h"
#include "ipc_skeleton.h"
#include "system_ability_definition.h"
#include "iservice_registry.h"
#include "hitrace/trace.h"
#include "system_ability_load_callback_stub.h"
#include <iostream>
#include <future>
#include <utility>
using namespace std;
using namespace OHOS;
using namespace OHOS::myapp;
#undef LOG_TAG
#define LOG_TAG "MyAppClient"
static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN, LOG_TAG };
class CallBack : public SystemAbilityLoadCallbackStub
{
~CallBack() {
}
int32_t GetSystemAbilityId() const
{
return currSystemAbilityId;
}
sptr<IRemoteObject> GetRemoteObject() const
{
return currRemoteObject;
}
virtual void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject)
{
cout << "OnLoadSystemAbilitySuccess" << endl;
currSystemAbilityId = systemAbilityId;
currRemoteObject = remoteObject;
isload.set_value(true);
}
virtual void OnLoadSystemAbilityFail(int32_t systemAbilityId)
{
cout << "OnLoadSystemAbilityFail" << endl;
isload.set_value(false);
}
public:
bool getLoadResult(){
return futureToGet.get();
}
private:
int32_t currSystemAbilityId = 0;
sptr<IRemoteObject> currRemoteObject;
std::promise<bool> isload;
std::future<bool> futureToGet = isload.get_future();
};
sptr<IMyAppService> ConnectService()
{
auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (saMgr == nullptr) {
MYAPP_LOGE(LABEL, "get registry fail");
return nullptr;
}
// 加载动态库服务
sptr<CallBack> callback(new CallBack());
saMgr->LoadSystemAbility(MY_APP_SERVICE_ID, callback);
// 阻塞,等待isload传入值
bool laodResult = callback->getLoadResult();
(void)laodResult;
// 获取对象,后续可以进行业务数据交互
sptr<IRemoteObject> object = saMgr->GetSystemAbility(MY_APP_SERVICE_ID);
sptr<IMyAppService> client = nullptr;
if (object != nullptr) {
MYAPP_LOGE(LABEL, "Got MyApp Service object");
sptr<IRemoteObject::DeathRecipient> death(new MyAppDeathRecipient());
object->AddDeathRecipient(death.GetRefPtr());
// client 是一个proxy对象客户端类
client = iface_cast<IMyAppService>(object);
}
if (client == nullptr) {
MYAPP_LOGE(LABEL, "Could not find MyApp Service!");
return nullptr;
}
return client;
}
int main(int argc, char *argv[])
{
int result = 0;
auto client = ConnectService();
std::string name = client->GetName();
cout << "name: " << name << endl;
int32_t ret = client->SetBalance(1024);
cout << "after setbalance1: " << ret << endl;
ret = client->SetBalance(1024);
cout << "after setbalance2: " << ret << endl;
ret = client->GetBalance();
cout << "getbalance: " << ret << endl;
MYAPP_LOGE(LABEL, "get from service: %{public}d", result);
IPCSkeleton::JoinWorkThread();
return 0;
}
OpenHarmony IPC服务的动态加载(拉起)流程(L2)
需积分: 0 200 浏览量
2023-10-02
15:52:26
上传
评论
收藏 10KB RAR 举报

douluo998
- 粉丝: 444
- 资源: 706
最新资源
- sm2.js,前端加密算法,主要方法sm2EncryptPwd
- 人工智能-项目实践-jira-Python Jira library. Development chat
- Python俄罗斯方块Tetris源文件下载
- 基于Java 实现的LFU算法,特别适合新手,带有测试case
- 基于Java实现的LRU算法,特别适合新手,带有测试case
- 人工智能-项目实践-数据结构-冒泡排序、选择排序、快速排序、堆排序、插入排序、希尔排序、归并排序.zip
- 基于SpringBoot+Vue实现增删改查和分页查询DEMO(源码+数据库)作业
- C++ OnnxRuntime部署yolov8模型
- 人工智能-项目实践-数据结构-冒泡排序;直接插入排序;希尔排序;快速排序;堆排序;归并排序;基数排序.zip
- 人工智能-项目实践-数据结构-二叉树的层序遍历(左-右).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


