HarmonyOS 基础技术赋能之公共事件(CommonEvent)开发

开发 前端 OpenHarmony
系统将收集到的事件信息,根据系统策略发送给订阅该事件的用户程序。 公共事件包括:终端设备用户可感知的亮灭屏事件,以及系统关键服务发布的系统事件(例如:USB插拔,网络连接,系统升级)等。

[[423584]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

引言

在HarmonyOS通过CES(Common Event Service,公共事件服务)为应用程序提供订阅、发布、退订公共事件的能力。

公共事件可分为系统公共事件和自定义公共事件。

系统公共事件:系统将收集到的事件信息,根据系统策略发送给订阅该事件的用户程序。 公共事件包括:终端设备用户可感知的亮灭屏事件,以及系统关键服务发布的系统事件(例如:USB插拔,网络连接,系统升级)等。

自定义公共事件:应用自定义一些公共事件用来处理业务逻辑。

场景介绍

每个应用都可以订阅自己感兴趣的公共事件,订阅成功后且公共事件发布后,系统会把其发送给应用。这些公共事件可能来自系统、其他应用和应用自身。HarmonyOS提供了一套完整的API,支持用户订阅、发布和接收公共事件。发布公共事件需要借助CommonEventData对象,接收公共事件需要继承CommonEventSubscriber类并实现onReceiveEvent回调函数。

开发者可以发布四种公共事件:无序的公共事件、带权限的公共事件、有序的公共事件、粘性的公共事件。

本文主讲无序的公共事件,其他类型事件,可参考华为官方开发文档学习。

指南

1.发布公共事件:

  1. try { 
  2.   Intent intent = new Intent(); 
  3.   Operation operation = new Intent.OperationBuilder() 
  4.       .withAction(“my.action”)//自定义字符串类型的action 
  5.       .build(); 
  6.   intent.setOperation(operation); 
  7.   intent.setParam("result","commonEventData"); 
  8.   intent.setParam("isCommonEvent",true); 
  9.   CommonEventData eventData = new CommonEventData(intent); 
  10.   CommonEventManager.publishCommonEvent(eventData); 
  11.   LogUtils.info(TAG,"PublishCommonEvent SUCCESS"); 
  12. } catch (RemoteException e) { 
  13.   LogUtils.error(TAG,"Exception occurred during publishCommonEvent invocation."); 

2. 订阅公共事件

1)创建CommonEventSubscriber派生类,在onReceiveEvent()回调函数中处理公共事件。

  1. private class MyCommonEventSubscriber extends CommonEventSubscriber { 
  2.     MyCommonEventSubscriber(CommonEventSubscribeInfo info) { 
  3.      super(info); 
  4.  } 
  5.  
  6.  @Override 
  7.  public void onReceiveEvent(CommonEventData commonEventData) { 

2)构造MyCommonEventSubscriber对象,调用CommonEventManager. subscribeCommonEvent()接口进行订阅。

  1. MatchingSkills matchingSkills = new MatchingSkills(); 
  2. //添加自定义的ation 
  3. matchingSkills.addEvent(ACTION);  
  4. matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_BOOT_COMPLETED); // 开机完成事件 
  5. matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_CHARGING); // 正在充电事件 
  6. CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills); 
  7. subscriber = new MyCommonEventSubscriber(subscribeInfo); 
  8. try { 
  9.   CommonEventManager.subscribeCommonEvent(subscriber); 
  10.   LogUtils.info(TAG,"SubscribeCommonEvent SUCCESS"); 
  11. } catch (RemoteException e) { 
  12.   LogUtils.error(TAG,"Exception occurred during subscribeCommonEvent invocation."); 

3)针对在onReceiveEvent中不能执行耗时操作的限制,可以使用CommonEventSubscriber的goAsyncCommonEvent()来实现异步操作,函数返回后仍保持该公共事件活跃,且执行完成后必须调用。

  1. // EventRunner创建新线程,将耗时的操作放到新的线程上执行 
  2.  private EventRunner eventRunner=EventRunner.create(); 
  3.  
  4. // MyEventHandler为EventHandler的派生类,在不同线程间分发和处理事件和Runnable任务 
  5.  private MyEventHandle myEventHandle=new MyEventHandle(eventRunner); 
  6.  
  7.  private class MyCommonEventSubscriber extends CommonEventSubscriber { 
  8.      MyCommonEventSubscriber(CommonEventSubscribeInfo info) { 
  9.       super(info); 
  10.   } 
  11.   @Override 
  12.   public void onReceiveEvent(CommonEventData commonEventData) { 
  13.         //以下为如果有耗时操作时,执行的代码 
  14.         final AsyncCommonEventResult result = goAsyncCommonEvent(); 
  15.         Runnable runnable=new Runnable() { 
  16.          @Override 
  17.          public void run() { 
  18.            // 待执行的操作,由开发者定义 
  19.            myEventHandle.sendEvent(100); 
  20.  
  21.            result.finishCommonEvent(); // 调用finish结束异步操作 
  22.          } 
  23.        }; 
  24.        myEventHandle.postTask(runnable); 
  25.   } 
  26.  
  27.  private class MyEventHandle extends EventHandler{ 
  28.  
  29.   public MyEventHandle(EventRunner runner) throws IllegalArgumentException { 
  30.     super(runner); 
  31.   } 
  32.  
  33.    @Override 
  34.    protected void processEvent(InnerEvent event) { 
  35.      super.processEvent(event); 
  36.      //处理事件,由开发者撰写 
  37.      int evnetID=event.eventId; 
  38.      LogUtils.info(TAG,"evnetID:"+evnetID); 
  39.  
  40.    } 
  41.  } 

3. 退订公共事件:

  1.  //在Ability的onStop()中调用CommonEventManager.unsubscribeCommonEvent()方法来退订公共事件。调用后,之前订阅的所有公共事件均被退订。 
  2.    @Override 
  3. protected void onStop() { 
  4.   super.onStop(); 
  5.   try { 
  6.     CommonEventManager.unsubscribeCommonEvent(subscriber); 
  7.     LogUtils.info(TAG, "unsubscribeCommonEvent success."); 
  8.   } catch (RemoteException e) { 
  9.     LogUtils.error(TAG, "Exception occurred during unsubscribeCommonEvent invocation."); 
  10.   } 

实现效果

1.启动APP时,如下图:

HarmonyOS 基础技术赋能之 公共事件(CommonEvent)开发-鸿蒙HarmonyOS技术社区

2. 先点击“订阅公共事件”,后点击“发布无序公共事件”。打印的log:

  1. 09-02 10:31:07.693 10390-10390/com.zel.commoneventdemo I 00000/LogUtil: MainAbilitySlice: SubscribeCommonEvent SUCCESS 
  2. 09-02 10:31:09.795 10390-10390/com.zel.commoneventdemo I 00000/LogUtil: MainAbilitySlice: PublishCommonEvent SUCCESS 
  3. 09-02 10:31:09.798 10390-10390/com.zel.commoneventdemo I 00000/LogUtil: MainAbilitySlice: action:action.send.message/result:commonEventData/isCommonEvent:true 
  4. 09-02 10:31:09.799 10390-12455/com.zel.commoneventdemo I 00000/LogUtil: MainAbilitySlice: evnetID:100    

附上源码

1.MainAbilitySlice

  1. public class MainAbilitySlice extends AbilitySlice implements ClickedListener { 
  2.   private String TAG="MainAbilitySlice"
  3.   private MyCommonEventSubscriber subscriber; 
  4.   private static final String ACTION="action.send.message"
  5.  
  6.   @Override 
  7.   public void onStart(Intent intent) { 
  8.     super.onStart(intent); 
  9.     super.setUIContent(ResourceTable.Layout_ability_main); 
  10.     Button btPublisher=(Button)findComponentById(ResourceTable.Id_btPublisher); 
  11.     Button btSubscriber=(Button)findComponentById(ResourceTable.Id_btSubscriber); 
  12.     btPublisher.setClickedListener(this); 
  13.     btSubscriber.setClickedListener(this); 
  14.   } 
  15.  
  16.   @Override 
  17.   public void onActive() { 
  18.     super.onActive(); 
  19.   } 
  20.  
  21.   @Override 
  22.   public void onForeground(Intent intent) { 
  23.     super.onForeground(intent); 
  24.   } 
  25.  
  26.   @Override 
  27.   public void onClick(Component component) { 
  28.     switch (component.getId()){ 
  29.       case ResourceTable.Id_btPublisher: 
  30.         try { 
  31.           Intent intent = new Intent(); 
  32.           Operation operation = new Intent.OperationBuilder() 
  33.               .withAction(ACTION
  34.               .build(); 
  35.           intent.setOperation(operation); 
  36.           intent.setParam("result","commonEventData"); 
  37.           intent.setParam("isCommonEvent",true); 
  38.           CommonEventData eventData = new CommonEventData(intent); 
  39.           CommonEventManager.publishCommonEvent(eventData); 
  40.           LogUtils.info(TAG,"PublishCommonEvent SUCCESS"); 
  41.         } catch (RemoteException e) { 
  42.           LogUtils.error(TAG,"Exception occurred during publishCommonEvent invocation."); 
  43.         } 
  44.         break; 
  45.       case ResourceTable.Id_btSubscriber: 
  46.         MatchingSkills matchingSkills = new MatchingSkills(); 
  47.         //添加自定义的ation 
  48.         matchingSkills.addEvent(ACTION);//自定义事件 
  49.         matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_BOOT_COMPLETED); // 开机完成事件 
  50.         matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_CHARGING); // 正在充电事件 
  51.         CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills); 
  52.         subscriber = new MyCommonEventSubscriber(subscribeInfo); 
  53.         try { 
  54.           CommonEventManager.subscribeCommonEvent(subscriber); 
  55.           LogUtils.info(TAG,"SubscribeCommonEvent SUCCESS"); 
  56.         } catch (RemoteException e) { 
  57.           LogUtils.error(TAG,"Exception occurred during subscribeCommonEvent invocation."); 
  58.         } 
  59.         break; 
  60.     } 
  61.  
  62.   } 
  63.    //// EventRunner创建新线程,将耗时的操作放到新的线程上执行 
  64.    private EventRunner eventRunner=EventRunner.create(); 
  65.  
  66.   // MyEventHandler为EventHandler的派生类,在不同线程间分发和处理事件和Runnable任务 
  67.    private MyEventHandle myEventHandle=new MyEventHandle(eventRunner); 
  68.  
  69.    private class MyCommonEventSubscriber extends CommonEventSubscriber { 
  70.        MyCommonEventSubscriber(CommonEventSubscribeInfo info) { 
  71.         super(info); 
  72.     } 
  73.  
  74.      /** 
  75.       * 针对在onReceiveEvent中不能执行耗时操作的限制,可以使用CommonEventSubscriber的goAsyncCommonEvent()来实现异步操作, 
  76.       * 函数返回后仍保持该公共事件活跃,且执行完成后必须调用AsyncCommonEventResult.finishCommonEvent()来结束。 
  77.       * @param commonEventData 
  78.       */ 
  79.     @Override 
  80.     public void onReceiveEvent(CommonEventData commonEventData) { 
  81.           //非执行耗时操作,以下代码即可 
  82.           Intent intent=commonEventData.getIntent(); 
  83.           String action= intent.getAction(); 
  84.           switch (action){ 
  85.             //自定义事件 
  86.             case ACTION
  87.               String result=intent.getStringParam("result"); 
  88.               boolean isCommonEventData=intent.getBooleanParam("isCommonEvent",false); 
  89.               LogUtils.info(TAG,"action:"+action+"/result:"+result+"/isCommonEvent:"+isCommonEventData); 
  90.               break; 
  91.             // 开机完成事件 
  92.             case CommonEventSupport.COMMON_EVENT_BOOT_COMPLETED: 
  93.               LogUtils.info(TAG,"action:"+action); 
  94.               break; 
  95.             // 正在充电事件 
  96.             case CommonEventSupport.COMMON_EVENT_CHARGING: 
  97.               LogUtils.info(TAG,"action:"+action); 
  98.               break; 
  99.           } 
  100.  
  101.  
  102.           //以下为如果有耗时操作时,选择执行的代码 
  103.           final AsyncCommonEventResult result = goAsyncCommonEvent(); 
  104.           Runnable runnable=new Runnable() { 
  105.            @Override 
  106.            public void run() { 
  107.              // 待执行的操作,由开发者定义 
  108.              myEventHandle.sendEvent(100); 
  109.  
  110.  
  111.              result.finishCommonEvent(); // 调用finish结束异步操作 
  112.            } 
  113.          }; 
  114.          myEventHandle.postTask(runnable); 
  115.     } 
  116.   } 
  117.  
  118.    private class MyEventHandle extends EventHandler{ 
  119.  
  120.     public MyEventHandle(EventRunner runner) throws IllegalArgumentException { 
  121.       super(runner); 
  122.     } 
  123.  
  124.      @Override 
  125.      protected void processEvent(InnerEvent event) { 
  126.        super.processEvent(event); 
  127.        //处理事件,由开发者撰写 
  128.        int evnetID=event.eventId; 
  129.        LogUtils.info(TAG,"evnetID:"+evnetID); 
  130.  
  131.      } 
  132.    } 
  133.  
  134.  
  135.   @Override 
  136.   protected void onStop() { 
  137.     super.onStop(); 
  138.     try { 
  139.       CommonEventManager.unsubscribeCommonEvent(subscriber); 
  140.       LogUtils.info(TAG, "unsubscribeCommonEvent success."); 
  141.     } catch (RemoteException e) { 
  142.       LogUtils.error(TAG, "Exception occurred during unsubscribeCommonEvent invocation."); 
  143.     } 
  144.   } 

2.LogUtils

  1. public class LogUtils { 
  2.     private static final String TAG_LOG = "LogUtil"
  3.  
  4.     private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, LogUtils.TAG_LOG); 
  5.  
  6.     private static final String LOG_FORMAT = "%{public}s: %{public}s"
  7.  
  8.     private LogUtils() { } 
  9.  
  10.     /** 
  11.      * Print debug log 
  12.      * 
  13.      * @param tag log tag 
  14.      * @param msg log message 
  15.      */ 
  16.     public static void debug(String tag, String msg) { 
  17.         HiLog.debug(LABEL_LOG, LOG_FORMAT, tag, msg); 
  18.     } 
  19.  
  20.     /** 
  21.      * Print info log 
  22.      * 
  23.      * @param tag log tag 
  24.      * @param msg log message 
  25.      */ 
  26.     public static void info(String tag, String msg) { 
  27.         HiLog.info(LABEL_LOG, LOG_FORMAT, tag, msg); 
  28.     } 
  29.  
  30.     /** 
  31.      * Print warn log 
  32.      * 
  33.      * @param tag log tag 
  34.      * @param msg log message 
  35.      */ 
  36.     public static void warn(String tag, String msg) { 
  37.         HiLog.warn(LABEL_LOG, LOG_FORMAT, tag, msg); 
  38.     } 
  39.  
  40.     /** 
  41.      * Print error log 
  42.      * 
  43.      * @param tag log tag 
  44.      * @param msg log message 
  45.      */ 
  46.     public static void error(String tag, String msg) { 
  47.         HiLog.error(LABEL_LOG, LOG_FORMAT, tag, msg); 
  48.     } 

3. xml 布局文件:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.   xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.   ohos:height="match_parent" 
  5.   ohos:orientation="vertical" 
  6.   ohos:width="match_parent"
  7.  
  8.  
  9.   <DirectionalLayout 
  10.     ohos:height="match_content" 
  11.     ohos:width="match_parent" 
  12.     ohos:left_margin="20vp" 
  13.     ohos:right_margin="20vp" 
  14.     ohos:top_margin="50vp" 
  15.     ohos:orientation="vertical"
  16.     <Button 
  17.       ohos:id="$+id:btPublisher" 
  18.       ohos:height="match_content" 
  19.       ohos:width="match_content" 
  20.       ohos:text_size="22vp" 
  21.       ohos:text_color="#ffffff" 
  22.       ohos:text="发布无序公共事件" 
  23.       ohos:padding="20vp" 
  24.       ohos:background_element="#00ffff"/> 
  25.  
  26.     <Button 
  27.       ohos:id="$+id:btSubscriber" 
  28.       ohos:height="match_content" 
  29.       ohos:width="match_content" 
  30.       ohos:text_size="22vp" 
  31.       ohos:text_color="#ffffff" 
  32.       ohos:text="订阅公共事件" 
  33.       ohos:padding="20vp" 
  34.       ohos:top_margin="30vp" 
  35.       ohos:background_element="#00ffff"/> 
  36.   </DirectionalLayout> 
  37.  
  38. </DirectionalLayout> 

 

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-09-23 10:00:57

鸿蒙HarmonyOS应用

2013-10-11 12:59:04

StrixMesh突发事件

2021-08-26 09:50:06

鸿蒙HarmonyOS应用

2021-08-31 14:58:52

鸿蒙HarmonyOS应用

2021-09-03 15:27:17

鸿蒙HarmonyOS应用

2021-08-27 09:57:18

鸿蒙HarmonyOS应用

2021-01-18 10:59:27

大数据公共事件大数据+

2021-12-03 09:49:59

鸿蒙HarmonyOS应用

2021-09-09 14:49:26

鸿蒙HarmonyOS应用

2022-04-27 18:06:12

数字安全数据安全网络安全

2009-07-02 09:56:24

导入事件驱动技术JSP Servlet

2017-08-13 18:17:46

网络文学赋能IP

2023-04-17 16:14:55

静态订阅鸿蒙

2011-08-08 07:46:39

政务

2018-09-18 11:47:17

2020-05-07 17:56:48

物联网人工智能技术

2020-05-07 21:49:53

物联网智慧AI
点赞
收藏

51CTO技术栈公众号