提交 a530f16d 编写于 作者: E ester.zhou

Update docs (16045)

Signed-off-by: Nester.zhou <ester.zhou@huawei.com>
上级 fc844515
......@@ -15,11 +15,11 @@ Common events are classified into system common events and custom common events.
Common events are also classified into unordered, ordered, and sticky common events.
- Unordered common events: CES forwards common events based on the subscription sequence, regardless of whether subscribers receive the events.
- Unordered common events: common events that CES forwards based on the subscription sequence, regardless of whether subscribers receive the events.
- Ordered common event: CES forwards common events to the next subscriber only after receiving a reply from the previous subscriber.
- Ordered common events: common events that CES forwards based on the subscriber priority. CES forwards common events to the subscriber with lower priority only after receiving a reply from the previous subscriber with higher priority.
- Sticky common event: a public event that can be sent to a subscriber before they initiate a subscription. Only system applications or system services can send sticky common event, and they must request the **ohos.permission.COMMONEVENT_STICKY** permission. For details about the configuration, see [Permission Application Guide](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file).
- Sticky common events: common events that can be sent to a subscriber before they initiate a subscription. Only system applications and system services can send sticky common events, and they must request the **ohos.permission.COMMONEVENT_STICKY** permission. For details about the configuration, see [Permission Application Guide](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file).
Each application can subscribe to common events as required. After your application subscribes to a common event, the system sends it to your application every time the event is published. Such an event may be published by the system, other applications, or your own application.
......
# Subscribing to Common Events in Static Mode (for System Applications Only)
## When to Use
A 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.
## How to Develop
1. Declaring a Static Subscriber
To declare a static subscriber, create an ExtensionAbility, which is derived from the **StaticSubscriberExtensionAbility** class, in the project. The sample code is as follows:
```ts
import StaticSubscriberExtensionAbility from '@ohos.application.StaticSubscriberExtensionAbility'
export default class StaticSubscriber extends StaticSubscriberExtensionAbility {
onReceiveEvent(event) {
console.log('onReceiveEvent, event:' + event.event);
}
}
```
You can implement service logic in the **onReceiveEvent** callback.
2. Project Configuration for a Static Subscriber
After writing the static subscriber code, configure the subscriber in the **module.json5** file. The configuration format is as follows:
```ts
{
"module": {
......
"extensionAbilities": [
{
"name": "StaticSubscriber",
"srcEntrance": "./ets/StaticSubscriber/StaticSubscriber.ts",
"description": "$string:StaticSubscriber_desc",
"icon": "$media:icon",
"label": "$string:StaticSubscriber_label",
"type": "staticSubscriber",
"visible": true,
"metadata": [
{
"name": "ohos.extension.staticSubscriber",
"resource": "$profile:subscribe"
}
]
}
]
......
}
}
```
Pay attention to the following fields in the JSON file:
- **srcEntrance**: entry file path of the ExtensionAbility, that is, the file path of the static subscriber declared in Step 2.
- **type**: ExtensionAbility type. For a static subscriber, set this field to **staticSubscriber**.
- **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.
- **name**: name of the ExtensionAbility. For a static subscriber, declare the name as **ohos.extension.staticSubscriber** for successful identification.
- **resource**: path that stores the ExtensionAbility configuration, which is customizable. In this example, the path is **resources/base/profile/subscribe.json**.
A level-2 configuration file pointed to by **metadata** must be in the following format:
```ts
{
"commonEvents": [
{
"name": "xxx",
"permission": "xxx",
"events":[
"xxx"
]
}
]
}
```
If the level-2 configuration file is not declared in this format, the file cannot be identified. The fields are described as follows:
- **name**: name of the ExtensionAbility, which must be the same as the name of **extensionAbility** declared in **module.json5**.
- **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.
- **events**: list of target events to subscribe to.
3. Device System Configuration
In the device system configuration file **/etc/static_subscriber_config.json**, add the bundle name of the static subscriber.
```json
{
"xxx",
"ohos.extension.staticSubscriber",
"xxx"
}
```
# Common Event Subscription Overview
​The common event service provides two subscription modes: dynamic and static. The biggest difference between these two modes is that dynamic subscription requires the application to be running, while static subscription does not.
- In dynamic subscription mode, a subscriber subscribes to common events by calling an API during the running period. For details, see [Subscribing to Common Events in Dynamic Mode](common-event-subscription.md).
- In static subscription mode, a subscriber subscribes to common events by configuring a declaration file and implementing a class that inherits from StaticSubscriberExtensionAbility. For details, see [Subscribing to Common Events in Static Mode](common-event-static-subscription.md).
# Subscribing to Common Events
# Subscribing to Common Events in Dynamic Mode
## When to Use
You can create a subscriber object to subscribe to a common event so as to obtain the parameters passed in the event. Certain system common events [require specific permissions](../security/accesstoken-guidelines.md) to subscribe to. For details, see [Required Permissions](../reference/apis/js-apis-commonEventManager.md#support).
In dynamic subscription mode, an application subscribes to a common event when it is running. If the subscribed event is published during the running period, the subscriber application will receive the event, together with the parameters passed in the event. For example, if an application expects to be notified of low battery so that it can reduce power consumption accordingly when running, then the application can subscribe to the low-battery event. Upon receiving the event, the application can close some unnecessary tasks to reduce power consumption. Certain system common events [require specific permissions](../security/accesstoken-guidelines.md) to subscribe to. For details, see [Required Permissions](../reference/apis/js-apis-commonEventManager.md#support).
## Available APIs
For details about the APIs, see [API Reference](../reference/apis/js-apis-commonEventManager.md#commoneventmanagersubscribe).
For details about the APIs, see [API Reference](../reference/apis/js-apis-commonEvent.md#commoneventcreatesubscriber).
| API| Description|
| -------- | -------- |
......@@ -19,10 +19,10 @@ For details about the APIs, see [API Reference](../reference/apis/js-apis-common
## How to Develop
1. Import the **commonEventManager** module.
1. Import the **commonEvent** module.
```ts
import commonEventManager from '@ohos.commonEventManager';
import commonEvent from '@ohos.commonEventManager';
```
2. Create a **subscribeInfo** object. For details about the data types and parameters of the object, see [CommonEventSubscribeInfo](../reference/apis/js-apis-commonEventManager.md#commoneventsubscribeinfo).
......@@ -40,7 +40,7 @@ For details about the APIs, see [API Reference](../reference/apis/js-apis-common
```ts
// Callback for subscriber creation.
commonEventManager.createSubscriber(subscribeInfo, (err, data) => {
commonEvent.createSubscriber(subscribeInfo, (err, data) => {
if (err) {
console.error(`[CommonEvent] CreateSubscriberCallBack err=${JSON.stringify(err)}`);
} else {
......@@ -56,7 +56,7 @@ For details about the APIs, see [API Reference](../reference/apis/js-apis-common
```ts
// Callback for common event subscription.
if (subscriber !== null) {
commonEventManager.subscribe(subscriber, (err, data) => {
commonEvent.subscribe(subscriber, (err, data) => {
if (err) {
console.error(`[CommonEvent] SubscribeCallBack err=${JSON.stringify(err)}`);
} else {
......
# Unsubscribing from Common Events
# Unsubscribing from Common Events in Dynamic Mode
## When to Use
You can call [unsubscribe()](../reference/apis/js-apis-commonEventManager.md#commoneventmanagerunsubscribe) to unsubscribe from a common event that is no longer required.
You can call [unsubscribe()](../reference/apis/js-apis-commonEventManager.md#commoneventmanagerunsubscribe) to unsubscribe from a common event that is no longer required in dynamic mode.
## Available APIs
......@@ -15,25 +15,25 @@ You can call [unsubscribe()](../reference/apis/js-apis-commonEventManager.md#com
## How to Develop
1. Import the **commonEventManager** module.
1. Import the **commonEvent** module.
```ts
import commonEventManager from '@ohos.commonEventManager';
import commonEvent from '@ohos.commonEventManager';
```
2. Subscribe to an event by following the procedure described in [Subscribing to Common Events](common-event-subscription.md).
2. Subscribe to an event by following the procedure described in [Subscribing to Common Events in Dynamic Mode](common-event-subscription.md).
3. Call **unsubscribe** in **CommonEvent** to unsubscribe from the common event.
```ts
// The subscriber object iscreated during event subscription.
// The subscriber object is created during event subscription.
if (subscriber !== null) {
commonEventManager.unsubscribe(subscriber, (err) => {
commonEvent.unsubscribe(subscriber, (err) => {
if (err) {
console.error(`[CommonEvent] UnsubscribeCallBack err=${JSON.stringify(err)}`);
console.error(`[CommonEvent] UnsubscribeCallBack err=${JSON.stringify(err)}`)
} else {
console.info(`[CommonEvent] Unsubscribe`);
subscriber = null;
console.info(`[CommonEvent] Unsubscribe`)
subscriber = null
}
})
}
......
......@@ -347,7 +347,7 @@ var subscribeInfo = {
// Callback for common event subscription.
function SubscribeCallBack(err, data) {
if (err.code) {
if (err) {
console.error("subscribe failed " + JSON.stringify(err));
} else {
console.info("subscribe ");
......@@ -472,7 +472,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for result code obtaining of an ordered common event.
function getCodeCallback(err, Code) {
if (err.code) {
if (err) {
console.error("getCode failed " + JSON.stringify(err));
} else {
console.info("getCode " + JSON.stringify(Code));
......@@ -529,7 +529,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for result code setting of an ordered common event.
function setCodeCallback(err) {
if (err.code) {
if (err) {
console.error("setCode failed " + JSON.stringify(err));
} else {
console.info("setCode");
......@@ -591,7 +591,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for result data obtaining of an ordered common event.
function getDataCallback(err, Data) {
if (err.code) {
if (err) {
console.error("getData failed " + JSON.stringify(err));
} else {
console.info("getData " + JSON.stringify(Data));
......@@ -648,7 +648,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for result data setting of an ordered common event
function setDataCallback(err) {
if (err.code) {
if (err) {
console.error("setData failed " + JSON.stringify(err));
} else {
console.info("setData");
......@@ -712,7 +712,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for result code and result data setting of an ordered common event.
function setCodeDataCallback(err) {
if (err.code) {
if (err) {
console.error("setCodeAndData failed " + JSON.stringify(err));
} else {
console.info("setCodeDataCallback");
......@@ -777,7 +777,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for checking whether the current common event is an ordered one.
function isOrderedCallback(err, isOrdered) {
if (err.code) {
if (err) {
console.error("isOrderedCommonEvent failed " + JSON.stringify(err));
} else {
console.info("isOrdered " + JSON.stringify(isOrdered));
......@@ -837,7 +837,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for checking whether the current common event is a sticky one.
function isStickyCallback(err, isSticky) {
if (err.code) {
if (err) {
console.error("isStickyCommonEvent failed " + JSON.stringify(err));
} else {
console.info("isSticky " + JSON.stringify(isSticky));
......@@ -895,7 +895,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for common event aborting.
function abortCallback(err) {
if (err.code) {
if (err) {
console.error("abortCommonEvent failed " + JSON.stringify(err));
} else {
console.info("abortCommonEvent");
......@@ -951,7 +951,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for clearing the aborted state of the current common event.
function clearAbortCallback(err) {
if (err.code) {
if (err) {
console.error("clearAbortCommonEvent failed " + JSON.stringify(err));
} else {
console.info("clearAbortCommonEvent");
......@@ -1007,7 +1007,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for checking whether the current common event is in the aborted state.
function getAbortCallback(err, AbortCommonEvent) {
if (err.code) {
if (err) {
console.error("getAbortCommonEvent failed " + JSON.stringify(err));
} else {
console.info("AbortCommonEvent " + AbortCommonEvent)
......@@ -1063,7 +1063,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for subscriber information obtaining.
function getSubscribeInfoCallback(err, SubscribeInfo) {
if (err.code) {
if (err) {
console.error("getSubscribeInfo failed " + JSON.stringify(err));
} else {
console.info("SubscribeInfo " + JSON.stringify(SubscribeInfo));
......@@ -1119,7 +1119,7 @@ var subscriber; // Subscriber object successfully created.
// Callback for ordered common event finishing.
function finishCommonEventCallback(err) {
if (err.code) {
if (err) {
console.error("finishCommonEvent failed " + JSON.stringify(err));
} else {
console.info("FinishCommonEvent");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册