1 Star 0 Fork 5.1K

youguilin / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
hiappevent.md 34.74 KB
一键复制 编辑 原始数据 按行查看 历史
mamingshuai 提交于 2021-06-02 01:00 . update OpenHarmony 2.0 Canary

HiAppEvent

Modules to Import

import hiappevent from '@ohos.hiappevent';

Required Permissions

None.

Functions

write(string, EventType, any..., AsyncCallback<number>)

Writes event information to the event file of the current day. This function supports variable parameters and uses an asynchronous callback to return the result.

Parameters

Name

Type

Mandatory

Description

eventName

string

Yes

Indicates the app event name.

eventType

EventType

Yes

Indicates the app event type.

params

any...

No

Indicates an array of parameters of the app event. Enter the name and value of each parameter in sequence. A parameter name must be a string, and a parameter value must be a string, number, boolean, or Array (which can only be a string, number, or boolean).

callback

AsyncCallback<number>

No

Indicates the callback function, which can be used to process the received return value.

  • Value 0 indicates that the event parameter verification is successful, and the event will be written to the event file asynchronously.
  • A value greater than 0 indicates that invalid parameters are present in the event, and the event will be written to the event file asynchronously after the invalid parameters are ignored.
  • A value less than 0 indicates that the event parameter verification fails, and the event will not be written to the event file asynchronously.

Example

hiappevent.write("testEvent", hiappevent.EventType.FAULT, "intData", 100, "strData", "strValue", (err, value) => {
    if (err) {
        // Event writing failed: The event contains invalid parameters or the event parameter verification fails.
        console.error(`failed to write event because ${err}`);
        return;
    }

    // Event writing succeeded.
    console.log(`success to write event: ${value}`);
});

write(string, EventType, any...)

Writes event information to the event file of the current day. This function supports variable parameters and uses an asynchronous promise to return the result.

Parameters

Name

Type

Mandatory

Description

eventName

string

Yes

Indicates the app event name.

eventType

EventType

Yes

Indicates the app event type.

params

any...

No

Indicates an array of parameters of the app event. Enter the name and value of each parameter in sequence. A parameter name must be a string, and a parameter value must be a string, number, boolean, or Array (which can only be a string, number, or boolean).

Return Values

Type

Description

Promise<void>

Indicates the promise used to return the execution result.

Example

hiappevent.write("testEvent", hiappevent.EventType.FAULT, "intData", 100, "strData", "strValue")
    .then((value) => {
        // Event writing succeeded.
        console.log(`success to write event: ${value}`);
    }).catch((err) => {
        // Event writing failed: The event contains invalid parameters or the event parameter verification fails.
        console.error(`failed to write event because ${err}`);
    });

writeJson(string, EventType, object, AsyncCallback<number>)

Writes event information to the event file of the current day. This function supports JSON parameters and uses an asynchronous callback to return the result.

Parameters

Name

Type

Mandatory

Description

eventName

string

Yes

Indicates the app event name.

eventType

EventType

Yes

Indicates the app event type.

jsonParam

object

Yes

Indicates an array of JSON parameters of the app event. A key must be a string, and a value must be a string, number, boolean, or Array (which can only be a string, number, or boolean).

callback

AsyncCallback<number>

No

Indicates the callback function, which can be used to process the received return value.

  • Value 0 indicates that the event parameter verification is successful, and the event will be written to the event file asynchronously.
  • A value greater than 0 indicates that invalid parameters are present in the event, and the event will be written to the event file asynchronously after the invalid parameters are ignored.
  • A value less than 0 indicates that the event parameter verification fails, and the event will not be written to the event file asynchronously.

Example

hiappevent.writeJson("testEvent", hiappevent.EventType.FAULT, {"intData":100, "strData":"strValue"}, (err, value) => {
    if (err) {
        // Event writing failed: The event contains invalid parameters or the event parameter verification fails.
        console.error(`failed to write event because ${err}`);
        return;
    }

    // Event writing succeeded.
    console.log(`success to write event: ${value}`);
});

writeJson(string, EventType, object)

Writes event information to the event file of the current day. This function supports JSON parameters and uses an asynchronous promise to return the result.

Parameters

Name

Type

Mandatory

Description

eventName

string

Yes

Indicates the app event name.

eventType

EventType

Yes

Indicates the app event type.

jsonParam

object

Yes

Indicates an array of JSON parameters of the app event. A key must be a string, and a value must be a string, number, boolean, or Array (which can only be a string, number, or boolean).

Return Values

Type

Description

Promise<void>

Indicates the promise used to return the execution result.

Example

hiappevent.writeJson("testEvent", hiappevent.EventType.FAULT, {"intData":100, "strData":"strValue"})
    .then((value) => {
        // Event writing succeeded.
        console.log(`success to write event: ${value}`);
    }).catch((err) => {
        // Event writing failed: The event contains invalid parameters or the event parameter verification fails.
        console.error(`failed to write event because ${err}`);
    });

Enums

EventType

Enumerates event types.

Name

Default Value

Description

FAULT

1

Fault event

STATISTIC

2

Statistical event

SECURITY

3

Security event

BEHAVIOR

4

System behavior event

1
https://gitee.com/yougl/docs.git
git@gitee.com:yougl/docs.git
yougl
docs
docs
master

搜索帮助