HarmonyOS应用开发-公共事件处理 - HarmonyOS技术社区 - 电子技术william hill官网 - 广受欢迎的专业电子william hill官网 - 威廉希尔官方网站
分享 收藏 返回

[文章]

HarmonyOS应用开发-公共事件处理

在开发过程中service想要控制多个ability时,可以考虑使用公共事件处理。
发布无序的公共事件:
//发布公共事件 同步修改卡片与页面
public void subscribeEvent(String id,String status) {
    try {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction("play")
                .build();
        intent.setOperation(operation);
        intent.setParam("id",id);
        intent.setParam("status",status);
        CommonEventData eventData = new CommonEventData(intent);

        CommonEventManager.publishCommonEvent(eventData);
        HiLog.info(TAG, "Publish succeeded");
    } catch (RemoteException e) {
        HiLog.error(TAG, "Exception occurred during publishCommonEvent invocation.");
    }

}
订阅该公共事件:
class MyCommonEventSubscriber extends CommonEventSubscriber {
    MyCommonEventSubscriber(CommonEventSubscribeInfo info) {
        super(info);

    }
    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
        HiLog.info(TAG,"订阅Seccess");
        id= Integer.parseInt(commonEventData.getIntent().getStringParam("id"));
        status=commonEventData.getIntent().getStringParam("status");
        HiLog.info(TAG,"订阅数据状态="+status);
        HiLog.info(TAG,"订阅数据歌曲id="+id);
    }
}
本文根据官方文档学习体验整理
附件:
HarmonyOS应用开发-公共事件处理.docx (13.97 KB)
(下载次数: 0, 2021-11-2 15:15 上传)


更多回帖

×
发帖