HarmonyOS开发中四种公共事件代码演示
目录
- 说些废话
- 环境
- 无序的公共事件
-
- 发布者(Project)
-
- ability_main
- MainAbilitySlice
- 订阅者(Project)
-
- ability_main
- MyCommonEventSubscriber
- MainAbilitySlice
- 效果展示
- 带权限的公共事件
-
- 发布者(Project)
-
- ability_main
- MainAbilitySlice
- config.json
- 订阅者(Project)
-
- ability_main
- MyCommonEventSubscriber
- MainAbilitySlice
- config.json
- 效果展示
- 有序的公共事件
-
- 发布者(Project)
-
- ability_main
- MainAbilitySlice
- 订阅者(Project)
-
- ability_main
- MyCommonEventSubscriber
- MainAbilitySlice
- 效果展示
- 粘性的公共事件
-
- 发布者(Project)
-
- ability_main
- MainAbilitySlice
- config.json
- 订阅者(Project)
-
- ability_main
- MyCommonEventSubscriber
- MainAbilitySlice
- 效果展示
说些废话
遇事不决换设备,是自己的手机就重启,真的能解决99.99%的问题。比如说我今天下午用平板测试好好的,晚上就莫名其妙自己能拿到一个公共事件,我都还没手动publish啊……重启之后就好了= =
贴一下这个部分的官方文档,本来偷懒不想写的,但是遇到了点问题搜索真的是,十篇有九篇是一样的,还有八篇和官方文档一模一样……公共事件开发指导(基于java开发)
我觉得理论知识主要还是看官方文档,但是官方文档的术语确实有的不太好理解,我的注释有一部分也有些解释,但是我这个文章主要是贴代码展示为主的。
再有一个就是,我是把这四种公共事件写在两个Project里了(一个订阅者一个发布者),所以我这篇文章里的拆分代码都是在我四合一的基础上删掉其他代码的,有可能漏删了或者删多了,大家看到了可以留言告诉我,我会及时更正。
环境
HUAWEI MatePad Pro 10.8
DevEco Studio 3.0.0.993 Release
SDK 6(使用java)
我看的《公共事件开发指导》更新于2022-10-28 18:02,现在是2022-11-09 23:03
无序的公共事件
这个事件用微信公众号来举例,某公众号现在准备发布(推送)一篇推文,给订阅(关注)了此公众号的用户都进行了推送,没有顺序也没有权限限制,只有关注了该公众号的用户才能收到,且在发布完毕后关注的用户不会收到这个推送。
发布者(Project)
ability_main
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:text_publish" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="发布无序公共事件" ohos:text_size="40vp" /></DirectionalLayout>
MainAbilitySlice
package com.openvalley.cyj.commonandfrontservice.slice;import com.openvalley.cyj.commonandfrontservice.FrontServiceAbility;import com.openvalley.cyj.commonandfrontservice.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.aafwk.content.Operation;import ohos.agp.components.Text;import ohos.agp.utils.LayoutAlignment;import ohos.agp.window.dialog.ToastDialog;import ohos.event.commonevent.*;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;import ohos.rpc.RemoteException;public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel LABEL_MAIN = new HiLogLabel(3, 0x00001, MainAbilitySlice.class.getSimpleName()); Text text_publish = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); text_publish = (Text)findComponentById(ResourceTable.Id_text_publish); text_publish.setClickedListener(listen -> publish()); } //无序 private void publish(){ try { Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withAction("com.openvalley.test") .build(); intent.setOperation(operation); //发布的数据对象 CommonEventData commonEventData = new CommonEventData(intent); //发布的数据对象的说明附加(不用这个的话就是直接发布无序// CommonEventPublishInfo publishInfo = new CommonEventPublishInfo(); //发布无序的公共事件 CommonEventManager.publishCommonEvent(commonEventData); HiLog.info(LABEL_MAIN, "MainAbilitySlice::publish a unordered common event"); } catch (Exception e) { e.printStackTrace(); } } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}
订阅者(Project)
ability_main
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:text_subscriber" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="订阅公共事件" ohos:text_size="40vp" /> <Text ohos:id="$+id:text_unSubscriber" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="退订公共事件" ohos:text_size="40vp" /></DirectionalLayout>
MyCommonEventSubscriber
package com.openvalley.cyj.subscriber;import ohos.event.commonevent.CommonEventData;import ohos.event.commonevent.CommonEventSubscribeInfo;import ohos.event.commonevent.CommonEventSubscriber;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;/** * 接收公共事件需要继承CommonEventSubscriber的重写类 */public class MyCommonEventSubscriber extends CommonEventSubscriber { private static final HiLogLabel LABEL_SUBSCRIBER = new HiLogLabel(3, 0x00001, MyCommonEventSubscriber.class.getSimpleName()); public MyCommonEventSubscriber(CommonEventSubscribeInfo subscribeInfo) { super(subscribeInfo); } @Override public void onReceiveEvent(CommonEventData commonEventData) {HiLog.info(LABEL_SUBSCRIBER, "MyCommonEventSubscriber::receive a unordered common event"); }}
MainAbilitySlice
package com.openvalley.cyj.subscriber.slice;import com.openvalley.cyj.subscriber.MyCommonEventSubscriber;import com.openvalley.cyj.subscriber.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.Text;import ohos.event.commonevent.CommonEventManager;import ohos.event.commonevent.CommonEventSubscribeInfo;import ohos.event.commonevent.CommonEventSupport;import ohos.event.commonevent.MatchingSkills;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;import ohos.rpc.RemoteException;public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel LABEL_SUBSCRIBER_MAIN = new HiLogLabel(3, 0x00001, MainAbilitySlice.class.getSimpleName()); MyCommonEventSubscriber subscriber = null; Text text_subscriber = null; Text text_unSubscriber = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); text_subscriber = (Text)findComponentById(ResourceTable.Id_text_subscriber); text_subscriber.setClickedListener(listen -> subscriber()); text_unSubscriber = (Text)findComponentById(ResourceTable.Id_text_unSubscriber); text_unSubscriber.setClickedListener(listen -> unSubscriber()); } private void subscriber(){ MatchingSkills skills = new MatchingSkills(); skills.addEvent("com.openvalley.test");// skills.addEvent(CommonEventSupport.COMMON_EVENT_SCREEN_ON); // 亮屏事件 CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(skills); subscriber = new MyCommonEventSubscriber(subscribeInfo); try { CommonEventManager.subscribeCommonEvent(subscriber);HiLog.info(LABEL_SUBSCRIBER_MAIN, "MainAbilitySlice::subscriber unordered common event"); } catch (RemoteException e) { e.printStackTrace(); } } private void unSubscriber(){ try { CommonEventManager.unsubscribeCommonEvent(subscriber); HiLog.info(LABEL_SUBSCRIBER_MAIN, "MainAbilitySlice::unSubscriber all common event"); } catch (RemoteException e) { } } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}
效果展示
因为使用的是真机,所以两张图结合看叭(特意把时间摆出来了)
带权限的公共事件
这个事件用微信公众号来举例,某公众号现在准备发布(推送)一篇推文,设置了关注(订阅)的人中需要满足某一条件(权限)才能收到这篇推文。
发布者(Project)
ability_main
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:text_publish_power" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="发布自定义带权限公共事件" ohos:text_size="40vp" /></DirectionalLayout>
MainAbilitySlice
package com.openvalley.cyj.commonandfrontservice.slice;import com.openvalley.cyj.commonandfrontservice.FrontServiceAbility;import com.openvalley.cyj.commonandfrontservice.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.aafwk.content.Operation;import ohos.agp.components.Text;import ohos.agp.utils.LayoutAlignment;import ohos.agp.window.dialog.ToastDialog;import ohos.event.commonevent.*;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;import ohos.rpc.RemoteException;public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel LABEL_MAIN = new HiLogLabel(3, 0x00001, MainAbilitySlice.class.getSimpleName()); Text text_publish_power = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); text_publish_power = (Text)findComponentById(ResourceTable.Id_text_publish_power); text_publish_power.setClickedListener(listen -> publishPower()); } //发布自定义带权限的公共事件 private void publishPower() { try { Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withAction("com.openvalley.test") .build(); intent.setOperation(operation); //权限数组 String[] permissions = {"com.valley.subscriber.PERMISSION"}; //发布的数据对象 CommonEventData commonEventData = new CommonEventData(intent); //发布的数据对象的说明附加 CommonEventPublishInfo publishInfo = new CommonEventPublishInfo(); //表示是有权限的公共事件 publishInfo.setSubscriberPermissions(permissions); //发布自定义带权限的 CommonEventManager.publishCommonEvent(commonEventData, publishInfo); HiLog.info(LABEL_MAIN, "MainAbilitySlice::publish a power common event"); } catch (Exception e) { e.printStackTrace(); } } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}
config.json
配置有点多,就只截取这一部分了,是写在module中的。
"module": { "defPermissions": [ { "name": "com.valley.subscriber.PERMISSION" } ], "package": "com.openvalley.cyj.commonandfrontservice", "name": ".MyApplication", "mainAbility": "com.openvalley.cyj.commonandfrontservice.MainAbility", "deviceType": [ "phone", "tablet" ], ......}
订阅者(Project)
ability_main
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:text_subscriber" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="订阅公共事件" ohos:text_size="40vp" /> <Text ohos:id="$+id:text_unSubscriber" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="退订公共事件" ohos:text_size="40vp" /></DirectionalLayout>
MyCommonEventSubscriber
package com.openvalley.cyj.subscriber;import ohos.event.commonevent.CommonEventData;import ohos.event.commonevent.CommonEventSubscribeInfo;import ohos.event.commonevent.CommonEventSubscriber;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;/** * 接收公共事件需要继承CommonEventSubscriber的重写类 */public class MyCommonEventSubscriber extends CommonEventSubscriber { private static final HiLogLabel LABEL_SUBSCRIBER = new HiLogLabel(3, 0x00001, MyCommonEventSubscriber.class.getSimpleName()); public MyCommonEventSubscriber(CommonEventSubscribeInfo subscribeInfo) { super(subscribeInfo); } @Override public void onReceiveEvent(CommonEventData commonEventData) { HiLog.info(LABEL_SUBSCRIBER, "MyCommonEventSubscriber::receive a power common event"); }}
MainAbilitySlice
package com.openvalley.cyj.subscriber.slice;import com.openvalley.cyj.subscriber.MyCommonEventSubscriber;import com.openvalley.cyj.subscriber.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.Text;import ohos.event.commonevent.CommonEventManager;import ohos.event.commonevent.CommonEventSubscribeInfo;import ohos.event.commonevent.CommonEventSupport;import ohos.event.commonevent.MatchingSkills;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;import ohos.rpc.RemoteException;public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel LABEL_SUBSCRIBER_MAIN = new HiLogLabel(3, 0x00001, MainAbilitySlice.class.getSimpleName()); MyCommonEventSubscriber subscriber = null; Text text_subscriber = null; Text text_unSubscriber = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); text_subscriber = (Text)findComponentById(ResourceTable.Id_text_subscriber); text_subscriber.setClickedListener(listen -> subscriber()); text_unSubscriber = (Text)findComponentById(ResourceTable.Id_text_unSubscriber); text_unSubscriber.setClickedListener(listen -> unSubscriber()); } private void subscriber(){ MatchingSkills skills = new MatchingSkills(); skills.addEvent("com.openvalley.test"); CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(skills); //当发布的事件为自定义有权限的时 也要配这个优先级(至少我的这台真机要配这个才有效果) subscribeInfo.setPriority(100); //接收自定义有权限的事件时,在config.json的module中配置reqPermissions subscriber = new MyCommonEventSubscriber(subscribeInfo); try { CommonEventManager.subscribeCommonEvent(subscriber); HiLog.info(LABEL_SUBSCRIBER_MAIN, "MainAbilitySlice::subscriber power common event"); } catch (RemoteException e) { e.printStackTrace(); } } private void unSubscriber(){ try { CommonEventManager.unsubscribeCommonEvent(subscriber); HiLog.info(LABEL_SUBSCRIBER_MAIN, "MainAbilitySlice::unSubscriber all common event"); } catch (RemoteException e) { } } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}
config.json
"module": { "reqPermissions": [ { "name": "com.valley.subscriber.PERMISSION" } ], "package": "com.openvalley.cyj.subscriber", "name": ".MyApplication", "mainAbility": "com.openvalley.cyj.subscriber.MainAbility", "deviceType": [ "phone", "tablet" ], ......}
效果展示
有序的公共事件
这个事件用微信公众号来举例,某公众号现在准备发布(推送)一篇推文,给订阅(关注)了此公众号的用户都设置了优先级(接收顺序),也设置了哪个用户是最后一个收到这篇推文的。
发布者(Project)
ability_main
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:text_publish_order" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="发布有序公共事件" ohos:text_size="40vp" /></DirectionalLayout>
MainAbilitySlice
package com.openvalley.cyj.commonandfrontservice.slice;import com.openvalley.cyj.commonandfrontservice.FrontServiceAbility;import com.openvalley.cyj.commonandfrontservice.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.aafwk.content.Operation;import ohos.agp.components.Text;import ohos.agp.utils.LayoutAlignment;import ohos.agp.window.dialog.ToastDialog;import ohos.event.commonevent.*;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;import ohos.rpc.RemoteException;public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel LABEL_MAIN = new HiLogLabel(3, 0x00001, MainAbilitySlice.class.getSimpleName()); Text text_publish_order = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); text_publish_order = (Text)findComponentById(ResourceTable.Id_text_publish_order); text_publish_order.setClickedListener(listen -> publishOrder()); } //有序 private void publishOrder() { try { Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withAction("com.openvalley.test") .build(); intent.setOperation(operation); //发布的数据对象 CommonEventData commonEventData = new CommonEventData(intent); //发布的数据对象的说明附加 CommonEventPublishInfo publishInfo = new CommonEventPublishInfo(); //表示是有序的公共事件 publishInfo.setOrdered(true); //发布有序的 //匹配技巧 MatchingSkills skills = new MatchingSkills(); skills.addEvent("com.openvalley.test"); CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(skills); CommonEventManager.publishCommonEvent(commonEventData, publishInfo, new CommonEventSubscriber(subscribeInfo) { @Override public void onReceiveEvent(CommonEventData commonEventData) { HiLog.info(LABEL_MAIN, "MainAbilitySlice::receive a ordered common event at last"); } }); HiLog.info(LABEL_MAIN, "publish.MainAbilitySlice::publish a ordered common event"); } catch (Exception e) { e.printStackTrace(); } } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}
订阅者(Project)
ability_main
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:text_subscriber" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="订阅公共事件" ohos:text_size="40vp" /> <Text ohos:id="$+id:text_unSubscriber" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="退订公共事件" ohos:text_size="40vp" /></DirectionalLayout>
MyCommonEventSubscriber
package com.openvalley.cyj.subscriber;import ohos.event.commonevent.CommonEventData;import ohos.event.commonevent.CommonEventSubscribeInfo;import ohos.event.commonevent.CommonEventSubscriber;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;/** * 接收公共事件需要继承CommonEventSubscriber的重写类 */public class MyCommonEventSubscriber extends CommonEventSubscriber { private static final HiLogLabel LABEL_SUBSCRIBER = new HiLogLabel(3, 0x00001, MyCommonEventSubscriber.class.getSimpleName()); public MyCommonEventSubscriber(CommonEventSubscribeInfo subscribeInfo) { super(subscribeInfo); } @Override public void onReceiveEvent(CommonEventData commonEventData) { HiLog.info(LABEL_SUBSCRIBER, "MyCommonEventSubscriber::receive a ordered common event, priority is 100"); }}
MainAbilitySlice
package com.openvalley.cyj.subscriber.slice;import com.openvalley.cyj.subscriber.MyCommonEventSubscriber;import com.openvalley.cyj.subscriber.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.Text;import ohos.event.commonevent.CommonEventManager;import ohos.event.commonevent.CommonEventSubscribeInfo;import ohos.event.commonevent.CommonEventSupport;import ohos.event.commonevent.MatchingSkills;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;import ohos.rpc.RemoteException;public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel LABEL_SUBSCRIBER_MAIN = new HiLogLabel(3, 0x00001, MainAbilitySlice.class.getSimpleName()); MyCommonEventSubscriber subscriber = null; Text text_subscriber = null; Text text_unSubscriber = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); text_subscriber = (Text)findComponentById(ResourceTable.Id_text_subscriber); text_subscriber.setClickedListener(listen -> subscriber()); text_unSubscriber = (Text)findComponentById(ResourceTable.Id_text_unSubscriber); text_unSubscriber.setClickedListener(listen -> unSubscriber()); } private void subscriber(){ MatchingSkills skills = new MatchingSkills(); skills.addEvent("com.openvalley.test"); CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(skills); //当发布的事件为有序时,指定接收的优先级 subscribeInfo.setPriority(100); subscriber = new MyCommonEventSubscriber(subscribeInfo); try { CommonEventManager.subscribeCommonEvent(subscriber); HiLog.info(LABEL_SUBSCRIBER_MAIN, "subscriber.MainAbilitySlice::subscriber ordered common event"); } catch (RemoteException e) { e.printStackTrace(); } } private void unSubscriber(){ try { CommonEventManager.unsubscribeCommonEvent(subscriber); HiLog.info(LABEL_SUBSCRIBER_MAIN, "MainAbilitySlice::unSubscriber all common event"); } catch (RemoteException e) { } } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}
效果展示
粘性的公共事件
这个事件用微信公众号来举例,假设某公众号已经成立许久,也发布了许多推文,而此时你才去关注(订阅),你也能收到这些推文的推送。
发布者(Project)
ability_main
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:text_publish_sticky" ohos:height="match_content" ohos:width="match_content" ohos:layout_alignment="horizontal_center" ohos:text="发布粘性公共事件" ohos:text_size="40vp" /></DirectionalLayout>
MainAbilitySlice
package com.openvalley.cyj.commonandfrontservice.slice;import com.openvalley.cyj.commonandfrontservice.FrontServiceAbility;import com.openvalley.cyj.commonandfrontservice.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.aafwk.content.Operation;import ohos.agp.components.Text;import ohos.agp.utils.LayoutAlignment;import ohos.agp.window.dialog.ToastDialog;import ohos.event.commonevent.*;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;import ohos.rpc.RemoteException;public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel LABEL_MAIN = new HiLogLabel(3, 0x00001, MainAbilitySlice.class.getSimpleName()); Text text_publish_sticky = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); text_publish_sticky = (Text)findComponentById(ResourceTable.Id_text_publish_sticky); text_publish_sticky.setClickedListener(listen -> publishSticky()); } //发布粘性公共事件 private void publishSticky() { try { Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withAction("com.openvalley.test") .build(); intent.setOperation(operation); //发布的数据对象 CommonEventData commonEventData = new CommonEventData(intent); //发布的数据对象的说明附加 CommonEventPublishInfo publishInfo = new CommonEventPublishInfo(); //设置为粘性的 publishInfo.setSticky(true); //发布粘性的 CommonEventManager.publishCommonEvent(commonEventData, publishInfo); HiLog.info(LABEL_MAIN, "MainAbilitySlice::publish a sticky common event"); } catch (Exception e) { e.printStackTrace(); } } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}
config.json
"module": { "reqPermissions": [ { "name": "ohos.permission.COMMONEVENT_STICKY", "usedScene": { "ability": [".MainAbility"], "when": "inuse" } } ], "package": "com.openvalley.cyj.commonandfrontservice", "name": ".MyApplication", "mainAbility": "com.openvalley.cyj.commonandfrontservice.MainAbility", "deviceType": [ "phone", "tablet" ], ......}
订阅者(Project)
ability_main
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:text_subscriber" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="订阅公共事件" ohos:text_size="40vp" /> <Text ohos:id="$+id:text_unSubscriber" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="退订公共事件" ohos:text_size="40vp" /></DirectionalLayout>
MyCommonEventSubscriber
package com.openvalley.cyj.subscriber;import ohos.event.commonevent.CommonEventData;import ohos.event.commonevent.CommonEventSubscribeInfo;import ohos.event.commonevent.CommonEventSubscriber;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;/** * 接收公共事件需要继承CommonEventSubscriber的重写类 */public class MyCommonEventSubscriber extends CommonEventSubscriber { private static final HiLogLabel LABEL_SUBSCRIBER = new HiLogLabel(3, 0x00001, MyCommonEventSubscriber.class.getSimpleName()); public MyCommonEventSubscriber(CommonEventSubscribeInfo subscribeInfo) { super(subscribeInfo); } @Override public void onReceiveEvent(CommonEventData commonEventData) { HiLog.info(LABEL_SUBSCRIBER, "MyCommonEventSubscriber::receive a sticky common event"); }}
MainAbilitySlice
package com.openvalley.cyj.subscriber.slice;import com.openvalley.cyj.subscriber.MyCommonEventSubscriber;import com.openvalley.cyj.subscriber.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.Text;import ohos.event.commonevent.CommonEventManager;import ohos.event.commonevent.CommonEventSubscribeInfo;import ohos.event.commonevent.CommonEventSupport;import ohos.event.commonevent.MatchingSkills;import ohos.hiviewdfx.HiLog;import ohos.hiviewdfx.HiLogLabel;import ohos.rpc.RemoteException;public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel LABEL_SUBSCRIBER_MAIN = new HiLogLabel(3, 0x00001, MainAbilitySlice.class.getSimpleName()); MyCommonEventSubscriber subscriber = null; Text text_subscriber = null; Text text_unSubscriber = null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); text_subscriber = (Text)findComponentById(ResourceTable.Id_text_subscriber); text_subscriber.setClickedListener(listen -> subscriber()); text_unSubscriber = (Text)findComponentById(ResourceTable.Id_text_unSubscriber); text_unSubscriber.setClickedListener(listen -> unSubscriber()); } private void subscriber(){ MatchingSkills skills = new MatchingSkills(); skills.addEvent("com.openvalley.test");// skills.addEvent(CommonEventSupport.COMMON_EVENT_SCREEN_ON); // 亮屏事件 CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(skills); subscriber = new MyCommonEventSubscriber(subscribeInfo); try { CommonEventManager.subscribeCommonEvent(subscriber); HiLog.info(LABEL_SUBSCRIBER_MAIN, "MainAbilitySlice::subscriber sticky common event"); } catch (RemoteException e) { e.printStackTrace(); } } private void unSubscriber(){ try { CommonEventManager.unsubscribeCommonEvent(subscriber); HiLog.info(LABEL_SUBSCRIBER_MAIN, "MainAbilitySlice::unSubscriber all common event"); } catch (RemoteException e) { } } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}
效果展示