1# Subscribing to Common Events in Static Mode (for System Applications Only) 2 3## When to Use 4 5A static subscriber is started once it receives a target event published by the system or application. At the same time, [onReceiveEvent()](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md#staticsubscriberextensionabilityonreceiveevent) is triggered, in which you can implement the service logic.<br> 6 7For example, if an application needs to execute some initialization tasks during device power-on, the application can subscribe to the power-on event in static mode. After receiving the power-on event, the application is started to execute the initialization tasks. 8 9Subscribing to a common event in static mode is achieved by configuring a declaration file and implementing a class that inherits from [StaticSubscriberExtensionAbility](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md). 10 11> **NOTE** 12> 13> Static subscription to common events affects system power consumption. Therefore, exercise caution when using this function. 14 15## Lifecycle 16 17The [StaticSubscriberExtensionAbility](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md) module is destroyed 15 seconds after [`onReceiveEvent()`](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md#staticsubscriberextensionabilityonreceiveevent) is executed. 18 19## How to Develop 20 211. Declare a static subscriber. 22 23 Create an ExtensionAbility inherited from [StaticSubscriberExtensionAbility](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md) in the project. 24 25 You can implement service logic using [onReceiveEvent()](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md#staticsubscriberextensionabilityonreceiveevent). 26 27 ```ts 28 import { commonEventManager, StaticSubscriberExtensionAbility } from '@kit.BasicServicesKit'; 29 import { hilog } from '@kit.PerformanceAnalysisKit'; 30 31 const TAG: string = 'StaticSubscriber'; 32 const DOMAIN_NUMBER: number = 0xFF00; 33 34 export default class StaticSubscriber extends StaticSubscriberExtensionAbility { 35 onReceiveEvent(event: commonEventManager.CommonEventData): void { 36 hilog.info(DOMAIN_NUMBER, TAG, 'onReceiveEvent, event: ' + event.event); 37 //... 38 } 39 } 40 ``` 41 422. Configure static subscriber settings. 43 44 After writing the static subscriber code, configure the subscriber in the [module.json5](../../quick-start/module-configuration-file.md) file. 45 46 ```json 47 { 48 "module": { 49 // ... 50 "extensionAbilities": [ 51 { 52 "name": "StaticSubscriber", 53 "srcEntry": "./ets/staticsubscriber/StaticSubscriber.ets", 54 "description": "$string:StaticSubscriber_desc", 55 "icon": "$media:app_icon", 56 "label": "$string:StaticSubscriber_label", 57 "type": "staticSubscriber", 58 "exported": false, 59 "metadata": [ 60 { 61 "name": "ohos.extension.staticSubscriber", 62 "resource": "$profile:subscribe" 63 } 64 ] 65 } 66 ], 67 // ... 68 } 69 } 70 ``` 71 72 Some fields in the file are described as follows: 73 74 - **srcEntry**: entry file path of the ExtensionAbility, that is, the file path of the static subscriber declared in Step 2. 75 76 - **type**: ExtensionAbility type. For a static subscriber, set this field to **staticSubscriber**. 77 78 - **metadata**: level-2 configuration file information of the ExtensionAbility. The configuration information varies according to the ExtensionAbility type. Therefore, you must use different config files to indicate the specific configuration. 79 - **name**: name of the ExtensionAbility. For a static subscriber, declare the name as **ohos.extension.staticSubscriber** for successful identification. 80 - **resource**: path that stores the ExtensionAbility configuration, which is customizable. In this example, the path is **resources/base/profile/subscribe.json**. 81 82 833. Configure the level-2 configuration file **subscribe.json** to which the metadata points. 84 85 > **NOTE** 86 > 87 > If the level-2 configuration file is not declared in this format, the file cannot be identified. 88 89 ```json 90 { 91 "commonEvents": [ 92 { 93 "name": "StaticSubscriber", 94 "permission": "", 95 "events": [ 96 "usual.event.AIRPLANE_MODE" 97 ], 98 "filter": [ 99 { 100 "event": "usual.event.AIRPLANE_MODE", 101 "conditions": { 102 "code": 0, 103 "data": "send common event data", 104 "parameters": { 105 "bundleType": 1, 106 "userId": 100 107 } 108 } 109 } 110 ] 111 } 112 ] 113 } 114 ``` 115 116 The **commonEvents** tag identifies a static subscription event configured by a subscriber. Lightweight devices do not support this tag and its subtags. The tag value is of an object array and contains four subtags: **name**, **permission**, **events**, and **filter**. 117 118 **Table 1** commonEvents 119 120 | **Name**| **Description** | **Data Type**| **Initial Value Allowed** | 121 | ------------ | ------------------------------------------------------------ | ------------ | -------------------------- | 122 | name | Name of the ExtensionAbility, which must be the same as the name of **extensionAbility** declared in **module.json5**.| String | No | 123 | permission | Permissions required by the publisher. | String | Yes (initial value: left empty)| 124 | events | List of subscribed target events. | String array | No | 125 | filter | Filter criteria for static events. This attribute is supported since API version 18.<br>For details about the values, see the following table.| Object array | Yes (initial value: left empty)| 126 127 The **filter** tag identifies the static subscription events that can be filtered by the subscriber as required. The tag value is of the object array type and contains two subtags: **event** and **conditions**. 128 129 **Table 2** filter 130 131 | **Name**| **Description** | **Data Type**| **Initial Value Allowed** | 132 | ------------ | ------------------------------------------------------------ | ------------ | ---------------- | 133 | event | Name of the event to be filtered out for static subscription events. The event name should match with the event list; otherwise, the configuration is invalid.| String | No| 134 | conditions | Filter criteria of an event, which is obtained from [CommonEventPublishData](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventPublishData.md#commoneventpublishdata).| Object | No| 135 136 The **conditions** tag identifies the condition configuration for filtering static subscription events. The tag value is of the object type and contains three subtags: **code**, **data**, and **parameters**. 137 138 **Table 3** conditions 139 140 | **Name**| **Description** | **Data Type** | **Initial Value Allowed** | 141 | ------------ | ------------------------------------------ | ------------------ | -------------------------- | 142 | code | Result code to filter. | Integer | Yes (initial value: left empty)| 143 | data | Custom result data to filter.| String | Yes (initial value: left empty)| 144 | parameters | Additional information to filter for a static subscription event. Only data of the Boolean, number, or string type can be configured.| Object| Yes (initial value: left empty)| 145 146 1474. Modify the [preset configuration file](https://gitee.com/openharmony/vendor_hihope/blob/master/rk3568/preinstall-config/install_list_capability.json) of the device, that is, the **/system/variant/phone/base/etc/app/install_list_capability.json** file. When the device is started, this file is read. During application installation, the common event type specified by **allowCommonEvent** in the file is authorized. The **install_list_capability.json** file contains the following fields: 148 149 - **bundleName**: bundle name of the application. 150 - **app_signature**: fingerprint information of the application. For details about how to configure fingerprint information, see [Application Privilege Configuration](../../../device-dev/subsystems/subsys-app-privilege-config-guide.md#configuration-in-install_list_capabilityjson), or obtain and enter the application ID using [Bundle Manager](../../tools/bm-tool.md). 151 - **allowCommonEvent**: type of common event that can be started by static broadcast. 152 153 ```json 154 [ 155 // ... 156 { 157 "bundleName": "com.samples.stageprocessthread", // Bundle name. 158 "app_signature": ["****"], // Fingerprint information. 159 "allowCommonEvent": ["usual.event.AIRPLANE_MODE"] // Type of common event that can be started by static broadcast. 160 } 161 ] 162 ``` 163 164 > **NOTE** 165 > 166 > The **install_list_capability.json** file is available only for preinstalled applications. 167<!--no_check-->