• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()](../reference/apis/js-apis-application-staticSubscriberExtensionAbility.md#staticsubscriberextensionabilityonreceiveevent) 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.
6
7Subscribing to a common event in static mode is achieved by configuring a declaration file and implementing a class that inherits from [StaticSubscriberExtensionAbility](../reference/apis/js-apis-application-staticSubscriberExtensionAbility.md).
8
9> **NOTE**
10>
11> The static subscription mode has negative impact on system power consumption. Therefore, exercise caution when using this mode.
12
13## How to Develop
14
151. Declaring a static subscriber.
16
17   To declare a static subscriber, create an ExtensionAbility, which is derived from the **StaticSubscriberExtensionAbility** class, in the project.
18
19   You can implement service logic in the [**onReceiveEvent()**](../reference/apis/js-apis-application-staticSubscriberExtensionAbility.md#staticsubscriberextensionabilityonreceiveevent) callback.
20
21   ```ts
22   import StaticSubscriberExtensionAbility from '@ohos.application.StaticSubscriberExtensionAbility';
23   import commonEventManager from '@ohos.commonEventManager';
24
25   export default class StaticSubscriber extends StaticSubscriberExtensionAbility {
26     onReceiveEvent(event: commonEventManager.CommonEventData) {
27       console.info('onReceiveEvent, event: ' + event.event);
28     }
29   }
30   ```
31
322. Configure static subscriber settings.
33
34   After writing the static subscriber code, configure the subscriber in the [module.json5](../quick-start/module-configuration-file.md) file.
35
36   ```json
37   {
38     "module": {
39       "extensionAbilities": [
40         {
41           "name": "StaticSubscriber",
42           "srcEntry": "./ets/staticsubscriber/StaticSubscriber.ts",
43           "description": "$string:StaticSubscriber_desc",
44           "icon": "$media:icon",
45           "label": "$string:StaticSubscriber_label",
46           "type": "staticSubscriber",
47           "exported": true,
48           "metadata": [
49             {
50               "name": "ohos.extension.staticSubscriber",
51               "resource": "$profile:subscribe"
52             }
53           ]
54         }
55       ]
56     }
57   }
58   ```
59
60   Some fields in the file are described as follows:
61
62   - **srcEntry **: entry file path of the ExtensionAbility, that is, the file path of the static subscriber declared in Step 2.
63
64   - **type**: ExtensionAbility type. For a static subscriber, set this field to **staticSubscriber**.
65
66   - **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.
67        - **name**: name of the ExtensionAbility. For a static subscriber, declare the name as **ohos.extension.staticSubscriber** for successful identification.
68        - **resource**: path that stores the ExtensionAbility configuration, which is customizable. In this example, the path is **resources/base/profile/subscribe.json**.
69
70
713. Configure the level-2 configuration file to which the metadata points.
72
73   ```json
74   {
75     "commonEvents": [
76       {
77         "name": "xxx",
78         "permission": "xxx",
79         "events":[
80           "xxx"
81         ]
82       }
83     ]
84   }
85   ```
86
87   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:
88
89   - **name**: name of the ExtensionAbility, which must be the same as the name of **extensionAbility** declared in **module.json5**.
90   - **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.
91   - **events**: list of target events to subscribe to.
92
934. 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/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:
94
95   - **bundleName**: bundle name of the application.
96   - **app_signature**: fingerprint information of the application. For details, see [Configuration in install_list_capability.json](../../device-dev/subsystems/subsys-app-privilege-config-guide.md#configuration-in-install_list_capabilityjson).
97   - **allowCommonEvent**: type of common event that can be started by static broadcast.
98
99   ```json
100   [
101     ...
102     {
103       "bundleName": "com.example.myapplication", // Bundle name.
104       "app_signature": ["****"], // Fingerprint information.
105       "allowCommonEvent": ["usual.event.A", "usual.event.B"] // Type of common event that can be started by static broadcast.
106     }
107   ]
108   ```
109
110   > **NOTE**
111   >
112   > The **install_list_capability.json** file is available only for preinstalled applications.
113
114