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 252. Project Configuration for a Static Subscriber 26 27 After writing the static subscriber code, configure the subscriber in the **module.json5** file. The configuration format is as follows: 28 29 ```ts 30 { 31 "module": { 32 ...... 33 "extensionAbilities": [ 34 { 35 "name": "StaticSubscriber", 36 "srcEntry": "./ets/StaticSubscriber/StaticSubscriber.ts", 37 "description": "$string:StaticSubscriber_desc", 38 "icon": "$media:icon", 39 "label": "$string:StaticSubscriber_label", 40 "type": "staticSubscriber", 41 "exported": true, 42 "metadata": [ 43 { 44 "name": "ohos.extension.staticSubscriber", 45 "resource": "$profile:subscribe" 46 } 47 ] 48 } 49 ] 50 ...... 51 } 52 } 53 ``` 54 55 Pay attention to the following fields in the JSON file: 56 57 - **srcEntry**: entry file path of the ExtensionAbility, that is, the file path of the static subscriber declared in Step 2. 58 59 - **type**: ExtensionAbility type. For a static subscriber, set this field to **staticSubscriber**. 60 61 - **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. 62 - **name**: name of the ExtensionAbility. For a static subscriber, declare the name as **ohos.extension.staticSubscriber** for successful identification. 63 - **resource**: path that stores the ExtensionAbility configuration, which is customizable. In this example, the path is **resources/base/profile/subscribe.json**. 64 65 A level-2 configuration file pointed to by **metadata** must be in the following format: 66 67 ```ts 68 { 69 "commonEvents": [ 70 { 71 "name": "xxx", 72 "permission": "xxx", 73 "events":[ 74 "xxx" 75 ] 76 } 77 ] 78 } 79 ``` 80 81 If the level-2 configuration file is not declared in this format, the file cannot be identified. The fields are described as follows: 82 83 - **name**: name of the ExtensionAbility, which must be the same as the name of **extensionAbility** declared in **module.json5**. 84 85 - **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. 86 87 - **events**: list of target events to subscribe to. 88 893. Device System Configuration 90 91 In the device system configuration file **/etc/static_subscriber_config.json**, add the bundle name of the static subscriber. 92 93 ```json 94 { 95 "xxx", 96 "ohos.extension.staticSubscriber", 97 "xxx" 98 } 99 ``` 100 101