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. 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> The static subscription mode has negative impact on system power consumption. Therefore, exercise caution when using this mode. 14 15## How to Develop 16 171. Declaring a static subscriber. 18 19 To declare a static subscriber, create an ExtensionAbility, which is derived from the **StaticSubscriberExtensionAbility** class, in the project. 20 21 You can implement service logic using [`onReceiveEvent()`](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md#staticsubscriberextensionabilityonreceiveevent). 22 23 ```ts 24 import { commonEventManager, StaticSubscriberExtensionAbility } from '@kit.BasicServicesKit'; 25 import { hilog } from '@kit.PerformanceAnalysisKit'; 26 27 const TAG: string = 'StaticSubscriber'; 28 const DOMAIN_NUMBER: number = 0xFF00; 29 30 export default class StaticSubscriber extends StaticSubscriberExtensionAbility { 31 onReceiveEvent(event: commonEventManager.CommonEventData): void { 32 hilog.info(DOMAIN_NUMBER, TAG, 'onReceiveEvent, event: ' + event.event); 33 //... 34 } 35 } 36 ``` 37 382. Configure static subscriber settings. 39 40 After writing the static subscriber code, configure the subscriber in the [module.json5](../../quick-start/module-configuration-file.md) file. 41 42 ```json 43 { 44 "module": { 45 // ... 46 "extensionAbilities": [ 47 { 48 "name": "StaticSubscriber", 49 "srcEntry": "./ets/staticsubscriber/StaticSubscriber.ets", 50 "description": "$string:StaticSubscriber_desc", 51 "icon": "$media:app_icon", 52 "label": "$string:StaticSubscriber_label", 53 "type": "staticSubscriber", 54 "exported": false, 55 "metadata": [ 56 { 57 "name": "ohos.extension.staticSubscriber", 58 "resource": "$profile:staticsubscriber" 59 } 60 ] 61 } 62 ], 63 // ... 64 } 65 } 66 ``` 67 68 Some fields in the file are described as follows: 69 70 - **srcEntry**: entry file path of the ExtensionAbility, that is, the file path of the static subscriber declared in Step 2. 71 72 - **type**: ExtensionAbility type. For a static subscriber, set this field to **staticSubscriber**. 73 74 - **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. 75 - **name**: name of the ExtensionAbility. For a static subscriber, declare the name as **ohos.extension.staticSubscriber** for successful identification. 76 - **resource**: path that stores the ExtensionAbility configuration, which is customizable. In this example, the path is **resources/base/profile/subscribe.json**. 77 78 793. Configure the level-2 configuration file to which the metadata points. 80 81 ```json 82 { 83 "commonEvents": [ 84 { 85 "name": "StaticSubscriber", 86 "permission": "", 87 "events": [ 88 "usual.event.AIRPLANE_MODE" 89 ] 90 } 91 ] 92 } 93 ``` 94 95 If the level-2 configuration file is not declared in this format, the file cannot be identified. Some fields in the file are described as follows: 96 97 - **name**: name of the ExtensionAbility, which must be the same as the name of **extensionAbility** declared in **module.json5**. 98 - **permission**: permission required for the publisher. If a publisher without the required permission attempts to publish an event, the event is regarded as invalid and will not be published. 99 - **events**: list of target events to subscribe to. 100 1014. 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 on the device. 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: 102 103 - **bundleName**: bundle name of the application. 104 - **app_signature**: fingerprint information of the application. For details about how to configure fingerprint information, see [Application Privilege Configuration](https://gitee.com/openharmony/docs/blob/master/en/device-dev/subsystems/subsys-app-privilege-config-guide.md#configuration-in-install_list_capabilityjson), or obtain and enter the application ID using [Bundle Manager](https://gitee.com/openharmony/docs/blob/master/en/application-dev/tools/bm-tool.md). 105 - **allowCommonEvent**: type of common event that can be started by static broadcast. 106 107 ```json 108 [ 109 // ... 110 { 111 "bundleName": "com.samples.stageprocessthread", // Bundle name. 112 "app_signature": ["****"], // Fingerprint information. 113 "allowCommonEvent": ["usual.event.AIRPLANE_MODE"] // Type of common event that can be started by static broadcast. 114 } 115 ] 116 ``` 117 118 > **NOTE** 119 > 120 > The **install_list_capability.json** file is available only for preinstalled applications. 121 122