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

鸿蒙时代
发布于 2021-11-2 14:41
浏览
0收藏

在开发过程中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.97K 6次下载
1
收藏
回复
举报
回复
    相关推荐