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, the **onReceiveEvent** callback is triggered, in which you can implement the service logic. For 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. Subscribing to a common event in static mode is achieved by configuring a declaration file and implementing a class that inherits from **StaticSubscriberExtensionAbility**. Note that this subscribing mode has negative impact on system power consumption. Therefore, exercise caution when using this mode. 6 7## How to Develop 8 91. Declaring a Static Subscriber 10 11 To declare a static subscriber, create an ExtensionAbility, which is derived from the **StaticSubscriberExtensionAbility** class, in the project. The sample code is as follows: 12 13 ```ts 14 import StaticSubscriberExtensionAbility from '@ohos.application.StaticSubscriberExtensionAbility' 15 16 export default class StaticSubscriber extends StaticSubscriberExtensionAbility { 17 onReceiveEvent(event) { 18 console.log('onReceiveEvent, event:' + event.event); 19 } 20 } 21 ``` 22 23 You can implement service logic in the **onReceiveEvent** callback. 24 25 26 272. Project Configuration for a Static Subscriber 28 29 After writing the static subscriber code, configure the subscriber in the **module.json5** file. The configuration format is as follows: 30 31 ```ts 32 { 33 "module": { 34 ...... 35 "extensionAbilities": [ 36 { 37 "name": "StaticSubscriber", 38 "srcEntrance": "./ets/StaticSubscriber/StaticSubscriber.ts", 39 "description": "$string:StaticSubscriber_desc", 40 "icon": "$media:icon", 41 "label": "$string:StaticSubscriber_label", 42 "type": "staticSubscriber", 43 "visible": true, 44 "metadata": [ 45 { 46 "name": "ohos.extension.staticSubscriber", 47 "resource": "$profile:subscribe" 48 } 49 ] 50 } 51 ] 52 ...... 53 } 54 } 55 ``` 56 57 Pay attention to the following fields in the JSON file: 58 59 - **srcEntrance**: entry file path of the ExtensionAbility, that is, the file path of the static subscriber declared in Step 2. 60 61 - **type**: ExtensionAbility type. For a static subscriber, set this field to **staticSubscriber**. 62 63 - **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. 64 - **name**: name of the ExtensionAbility. For a static subscriber, declare the name as **ohos.extension.staticSubscriber** for successful identification. 65 - **resource**: path that stores the ExtensionAbility configuration, which is customizable. In this example, the path is **resources/base/profile/subscribe.json**. 66 67 A level-2 configuration file pointed to by **metadata** must be in the following format: 68 69 ```ts 70 { 71 "commonEvents": [ 72 { 73 "name": "xxx", 74 "permission": "xxx", 75 "events":[ 76 "xxx" 77 ] 78 } 79 ] 80 } 81 ``` 82 83 If the level-2 configuration file is not declared in this format, the file cannot be identified. The fields are described as follows: 84 85 - **name**: name of the ExtensionAbility, which must be the same as the name of **extensionAbility** declared in **module.json5**. 86 87 - **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. 88 89 - **events**: list of target events to subscribe to. 90 913. Device System Configuration 92 93 In the device system configuration file **/etc/static_subscriber_config.json**, add the bundle name of the static subscriber. 94 95 ```json 96 { 97 "xxx", 98 "ohos.extension.staticSubscriber", 99 "xxx" 100 } 101 ``` 102 103