# CommonEventService **Repository Path**: cxshu/common-event-service ## Basic Information - **Project Name**: CommonEventService - **Description**: 本示例演示了HarmonyOS公共事件的发布与订阅 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-09-17 - **Last Updated**: 2021-09-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 一、概述 每个应用都可以订阅自己感兴趣的公共事件,订阅成功后且公共事件发布后,系统会把其发送给应用。这些公共事件可能来自系统、其他应用和应用自身。 发布公共事件需要借助CommonEventData对象,接收公共事件需要继承CommonEventSubscriber类并实现onReceiveEvent回调函数。 # 二、开发步骤 ## 无序公共事件 * 发布 1. 构建一个Intent对象,包含了自定义的事件的标识符 2. 构建CommonEventData对象 3. 核心的发布事件的动作 ```java Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withAction("com.cxs.commoneventtest.event") .build(); intent.setOperation(operation); intent.setParam("key1", "value1"); CommonEventData commonEventData = new CommonEventData(intent); try { CommonEventManager.publishCommonEvent(commonEventData); } catch (RemoteException e) { e.printStackTrace(); } ``` * 订阅 1. 创建CommonEventSubscriber派生类,在onReceiveEvent()回调函数中处理公共事件。 > 注意:此处不要执行耗时操作,否则会阻塞UI线程,产生用户点击没有反应等异常。 ```java public class MyCommonEventSubscriber extends CommonEventSubscriber { public MyCommonEventSubscriber(CommonEventSubscribeInfo subscribeInfo) { super(subscribeInfo); } @Override public void onReceiveEvent(CommonEventData commonEventData) { Intent intent = commonEventData.getIntent(); IntentParams params = intent.getParams(); System.out.println(params.getParam("key1")); System.out.println("在UI主线程中执行的不耗时方法"); } } ``` 2. 构造MyCommonEventSubscriber对象,调用CommonEventManager.subscribeCommonEvent()接口进行订阅。 ```java MatchingSkills matchingSkills = new MatchingSkills(); matchingSkills.addEvent("com.cxs.commoneventtest.event"); CommonEventSubscribeInfo subscribe = new CommonEventSubscribeInfo(matchingSkills); try { CommonEventManager.subscribeCommonEvent(new MyCommonEventSubscriber(subscribe)); System.out.println("订阅事件完成"); } catch (RemoteException e) { e.printStackTrace(); } ``` ## 带权限的公共事件 * 发布 1. 自定义权限 ```json "defPermissions": [ { "name": "com.cxs.commoneventtest.permission" } ] ``` 2. 发布事件 ```java MatchingSkills matchingSkills = new MatchingSkills(); matchingSkills.addEvent("com.cxs.commoneventtest.event"); CommonEventSubscribeInfo subscribe = new CommonEventSubscribeInfo(matchingSkills); try { CommonEventManager.subscribeCommonEvent(new MyCommonEventSubscriber(subscribe)); System.out.println("订阅事件结束了"); } catch (RemoteException e) { e.printStackTrace(); } ``` * 订阅 1. 需要申请发布事件时自定义的权限 ```json "reqPermissions": [ { "name": "com.cxs.commoneventtest.permission", } ], ``` 2. (同订阅无序公共事件)创建CommonEventSubscriber派生类,在onReceiveEvent()回调函数中处理公共事件。 3. (同订阅无序公共事件)构造MyCommonEventSubscriber对象,调用CommonEventManager.subscribeCommonEvent()接口进行订阅。 ## 有序公共事件 有序公共事件,按照优先级顺序执行发布事件后的onReceiveEvent方法 * 发布 1. 构造CommonEventPublishInfo对象,通过setOrdered(true)指定公共事件属性为有序公共事件。 2. 通过CommonEventData对象的setData()可以传递参数 ```java Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withAction("com.cxs.commoneventtest.event") .build(); intent.setOperation(operation); CommonEventData commonEventData = new CommonEventData(intent); commonEventData.setData("你好!鸿蒙"); CommonEventPublishInfo publishInfo = new CommonEventPublishInfo(); publishInfo.setOrdered(true); try { CommonEventManager.publishCommonEvent(commonEventData, publishInfo); } catch (RemoteException e) { e.printStackTrace(); } ``` * 订阅 多个订阅者设置不同优先级,优先级高的先执行响应事件 1. 构建MatchingSkills对象 2. 构建订阅信息对象 3. 构建订阅者对象,对事件发生后的响应程序 4. 核心动作接收事件 ```java // 1. 构建MatchingSkills对象 MatchingSkills matchingSkills = new MatchingSkills(); matchingSkills.addEvent("com.cxs.commoneventtest.event"); // 2. 构建订阅信息对象 CommonEventSubscribeInfo subscribeInfo1 = new CommonEventSubscribeInfo(matchingSkills); CommonEventSubscribeInfo subscribeInfo2 = new CommonEventSubscribeInfo(matchingSkills); // 指定优先级,缺省为0,优先级范围[-1000, 1000],值越大优先级越高。 subscribeInfo1.setPriority(0); subscribeInfo2.setPriority(1); // 3. 构建订阅者对象,对事件发生后的响应程序 CommonEventSubscriber commonEventSubscriber1 = new CommonEventSubscriber(subscribeInfo1) { @Override public void onReceiveEvent(CommonEventData commonEventData) { System.out.println("不耗时的任务开始111111........"); } }; CommonEventSubscriber commonEventSubscriber2 = new CommonEventSubscriber(subscribeInfo2) { @Override public void onReceiveEvent(CommonEventData commonEventData) { System.out.println("不耗时的任务开始22222........"); } }; // 4. 核心动作接收事件 try { CommonEventManager.subscribeCommonEvent(commonEventSubscriber1); CommonEventManager.subscribeCommonEvent(commonEventSubscriber2); } catch (RemoteException e) { e.printStackTrace(); } ``` 运行应用,点击订阅,再点击发布,输出如下 ![image](screenshots/phone/2.png) 我们可以看到订阅者对象commonEventSubscriber2先执行了,因为它的优先级更高 ![image](screenshots/phone/1.png)