当前位置:   article > 正文

【鸿蒙应用开发系列】- 事件订阅者CommonEventSubscriber使用_鸿蒙 广播接收器

鸿蒙 广播接收器

Android开发中,有一个Android四大组件之一的BroadcastReceiver, 也就是我们常说的广播接收器,可用于消息的订阅接收。

那在鸿蒙开发中,有没有提供相应的事件通知机制呢? 答案是有的,那就是我们本文要讲的CommonEventSubscriber,顾名思义,这是一个常用事件订阅者,可用于事件(消息)的订阅,相信很多Harmony开发者也见过或者使用过这个类,那这里作为代码记录,跟大家简单的讲解下具体的使用方式。

1、定义事件接收器

  1. public class MessageCommonEventSubscriber extends CommonEventSubscriber {
  2. public static final String ACTION_MESSAGE = "action_message";
  3. public static final String KEY_MESSAGE = "key_message";
  4. public MessageCommonEventSubscriber(CommonEventSubscribeInfo subscribeInfo) {
  5. super(subscribeInfo);
  6. }
  7. @Override
  8. public void onReceiveEvent(CommonEventData commonEventData) {
  9. if (commonEventData.getIntent() == null) {
  10. return;
  11. }
  12. Intent intent = commonEventData.getIntent();
  13. String action = intent.getAction();
  14. if (ACTION_MESSAGE.equals(action)) {
  15. if (intent.hasParameter(KEY_MESSAGE)) {
  16. String data = intent.getStringParam(KEY_MESSAGE);
  17. }
  18. }
  19. }
  20. }

我们定义一个MessageCommonEventSubscriber 类,继承自CommonEventSubscriber,然后实现onReceiveEvent方法,在方法中获取消息事件。

由于一个事件订阅者可以订阅多个action(多个事件),因此我们这里通过getAction获取到对应的事件意图,然后再获取传递的数据。

2、注册事件订阅者

  1. private void subscribeCommonEvent() {
  2. MatchingSkills matchingSkills = new MatchingSkills();
  3. matchingSkills.addEvent(MessageCommonEventSubscriber.ACTION_MESSAGE);
  4. matchingSkills.addEvent("actionxxx");
  5. CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
  6. CommonEventSubscriber subscriber = new MessageCommonEventSubscriber(subscribeInfo);
  7. try {
  8. CommonEventManager.subscribeCommonEvent(subscriber);
  9. } catch (RemoteException e) {
  10. LogUtil.error("subscribeCommonEvent", e.getMessage());
  11. }
  12. }

通过调用CommonEventManager管理器的subscribeCommonEvent方法进行事件订阅,这样就可以收到相应的消息事件了。

上面一个事件订阅者注册需要进行订阅信息的包装,App中可能存在多个订阅者需要注册订阅,场景多了,不免繁琐。下面提供一个注册的封装方法,仅供参考(代码记录哈哈)

  1. public <T extends CommonEventSubscriber> void subscribeMessage(Class<T> tClass, String action) {
  2. if (tClass == null) {
  3. return;
  4. }
  5. try {
  6. MatchingSkills matchingSkills = new MatchingSkills();
  7. matchingSkills.addEvent(action);
  8. CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
  9. CommonEventSubscriber subscriber = tClass
  10. .getDeclaredConstructor(CommonEventSubscribeInfo.class)
  11. .newInstance(subscribeInfo);
  12. CommonEventManager.subscribeCommonEvent(subscriber);
  13. } catch (RemoteException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
  14. LogUtil.error("subscribeMessage", e.getMessage());
  15. }
  16. }

这里采用反射获取事件订阅者的对象,实参只需要提供一个事件订阅者的类、一个action事件字符串,就可以完成事件订阅注册,这里如果有多个action,自行调整为数组实现即可。

3、解除注册事件订阅者

  1. ...
  2. CommonEventManager.unsubscribeCommonEvent(subscriber);

通过调用CommonEventManager管理器的unsubscribeCommonEvent方法进行解除订阅,解除订阅后,就不会接收到对应action的消息事件。

切记,在页面级别生命周期里,订阅事件跟解除订阅事件,需要配合使用,如果在页面中订阅了事件通知,在页面关闭的时候没有解除订阅,会造成内存泄露,可能会导致一些异常闪退问题。

题外话:这里的事件订阅者订阅是在代码中手动触发订阅,鸿蒙SDK只提供了这种订阅方式,在Android中我们叫动态注册,Android那种在AndroidManifest中添加配置进行静态注册的方式,鸿蒙暂不支持。

4、消息事件发送

  1. public void sendMessageEvent() {
  2. try {
  3. Intent intent = new Intent();
  4. Intent.OperationBuilder builder = new Intent.OperationBuilder();
  5. builder.withAction(MessageCommonEventSubscriber.ACTION_MESSAGE);// 设置事件的 action
  6. intent.setOperation(builder.build());
  7. //鸿蒙3.0系统后才支持传序列化对象
  8. // IntentParams intentParams = new IntentParams();
  9. // intentParams.setClassLoader(getContext().getClassloader());
  10. // intentParams.setParam(MessageCommonEventSubscriber.KEY_MESSAGE, message);
  11. // intent.setParams(intentParams);
  12. Gson gson = new Gson();
  13. intent.setParam(MessageCommonEventSubscriber.KEY_MESSAGE, gson.toJson(message));
  14. CommonEventData eventData = new CommonEventData(intent);
  15. CommonEventManager.publishCommonEvent(eventData);
  16. } catch (Exception e) {
  17. LogUtil.info(TAG, "publishCommonEvent occur exception.");
  18. }
  19. }

上面通过CommonEventManager.publishCommonEvent就完成了一个消息事件的发送。

注意:在鸿蒙3.0系统之前,CommonEventSubscriber不支持传递序列化对象数据,如果要传递一个对象,这里采用了json的形式,先将对象转成json数据,然后在接收数据的时候进行反序列处理。

到此,本文就完毕了,如果有什么疑问,欢迎评论区沟通探讨。谢谢阅读!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/257735
推荐阅读
相关标签
  

闽ICP备14008679号