提交 122e3050 编写于 作者: S shawn_he

update docs

Signed-off-by: Nshawn_he <shawn.he@huawei.com>
上级 3e9c571a
# Performance Tracing<a name="EN-US_TOPIC_0000001165886716"></a>
# Performance Tracing
>![](../../public_sys-resources/icon-note.gif) **NOTE:**
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> - The APIs of this module are no longer maintained since API version 8. It is recommended that you use the APIs of [hiTraceMeter](js-apis-hitracemeter.md) instead.
> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import<a name="s56d19203690d4782bfc74069abb6bd71"></a>
## Modules to Import
```
import bytrace from '@ohos.bytrace';
```
## Required Permissions<a name="section11257113618419"></a>
None
## bytrace.startTrace<a name="section785191510485"></a>
startTrace\(name: string, taskId: number, expectedTime?: number\): void
Starts a trace of a task. **expectedTime** is an optional parameter, which specifies the expected duration of the trace.
- Parameters
<a name="table69661135912"></a>
<table><thead align="left"><tr id="row149668318915"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p7966738914"><a name="p7966738914"></a><a name="p7966738914"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="13.22%" id="mcps1.1.5.1.2"><p id="p296713699"><a name="p296713699"></a><a name="p296713699"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="10.67%" id="mcps1.1.5.1.3"><p id="p196718315911"><a name="p196718315911"></a><a name="p196718315911"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p9967231197"><a name="p9967231197"></a><a name="p9967231197"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row99671533914"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p79671633910"><a name="p79671633910"></a><a name="p79671633910"></a>name</p>
</td>
<td class="cellrowborder" valign="top" width="13.22%" headers="mcps1.1.5.1.2 "><p id="p11967433914"><a name="p11967433914"></a><a name="p11967433914"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="10.67%" headers="mcps1.1.5.1.3 "><p id="p19671336916"><a name="p19671336916"></a><a name="p19671336916"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p2084833211463"><a name="p2084833211463"></a><a name="p2084833211463"></a>Name of the trace task to start.</p>
</td>
</tr>
<tr id="row18967831393"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p16813106387"><a name="p16813106387"></a><a name="p16813106387"></a>taskId</p>
</td>
<td class="cellrowborder" valign="top" width="13.22%" headers="mcps1.1.5.1.2 "><p id="p976115416386"><a name="p976115416386"></a><a name="p976115416386"></a>number</p>
</td>
<td class="cellrowborder" valign="top" width="10.67%" headers="mcps1.1.5.1.3 "><p id="p149671932919"><a name="p149671932919"></a><a name="p149671932919"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p19675312911"><a name="p19675312911"></a><a name="p19675312911"></a>Task ID.</p>
</td>
</tr>
<tr id="row1225911163813"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p132250110380"><a name="p132250110380"></a><a name="p132250110380"></a>expectedTime</p>
</td>
<td class="cellrowborder" valign="top" width="13.22%" headers="mcps1.1.5.1.2 "><p id="p1622551113381"><a name="p1622551113381"></a><a name="p1622551113381"></a>number</p>
</td>
<td class="cellrowborder" valign="top" width="10.67%" headers="mcps1.1.5.1.3 "><p id="p5226151110383"><a name="p5226151110383"></a><a name="p5226151110383"></a>No</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p22262111381"><a name="p22262111381"></a><a name="p22262111381"></a>Expected duration of the trace, in ms.</p>
</td>
</tr>
</tbody>
</table>
>![](../../public_sys-resources/icon-note.gif) **NOTE:**
>If multiple trace tasks with the same name need to be performed at the same time or a trace task needs to be performed multiple times concurrently, different **taskId**s must be specified in **startTrace**. If the trace tasks with the same name are not performed at the same time, the same **taskId** can be used. For details, see the **bytrace.finishTrace** example.
- Example
```
bytrace.startTrace("myTestFunc", 1);
bytrace.startTrace("myTestFunc", 1, 5); // The expected duration of the trace is 5 ms.
```
## bytrace.finishTrace<a name="section7895134841213"></a>
finishTrace\(name: string, taskId: number\): void
## System Capabilities
SystemCapability.Developtools.Bytrace
## bytrace.startTrace
startTrace(name: string, taskId: number, expectedTime?: number): void
Starts a trace task. **expectedTime** is an optional parameter, which specifies the expected duration of the trace.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the trace task to start.|
| taskId | number | Yes| Task ID.|
| expectedTime | number | No| Expected duration of the trace, in ms.|
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> If multiple trace tasks with the same name need to be performed at the same time or a trace task needs to be performed multiple times concurrently, different task IDs must be specified in **startTrace**. If the trace tasks with the same name are not performed at the same time, the same taskId can be used. For details, see the bytrace.finishTrace example.
**Example**
```
bytrace.startTrace("myTestFunc", 1);
bytrace.startTrace("myTestFunc", 1, 5); // The expected duration of the trace is 5 ms.
```
## bytrace.finishTrace
finishTrace(name: string, taskId: number): void
Stops a trace task.
- Parameters
<a name="table1589613488127"></a>
<table><thead align="left"><tr id="row208961448131215"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p1089674831216"><a name="p1089674831216"></a><a name="p1089674831216"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="13.22%" id="mcps1.1.5.1.2"><p id="p1689613481128"><a name="p1689613481128"></a><a name="p1689613481128"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="10.67%" id="mcps1.1.5.1.3"><p id="p989613483125"><a name="p989613483125"></a><a name="p989613483125"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p13896148111215"><a name="p13896148111215"></a><a name="p13896148111215"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row14896204817125"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p10896174817128"><a name="p10896174817128"></a><a name="p10896174817128"></a>name</p>
</td>
<td class="cellrowborder" valign="top" width="13.22%" headers="mcps1.1.5.1.2 "><p id="p189618483125"><a name="p189618483125"></a><a name="p189618483125"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="10.67%" headers="mcps1.1.5.1.3 "><p id="p118961548101215"><a name="p118961548101215"></a><a name="p118961548101215"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1989674810124"><a name="p1989674810124"></a><a name="p1989674810124"></a>Name of the trace task to stop.</p>
</td>
</tr>
<tr id="row5896548101211"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p10896164811121"><a name="p10896164811121"></a><a name="p10896164811121"></a>taskId</p>
</td>
<td class="cellrowborder" valign="top" width="13.22%" headers="mcps1.1.5.1.2 "><p id="p1789694821215"><a name="p1789694821215"></a><a name="p1789694821215"></a>number</p>
</td>
<td class="cellrowborder" valign="top" width="10.67%" headers="mcps1.1.5.1.3 "><p id="p08962488128"><a name="p08962488128"></a><a name="p08962488128"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p8896194831214"><a name="p8896194831214"></a><a name="p8896194831214"></a>Task ID.</p>
</td>
</tr>
</tbody>
</table>
>![](../../public_sys-resources/icon-note.gif) **NOTE:**
>To stop a trace task, the values of **name** and **taskId** in **finishTrace** must be the same as those in **startTrace**.
- Example
```
bytrace.finishTrace("myTestFunc", 1);
```
```
// Start track tasks with the same name concurrently.
bytrace.startTrace("myTestFunc", 1);
...... // Service flow
bytrace.startTrace ("myTestFunc", 2); // The second trace task starts while the first task is still running. The first and second tasks have the same name but different task IDs.
...... // Service flow
bytrace.finishTrace("myTestFunc", 1);
...... // Service flow
bytrace.finishTrace("myTestFunc", 2);
```
```
// Start track tasks with the same name at different times.
bytrace.startTrace("myTestFunc", 1);
...... // Service flow
bytrace.finishTrace("myTestFunc", 1); // The first trace task ends.
...... // Service flow
bytrace.startTrace("myTestFunc", 1); // The second trace task starts after the first task ends. The two tasks have the same name and task ID.
...... // Service flow
bytrace.finishTrace("myTestFunc", 1);
```
## bytrace.traceByValue<a name="section1388414179173"></a>
traceByValue\(name: string, value: number\): void
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the trace task to start.|
| taskId | number | Yes| Task ID.|
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> To stop a trace task, the values of name and task ID in **finishTrace** must be the same as those in **startTrace**.
**Example**
```
bytrace.finishTrace("myTestFunc", 1);
```
```
// Start track tasks with the same name concurrently.
bytrace.startTrace("myTestFunc", 1);
// Service flow
bytrace.startTrace("myTestFunc", 2); // The second trace task starts while the first task is still running. The first and second tasks have the same name but different task IDs.
// Service flow
bytrace.finishTrace("myTestFunc", 1);
// Service flow
bytrace.finishTrace("myTestFunc", 2);
```
```
// Start track tasks with the same name at different times.
bytrace.startTrace("myTestFunc", 1);
// Service flow
bytrace.finishTrace("myTestFunc", 1); // The first trace task ends.
// Service flow
bytrace.startTrace("myTestFunc", 1); // The second trace task starts after the first task ends. The two tasks have the same name and task ID.
// Service flow
bytrace.finishTrace("myTestFunc", 1);
```
## bytrace.traceByValue
traceByValue(name: string, value: number): void
Traces the value changes of a variable.
- Parameters
<a name="table0884617171718"></a>
<table><thead align="left"><tr id="row1188481771714"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p488419174178"><a name="p488419174178"></a><a name="p488419174178"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="13.22%" id="mcps1.1.5.1.2"><p id="p12884131721718"><a name="p12884131721718"></a><a name="p12884131721718"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="10.67%" id="mcps1.1.5.1.3"><p id="p188481714178"><a name="p188481714178"></a><a name="p188481714178"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p188411174177"><a name="p188411174177"></a><a name="p188411174177"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row38849172174"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p7884121719172"><a name="p7884121719172"></a><a name="p7884121719172"></a>name</p>
</td>
<td class="cellrowborder" valign="top" width="13.22%" headers="mcps1.1.5.1.2 "><p id="p1488415174171"><a name="p1488415174171"></a><a name="p1488415174171"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="10.67%" headers="mcps1.1.5.1.3 "><p id="p7884171711717"><a name="p7884171711717"></a><a name="p7884171711717"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p17884517161715"><a name="p17884517161715"></a><a name="p17884517161715"></a>Name of the variable to trace.</p>
</td>
</tr>
<tr id="row188419179172"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p206756457267"><a name="p206756457267"></a><a name="p206756457267"></a>value</p>
</td>
<td class="cellrowborder" valign="top" width="13.22%" headers="mcps1.1.5.1.2 "><p id="p9884917111710"><a name="p9884917111710"></a><a name="p9884917111710"></a>number</p>
</td>
<td class="cellrowborder" valign="top" width="10.67%" headers="mcps1.1.5.1.3 "><p id="p18841217141719"><a name="p18841217141719"></a><a name="p18841217141719"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p178842174174"><a name="p178842174174"></a><a name="p178842174174"></a>Value of the variable to trace.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let traceCount = 3;
bytrace.traceByValue("myTestCount", traceCount);
......
traceCount = 5;
bytrace.traceByValue("myTestCount", traceCount);
```
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the variable.|
| value | number | Yes| Value of the variable.|
**Example**
```
let traceCount = 3;
bytrace.traceByValue("myTestCount", traceCount);
traceCount = 4;
bytrace.traceByValue("myTestCount", traceCount);
// Service flow
```
# HiChecker
> **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import hichecker from '@ohos.hichecker';
```
## System Capabilities
SystemCapability.HiviewDFX.HiChecker
## Rule Constants
Provides the constants of all rule types.
| Name| Type| Description|
| ---------------------------------- | -------- | ------------------------------------------------------ |
| RULE_CAUTION_PRINT_LOG | BigInt | Alarm rule, which is programmed to print a log when an alarm is generated.|
| RULE_CAUTION_TRIGGER_CRASH | BigInt | Alarm rule. When an alarm is generated, the application exits.|
| RULE_THREAD_CHECK_SLOW_PROCESS | BigInt | Caution rule, which is programmed to detect whether any time-consuming function is invoked.|
| RULE_CHECK_SLOW_EVENT | BigInt | Caution rule, which is programmed to detect whether the event distribution or processing time has exceeded the specified time threshold.|
| RULE_CHECK_ABILITY_CONNECTION_LEAK | BigInt | Caution rule, which is programmed to detect whether ability leakage has occurred.|
## hichecker.addRule
addRule(rule: BigInt): void
Adds one or more rules. HiChecker detects unexpected operations or gives feedback based on the added rules.
**Parameters**
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | ---------------- |
| rule | BigInt | Yes| Rule to be added.|
**Example**
```
// Add a rule.
hichecker.addRule(hichecker.RULE_CAUTION_PRINT_LOG);
// Add multiple rules.
hichecker.addRule(
hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
```
## hichecker.removeRule
removeRule(rule: BigInt): void
Removes one or more rules. The removed rules will become ineffective.
**Parameters**
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | ---------------- |
| rule | BigInt | Yes| Rule to be removed.|
**Example**
```
// Remove a rule.
hichecker.removeRule(hichecker.RULE_CAUTION_PRINT_LOG);
// Remove multiple rules.
hichecker.removeRule(
hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
```
## hichecker.getRule
getRule(): BigInt
Obtains a collection of thread, process, and alarm rules that have been added.
**Return value**
| Type| Description|
| ------ | ---------------------- |
| BigInt | Collection of added rules.|
**Example**
```
// Add a rule.
hichecker.addRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS);
// Obtain the collection of added rules.
hichecker.getRule(); // return 1n;
```
## hichecker.contains
contains(rule: BigInt): boolean
Checks whether the specified rule exists in the collection of added rules. If the rule is of the thread level, this operation is performed only on the current thread.
**Parameters**
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | ---------------- |
| rule | BigInt | Yes| Rule to be checked.|
**Return value**
| Type| Description|
| ------- | ---------------------------------------------------------- |
| boolean | Returns **true** if the rule exists in the collection of added rules; returns **false** otherwise.|
**Example**
```
// Add a rule.
hichecker.addRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS);
// Check whether the added rule exists in the collection of added rules.
hichecker.contains(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS); // return true;
hichecker.contains(hichecker.RULE_CAUTION_PRINT_LOG); // return false;
```
# HiDebug
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
You can run the hidebug command to obtain the memory usage of an application, including the static heap memory (native heap) and proportional set size (PSS) occupied by the application process. You can also export VM memory slices and collect VM CPU profiling data.
## Modules to Import
```
import hidebug from '@ohos.hidebug';
```
## System Capabilities
SystemCapability.HiviewDFX.HiProfiler.HiDebug
## hidebug.getNativeHeapSize
getNativeHeapSize(): bigint
Obtains the total size of the native heap memory.
- **Return value**
| Type| Description|
| -------- | -------- |
| bigint | Total size of the native heap memory.|
- **Example**
```
let nativeHeapSize = hidebug.getNativeHeapSize();
```
## hidebug.getNativeHeapAllocatedSize
getNativeHeapAllocatedSize(): bigint
Obtains the size of the allocated native heap memory.
- **Return value**
| Type| Description|
| -------- | -------- |
| bigint | Size of the allocated native heap memory.|
- **Example**
```
let nativeHeapAllocatedSize = hidebug.getNativeHeapAllocatedSize();
```
## hidebug.getNativeHeapFreeSize
getNativeHeapFreeSize(): bigint
Obtains the size of the free native heap memory.
- **Return value**
| Type| Description|
| -------- | -------- |
| bigint | Size of the free native heap memory.|
- **Example**
```
let nativeHeapFreeSize = hidebug.getNativeHeapFreeSize();
```
## hidebug.getPss
getPss(): bigint
Obtains the PSS of this process.
- **Return value**
| Type| Description|
| -------- | -------- |
| bigint | PSS of the process.|
- **Example**
```
let pss = hidebug.getPss();
```
## hidebug.getSharedDirty
getSharedDirty(): bigint
Obtains the size of the shared dirty memory of this process.
- **Return value**
| Type| Description|
| -------- | -------- |
| bigint | Size of the shared dirty memory of the process.|
- **Example**
```
let sharedDirty = hidebug.getSharedDirty());
```
## hidebug.startProfiling
startProfiling(filename : string) : void
Starts the profiling method. `startProfiling()` and `stopProfiling()` are called in pairs. `startProfiling()` always occurs before `stopProfiling()`; that is, calling the functions in the following sequences is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | ------ | ---- | ------------------------------------------------------------ |
| filename | string | Yes| User-defined profile name. The `filename.json` file is generated in the `files` directory of the application based on the specified `filename`.|
**Example**
```js
hidebug.startProfiling("cpuprofiler-20220216");
// Code block
// ...
// Code block
hidebug.stopProfiling();
```
## hidebug.stopProfiling
stopProfiling() : void
Stops the profiling method. `stopProfiling()` and `startProfiling()` are called in pairs. `stopProfiling()` always occurs after `startProfiling()`; that is, calling the functions in the following sequences is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`.
**Example**
```js
hidebug.startProfiling("cpuprofiler-20220216");
// Code block
// ...
// Code block
hidebug.stopProfiling();
```
## hidebug.dumpHeapData
dumpHeapData(filename : string) : void
Exports the heap data.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | ------ | ---- | ------------------------------------------------------------ |
| filename | string | Yes| User-defined heap file name. The `filename.heapsnapshot` file is generated in the `files` directory of the app based on the specified `filename`.|
**Example**
```js
hidebug.dumpHeapData("heap-20220216");
```
# Distributed Call Chain Tracing
> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
......@@ -68,20 +68,24 @@ begin(name: string, flags: number = HiTraceFlag.DEFAULT): HiTraceId
Starts call chain tracing. This API works in synchronous manner.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Traced service name.|
| flags | number | Yes| [Trace flag combination](#hitraceflag).|
- Return value
| Type| Description|
| -------- | -------- |
| [HiTraceId](#hitraceid) | **HiTraceId** instance.|
- Example
```
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC | hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
```
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Traced service name.|
| flags | number | Yes| Trace flag combination. For details, see [HiTraceFlag](#hitraceflag).|
**Return value**
| Type| Description|
| -------- | -------- |
| [HiTraceId](#hitraceid) | **HiTraceId** instance.|
**Example**
```
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC | hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
```
## hiTraceChain.end
......@@ -89,17 +93,19 @@ end(id: HiTraceId): void
Stops call chain tracing. This API works in synchronous manner.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
**Example**
- Example
```
let asyncTraceId = hiTraceChain.begin("business");
// End the call chain tracing after the service logic is executed for several times.
hiTraceChain.end(asyncTraceId);
```
```
let asyncTraceId = hiTraceChain.begin("business");
// End the call chain tracing after the service logic is executed for several times.
hiTraceChain.end(asyncTraceId);
```
## hiTraceChain.getId
......@@ -107,17 +113,19 @@ getId(): HiTraceId
Obtains the trace ID. This API works in synchronous manner.
- Return value
| Type| Description|
| -------- | -------- |
| [HiTraceId](#hitraceid) | **HiTraceId** instance.|
**Return value**
| Type| Description|
| -------- | -------- |
| [HiTraceId](#hitraceid) | **HiTraceId** instance.|
- Example
```
let traceId = hiTraceChain.begin("business");
// Obtain the current trace ID after the service logic is executed for several times.
let curTraceId = hiTraceChain.getId();
```
**Example**
```
let traceId = hiTraceChain.begin("business");
// Obtain the current trace ID after the service logic is executed for several times.
let curTraceId = hiTraceChain.getId();
```
## hiTraceChain.setId
......@@ -125,17 +133,19 @@ setId(id: HiTraceId): void
Sets a trace ID. This API works in synchronous manner.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
- Example
```
let traceId = hiTraceChain.begin("business");
// Set the current trace ID after the service logic is executed for several times.
hiTraceChain.setId(asyncTraceId);
```
**Example**
```
let traceId = hiTraceChain.begin("business");
// Set the current trace ID after the service logic is executed for several times.
hiTraceChain.setId(asyncTraceId);
```
## hiTraceChain.clearId
......@@ -143,12 +153,13 @@ clearId(): void
Clears the trace ID. This API works in synchronous manner.
- Example
```
let traceId = hiTraceChain.begin("business");
// Clear the current trace ID after the service logic is executed for several times.
hiTraceChain.clearId();
```
**Example**
```
let traceId = hiTraceChain.begin("business");
// Clear the current trace ID after the service logic is executed for several times.
hiTraceChain.clearId();
```
## hiTraceChain.createSpan
......@@ -156,17 +167,19 @@ createSpan(): HiTraceId
Creates a trace span. This API works in synchronous manner.
- Return value
| Type| Description|
| -------- | -------- |
| [HiTraceId](#hitraceid) | **HiTraceId** instance.|
**Return value**
- Example
```
let traceId = hiTraceChain.begin("business");
// Create a trace span after the service logic is executed for several times.
let spanTraceId = hiTraceChain.createSpan();
```
| Type| Description|
| -------- | -------- |
| [HiTraceId](#hitraceid) | **HiTraceId** instance.|
**Example**
```
let traceId = hiTraceChain.begin("business");
// Create a trace span after the service logic is executed for several times.
let spanTraceId = hiTraceChain.createSpan();
```
## hiTraceChain.tracepoint
......@@ -174,20 +187,22 @@ tracepoint(mode: HiTraceCommunicationMode, type: HiTraceTracepointType, id: HiTr
Triggers a trace point. This API works in synchronous manner.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| mode | [HiTraceCommunicationMode](#hitracecommunicationmode) | Yes| Communication mode for the trace point.|
| type | [HiTraceTracepointType](#hitracetracepointtype)| Yes| Trace point type.|
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance for trace point triggering.|
| msg | string | No| Trace description passed for trace point triggering.|
- Example
```
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC | hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
// Trigger the trace point after the service logic is executed for several times.
hiTraceChain.tracepoint(hiTraceChain.HiTraceCommunicationMode.THREAD, hiTraceChain.HiTraceTracepointType.SS, asyncTraceId, "Just a example");
```
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| mode | [HiTraceCommunicationMode](#hitracecommunicationmode) | Yes| Communication mode for the trace point.|
| type | [HiTraceTracepointType](#hitracetracepointtype)| Yes| Trace point type.|
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance for trace point triggering.|
| msg | string | No| Trace description passed for trace point triggering.|
**Example**
```
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC | hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
// Trigger the trace point after the service logic is executed for several times.
hiTraceChain.tracepoint(hiTraceChain.HiTraceCommunicationMode.THREAD, hiTraceChain.HiTraceTracepointType.SS, asyncTraceId, "Just a example");
```
## hiTraceChain.isValid
......@@ -195,20 +210,24 @@ isValid(id: HiTraceId): boolean
Checks whether a **HiTraceId** instance is valid. This API works in synchronous manner.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
- Return value
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the **HiTraceId** instance is valid; returns **false** otherwise.|
- Example
```
let traceId = hiTraceChain.begin("business");
let traceIdIsvalid = hiTraceChain.isValid(traceId);
```
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the **HiTraceId** instance is valid; returns **false** otherwise.|
**Example**
```
let traceId = hiTraceChain.begin("business");
let traceIdIsvalid = hiTraceChain.isValid(traceId);
```
## hiTraceChain.isFlagEnabled
......@@ -216,21 +235,26 @@ isFlagEnabled(id: HiTraceId, flag: HiTraceFlag): boolean
Checks whether the specified trace flag in the **HiTraceId** instance is enabled. This API works in synchronous manner.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
| flag | [HiTraceFlag](#hitraceflag) | Yes| Specified trace flag.|
- Return value
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified trace flag in the **HiTraceId** instance is enabled; returns **false** otherwise.|
- Example
```
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
// The value of enabledDoNotCreateSpanFlag is true.
let enabledDoNotCreateSpanFlag = hiTraceChain.isFlagEnabled(asyncTraceId, hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
```
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
| flag | [HiTraceFlag](#hitraceflag) | Yes| Specified trace flag.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified trace flag in the **HiTraceId** instance is enabled; returns **false** otherwise.|
**Example**
```
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
// The value of enabledDoNotCreateSpanFlag is true.
let enabledDoNotCreateSpanFlag = hiTraceChain.isFlagEnabled(asyncTraceId, hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
```
## hiTraceChain.enableFlag
......@@ -238,16 +262,17 @@ enableFlag(id: HiTraceId, flag: HiTraceFlag): void
Enables the specified trace flag in the **HiTraceId** instance. This API works in synchronous manner.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
| flag | [HiTraceFlag](#hitraceflag) | Yes| Specified trace flag.|
- Example
```
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
hiTraceChain.enable(asyncTraceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
// The value of enabledDoNotCreateSpanFlag is true.
let enabledDoNotCreateSpanFlag = hiTraceChain.isFlagEnabled(asyncTraceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
```
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
| flag | [HiTraceFlag](#hitraceflag) | Yes| Specified trace flag.|
**Example**
```
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
hiTraceChain.enable(asyncTraceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
// The value of enabledDoNotCreateSpanFlag is true.
let enabledDoNotCreateSpanFlag = hiTraceChain.isFlagEnabled(asyncTraceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
```
# Performance Tracing
> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
......@@ -22,22 +22,24 @@ startTrace(name: string, taskId: number, expectedTime?: number): void
Starts a trace task. **expectedTime** is an optional parameter, which specifies the expected duration of the trace.
If multiple trace tasks with the same name need to be performed at the same time or a trace task needs to be performed multiple times concurrently, different task IDs must be specified in **startTrace**.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the trace task to start.|
| taskId | number | Yes| Task ID.|
| expectedTime | number | No| Expected duration of the trace, in ms.|
If the trace tasks with the same name are not performed at the same time, the same taskId can be used. For a specific example, refer to an example in [hiTraceMeter.finishTrace](#hitracemeterfinishtrace).
> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
> If multiple trace tasks with the same name need to be performed at the same time or a trace task needs to be performed multiple times concurrently, different task IDs must be specified in **startTrace**. If the trace tasks with the same name are not performed at the same time, the same task ID can be used. For details, see the **hiTraceMeter.finishTrace** example.
**Parameters**
- Example
```
hiTraceMeter.startTrace("myTestFunc", 1);
hiTraceMeter.startTrace("myTestFunc", 1, 5); // The expected duration of the trace task is 5 ms.
```
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the trace task to start.|
| taskId | number | Yes| Task ID.|
| expectedTime | number | No| Expected duration of the trace, in ms.|
**Example**
```
hiTraceMeter.startTrace("myTestFunc", 1);
hiTraceMeter.startTrace("myTestFunc", 1, 5); // The expected duration of the trace task is 5 ms.
```
## hiTraceMeter.finishTrace
......@@ -46,42 +48,42 @@ finishTrace(name: string, taskId: number): void
Stops a trace task.
To stop a trace task, the values of name and task ID in **finishTrace** must be the same as those in [startTrace](#hitracemeterstarttrace).
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the trace task to start.|
| taskId | number | Yes| Task ID.|
**Example**
```
hiTraceMeter.finishTrace("myTestFunc", 1);
```
```
// Start track tasks with the same name concurrently.
hiTraceMeter.startTrace("myTestFunc", 1);
// Service flow
hiTraceMeter.startTrace("myTestFunc", 2); // The second trace task starts while the first task is still running. The first and second tasks have the same name but different task IDs.
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 1);
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 2);
```
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the trace task to start.|
| taskId | number | Yes| Task ID.|
> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
> To stop a trace task, the values of name and task ID in **finishTrace** must be the same as those in **startTrace**.
- Example
```
hiTraceMeter.finishTrace("myTestFunc", 1);
```
```
// Start track tasks with the same name concurrently.
hiTraceMeter.startTrace("myTestFunc", 1);
// Service flow
hiTraceMeter.startTrace("myTestFunc", 2); // The second trace task starts while the first task is still running. The first and second tasks have the same name but different task IDs.
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 1);
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 2);
```
```
// Start track tasks with the same name at different times.
hiTraceMeter.startTrace("myTestFunc", 1);
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 1); // The first trace task ends.
// Service flow
hiTraceMeter.startTrace("myTestFunc", 1); // The second trace task starts after the first task ends. The two tasks have the same name and task ID.
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 1);
```
```
// Start track tasks with the same name at different times.
hiTraceMeter.startTrace("myTestFunc", 1);
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 1); // The first trace task ends.
// Service flow
hiTraceMeter.startTrace("myTestFunc", 1); // The second trace task starts after the first task ends. The two tasks have the same name and task ID.
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 1);
```
## hiTraceMeter.traceByValue
......@@ -90,18 +92,18 @@ traceByValue(name: string, value: number): void
Traces the value changes of a variable.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the variable.|
| value | number | Yes| Value of the variable.|
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the variable.|
| value | number | Yes| Value of the variable.|
- Example
```
let traceCount = 3;
hiTraceMeter.traceByValue("myTestCount", traceCount);
traceCount = 4;
hiTraceMeter.traceByValue("myTestCount", traceCount);
// Service flow
```
**Example**
```
let traceCount = 3;
hiTraceMeter.traceByValue("myTestCount", traceCount);
traceCount = 4;
hiTraceMeter.traceByValue("myTestCount", traceCount);
// Service flow
```
# Power Management<a name="EN-US_TOPIC_0000001133775514"></a>
# Power Management
>![](../../public_sys-resources/icon-note.gif) **NOTE:**
>The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import<a name="s56d19203690d4782bfc74069abb6bd71"></a>
## Modules to Import
```
import power from '@ohos.power';
......@@ -14,163 +15,94 @@ import power from '@ohos.power';
SystemCapability.PowerManager.PowerManager
## power.shutdownDevice<a name="section192192415554"></a>
## power.shutdownDevice
shutdownDevice\(reason: string\): void
shutdownDevice(reason: string): void
Shuts down the system.
- Parameters
<a name="table69661135912"></a>
<table><thead align="left"><tr id="row149668318915"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p7966738914"><a name="p7966738914"></a><a name="p7966738914"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p296713699"><a name="p296713699"></a><a name="p296713699"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p196718315911"><a name="p196718315911"></a><a name="p196718315911"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p9967231197"><a name="p9967231197"></a><a name="p9967231197"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row99671533914"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p5518501625"><a name="p5518501625"></a><a name="p5518501625"></a>reason</p>
</td>
<td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p11967433914"><a name="p11967433914"></a><a name="p11967433914"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p19671336916"><a name="p19671336916"></a><a name="p19671336916"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p69671631796"><a name="p69671631796"></a><a name="p69671631796"></a>Reason for system shutdown.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
power.shutdownDevice("shutdown_test");
console.info('power_shutdown_device_test success')
```
## power.rebootDevice<a name="section1773218207620"></a>
rebootDevice\(reason: string\): void
Reboots the system.
- Parameters
<a name="table8732192015611"></a>
<table><thead align="left"><tr id="row1173216204612"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p973222015615"><a name="p973222015615"></a><a name="p973222015615"></a>Name</p>
</th>
<th class="cellrowborder" valign="top" width="16.830000000000002%" id="mcps1.1.5.1.2"><p id="p273232015612"><a name="p273232015612"></a><a name="p273232015612"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="11.88%" id="mcps1.1.5.1.3"><p id="p9732202013618"><a name="p9732202013618"></a><a name="p9732202013618"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="56.47%" id="mcps1.1.5.1.4"><p id="p167326201566"><a name="p167326201566"></a><a name="p167326201566"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row773292016614"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p57320201162"><a name="p57320201162"></a><a name="p57320201162"></a>reason</p>
</td>
<td class="cellrowborder" valign="top" width="16.830000000000002%" headers="mcps1.1.5.1.2 "><p id="p973216201164"><a name="p973216201164"></a><a name="p973216201164"></a>string</p>
</td>
<td class="cellrowborder" valign="top" width="11.88%" headers="mcps1.1.5.1.3 "><p id="p5732132020612"><a name="p5732132020612"></a><a name="p5732132020612"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="56.47%" headers="mcps1.1.5.1.4 "><p id="p4733720662"><a name="p4733720662"></a><a name="p4733720662"></a>Reason for system reboot.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
power.rebootDevice("reboot_test");
console.info('power_reboot_device_test success')
```
## power.isScreenOn<a name="section69511458115312"></a>
isScreenOn\(callback: AsyncCallback<boolean\>\): void
Before using this API, you must declare the **ohos.permission.SHUTDOWN** permission.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| reason | string | Yes| Reason for system shutdown.|
**Example**
```
power.shutdownDevice("shutdown_test");
console.info('power_shutdown_device_test success')
```
## power.rebootDevice
rebootDevice(reason: string): void
Restarts the device.
Before using this API, you must declare the **ohos.permission.REBOOT** permission (to reboot) or the **ohos.permission.REBOOT_RECOVERY** permission (to reboot and enter the recovery mode)
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| reason | string | Yes| Reason for system reboot.|
**Example**
```
power.rebootDevice("reboot_test");
console.info('power_reboot_device_test success')
```
## power.isScreenOn
isScreenOn(callback: AsyncCallback&lt;boolean&gt;): void
Checks the screen status of the current device.
- Parameters
<a name="table183681450191519"></a>
<table><thead align="left"><tr id="row20368050151519"><th class="cellrowborder" valign="top" width="15.989999999999998%" id="mcps1.1.5.1.1"><p id="p13681150121519"><a name="p13681150121519"></a><a name="p13681150121519"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="28.27%" id="mcps1.1.5.1.2"><p id="p1636895015154"><a name="p1636895015154"></a><a name="p1636895015154"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="9.64%" id="mcps1.1.5.1.3"><p id="p7565410191617"><a name="p7565410191617"></a><a name="p7565410191617"></a>Mandatory</p>
</th>
<th class="cellrowborder" valign="top" width="46.1%" id="mcps1.1.5.1.4"><p id="p43681350181515"><a name="p43681350181515"></a><a name="p43681350181515"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row636865081513"><td class="cellrowborder" valign="top" width="15.989999999999998%" headers="mcps1.1.5.1.1 "><p id="p4368165091513"><a name="p4368165091513"></a><a name="p4368165091513"></a>callback</p>
</td>
<td class="cellrowborder" valign="top" width="28.27%" headers="mcps1.1.5.1.2 "><p id="p83691450151519"><a name="p83691450151519"></a><a name="p83691450151519"></a>AsyncCallback&lt;boolean&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="9.64%" headers="mcps1.1.5.1.3 "><p id="p17565310191610"><a name="p17565310191610"></a><a name="p17565310191610"></a>Yes</p>
</td>
<td class="cellrowborder" valign="top" width="46.1%" headers="mcps1.1.5.1.4 "><p id="p163691850191515"><a name="p163691850191515"></a><a name="p163691850191515"></a>Callback used to obtain the return value.</p>
<p id="p3566124615812"><a name="p3566124615812"></a><a name="p3566124615812"></a>The value <strong id="b4350172620165"><a name="b4350172620165"></a><a name="b4350172620165"></a>true</strong> indicates that the screen is on, and value <strong id="b035432613168"><a name="b035432613168"></a><a name="b035432613168"></a>false</strong> indicates the opposite.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
power.isScreenOn((error, screenOn) => {
**Parameters**
| Type| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to obtain the return value. <br/>The value **true** indicates that the screen is on, and value **false** indicates the opposite.|
**Example**
```
power.isScreenOn((error, screenOn) => {
if (typeof error === "undefined") {
console.info('screenOn status is ' + screenOn);
} else {
console.log('error: ' + error);
}
})
```
})
```
## power.isScreenOn<a name="section143367619119"></a>
## power.isScreenOn
isScreenOn\(\): Promise<boolean\>
isScreenOn(): Promise&lt;boolean&gt;
Checks the screen status of the current device.
- Return values
<a name="table1730417311314"></a>
<table><thead align="left"><tr id="row1930412311018"><th class="cellrowborder" valign="top" width="23.87%" id="mcps1.1.3.1.1"><p id="p15304113117114"><a name="p15304113117114"></a><a name="p15304113117114"></a>Type</p>
</th>
<th class="cellrowborder" valign="top" width="76.13%" id="mcps1.1.3.1.2"><p id="p830411311017"><a name="p830411311017"></a><a name="p830411311017"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row173045311711"><td class="cellrowborder" valign="top" width="23.87%" headers="mcps1.1.3.1.1 "><p id="p686517815914"><a name="p686517815914"></a><a name="p686517815914"></a>Promise&lt;boolean&gt;</p>
</td>
<td class="cellrowborder" valign="top" width="76.13%" headers="mcps1.1.3.1.2 "><p id="p163041531916"><a name="p163041531916"></a><a name="p163041531916"></a>Promise used to asynchronously obtain the return value. The value <strong id="b1934154320392"><a name="b1934154320392"></a><a name="b1934154320392"></a>true</strong> indicates that the screen is on, and value <strong id="b1693454393914"><a name="b1693454393914"></a><a name="b1693454393914"></a>false</strong> indicates the opposite.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
power.isScreenOn()
.then(screenOn => {
console.info('screenOn status is ' + screenOn);
})
.catch(error => {
console.log('error: ' + error);
})
```
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;boolean&gt; | Promise used to asynchronously obtain the return value. <br/>The value **true** indicates that the screen is on, and value **false** indicates the opposite.|
**Example**
```
power.isScreenOn()
.then(screenOn => {
console.info('screenOn status is ' + screenOn);
})
.catch(error => {
console.log('error: ' + error);
})
```
# SIM Management
>**Note:**
>**NOTE**
>
>The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
......@@ -18,21 +18,20 @@ Obtains the integrated circuit card identity (ICCID) of the SIM card in the spe
Before using this API, you must declare the **ohos.permission.GET\_TELEPHONY\_STATE** permission.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the result.|
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the result.|
**Example**
- Example
```
sim.getSimIccId(0, (err, data) => {
```
sim.getSimIccId(0, (err, data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
});
```
## sim.getSimIccId<a name=sim.getSimIccId-promise></a>
......@@ -43,28 +42,28 @@ Obtains the ICCID of the SIM card in the specified slot. This function uses a pr
Before using this API, you must declare the **ohos.permission.GET\_TELEPHONY\_STATE** permission.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
- Return values
**Return value**
| Type| Description|
| --------------------- | ---------------------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
| Type| Description|
| --------------------- | ---------------------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
- Example
**Example**
```
let promise = sim.getSimIccId(0);
promise.then(data => {
```
let promise = sim.getSimIccId(0);
promise.then(data => {
console.log(`getSimIccId success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
}).catch(err => {
console.log(`getSimIccId fail, promise: err->${JSON.stringify(err)}`);
});
```
});
```
## sim.getDefaultVoiceSlotId<sup>7+</sup><a name= sim.getDefaultVoiceSlotId-callback></a>
......@@ -72,19 +71,19 @@ getDefaultVoiceSlotId\(callback: AsyncCallback<number\>\): void
Obtains the default slot ID of the SIM card that provides voice services. This function uses an asynchronous callback to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;number&gt; | Yes| Callback used to return the result.|
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked to traverse the entries in the container.|
- Example
**Example**
```
sim.getDefaultVoiceSlotId((err, data) => {
```
sim.getDefaultVoiceSlotId((err, data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
});
```
## sim.getDefaultVoiceSlotId<sup>7+</sup><a name=sim.getDefaultVoiceSlotId-promise></a>
......@@ -93,22 +92,22 @@ getDefaultVoiceSlotId\(\): Promise<number\>
Obtains the default slot ID of the SIM card that provides voice services. This function uses a promise to return the result.
- Return values
**Return value**
| Type| Description|
| ----------------- | --------------------------------------- |
| Promise\<number\> | Promise used to return the result.|
| Type| Description|
| ----------------- | --------------------------------------- |
| Promise\<number\> | Promise used to return the result.|
- Example
**Example**
```
let promise = sim.getDefaultVoiceSlotId();
promise.then(data => {
```
let promise = sim.getDefaultVoiceSlotId();
promise.then(data => {
console.log(`getDefaultVoiceSlotId success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
}).catch(err => {
console.log(`getDefaultVoiceSlotId fail, promise: err->${JSON.stringify(err)}`);
});
```
});
```
## sim.getISOCountryCodeForSim<a name=sim.getISOCountryCodeForSim-callback></a>
......@@ -116,20 +115,20 @@ getISOCountryCodeForSim\(slotId: number, callback: AsyncCallback<string\>\): voi
Obtains the ISO country code of the SIM card in the specified slot. This function uses an asynchronous callback to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | ----------------------- | ---- | ---------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback\<string\> | Yes| Callback used to return the result, which is a country code, for example, **CN** (China).|
| Name| Type| Mandatory| Description|
| -------- | ----------------------- | ---- | ---------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback\<string\> | Yes| Callback used to return the result, which is a country code, for example, **CN** (China).|
- Example
**Example**
```
sim.getISOCountryCodeForSim(0, (err, data) => {
```
sim.getISOCountryCodeForSim(0, (err, data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
});
```
## sim.getISOCountryCodeForSim<a name=sim.getISOCountryCodeForSim-promise></a>
......@@ -138,28 +137,28 @@ getISOCountryCodeForSim\(slotId: number\): Promise<string\>
Obtains the ISO country code of the SIM card in the specified slot. This function uses a promise to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
- Return values
**Return value**
| Type| Description|
| ----------------- | ------------------------------------------------------------ |
| Promise\<string\> | Promise used to return the result, which is a country code, for example, **CN** (China).|
| Type| Description|
| ----------------- | ------------------------------------------------------------ |
| Promise\<string\> | Promise used to return the result, which is a country code, for example, **CN** (China).|
- Example
**Example**
```
let promise = sim.getISOCountryCodeForSim(0);
promise.then(data => {
```
let promise = sim.getISOCountryCodeForSim(0);
promise.then(data => {
console.log(`getISOCountryCodeForSim success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
}).catch(err => {
console.log(`getISOCountryCodeForSim fail, promise: err->${JSON.stringify(err)}`);
});
```
});
```
## sim.getSimOperatorNumeric<a name=sim.getSimOperatorNumeric-callback></a>
......@@ -168,20 +167,20 @@ getSimOperatorNumeric\(slotId: number, callback: AsyncCallback<string\>\): void
Obtains the public land mobile network (PLMN) ID of the SIM card in the specified slot. This function uses an asynchronous callback to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | ----------------------- | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback\<string\> | Yes| Callback used to return the result.|
| Name| Type| Mandatory| Description|
| -------- | ----------------------- | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback\<string\> | Yes| Callback used to return the result.|
- Example
**Example**
```
sim.getSimOperatorNumeric(0, (err, data) => {
```
sim.getSimOperatorNumeric(0, (err, data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
});
```
## sim.getSimOperatorNumeric<a name=sim.getSimOperatorNumeric-promise></a>
......@@ -190,28 +189,28 @@ getSimOperatorNumeric\(slotId: number\): Promise<string\>
Obtains the PLMN ID of the SIM card in the specified slot. This function uses a promise to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
- Return values
**Return value**
| Type| Description|
| ----------------- | ------------------------------------------------ |
| Promise\<string\> | Promise used to return the result.|
| Type| Description|
| ----------------- | ------------------------------------------------ |
| Promise\<string\> | Promise used to return the result.|
- Example
**Example**
```
let promise = sim.getSimOperatorNumeric(0);
promise.then(data => {
```
let promise = sim.getSimOperatorNumeric(0);
promise.then(data => {
console.log(`getSimOperatorNumeric success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
}).catch(err => {
console.log(`getSimOperatorNumeric fail, promise: err->${JSON.stringify(err)}`);
});
```
});
```
## sim.getSimSpn<a name=sim.getSimSpn-callback></a>
......@@ -220,20 +219,20 @@ getSimSpn\(slotId: number, callback: AsyncCallback<string\>\): void
Obtains the service provider name (SPN) of the SIM card in the specified slot. This function uses an asynchronous callback to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | ----------------------- | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback\<string\> | Yes| Callback used to return the result.|
| Name| Type| Mandatory| Description|
| -------- | ----------------------- | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback\<string\> | Yes| Callback used to return the result.|
- Example
**Example**
```
sim.getSimSpn(0, (err, data) => {
```
sim.getSimSpn(0, (err, data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
});
```
## sim.getSimSpn<a name=sim.getSimSpn-promise></a>
......@@ -242,28 +241,28 @@ getSimSpn\(slotId: number\): Promise<string\>
Obtains the SPN of the SIM card in the specified slot. This function uses a promise to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
- Return values
**Return value**
| Type| Description|
| ----------------- | ----------------------------------------- |
| Promise\<string\> | Promise used to return the result.|
| Type| Description|
| ----------------- | ----------------------------------------- |
| Promise\<string\> | Promise used to return the result.|
- Example
**Example**
```
let promise = sim.getSimSpn(0);
promise.then(data => {
```
let promise = sim.getSimSpn(0);
promise.then(data => {
console.log(`getSimSpn success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
}).catch(err => {
console.log(`getSimSpn fail, promise: err->${JSON.stringify(err)}`);
});
```
});
```
## sim.getSimState<a name=sim.getSimState-callback></a>
......@@ -272,20 +271,20 @@ getSimState\(slotId: number, callback: AsyncCallback<SimState\>\): void
Obtains the status of the SIM card in the specified slot. This function uses an asynchronous callback to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------------------------------------- | ---- | ----------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback\<[SimState](#SimState)\> | Yes| Callback used to return the result. For details, see [SimState](#SimState).|
| Name| Type| Mandatory| Description|
| -------- | -------------------------------------- | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback\<[SimState](#SimState)\> | Yes| Callback used to return the result, For details, see [SimState](#SimState).|
- Example
**Example**
```
sim.getSimState(0, (err, data) => {
```
sim.getSimState(0, (err, data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
});
```
## sim.getSimState<a name=sim.getSimState-promise></a>
......@@ -294,28 +293,28 @@ getSimState\(slotId: number\): Promise<SimState\>
Obtains the status of the SIM card in the specified slot. This function uses a promise to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
- Return values
**Return value**
| Type| Description|
| -------------------------------- | ------------------------------------------ |
| Promise\<[SimState](#SimState)\> | Promise used to return the result.|
| Type| Description|
| -------------------------------- | ------------------------------------------ |
| Promise\<[SimState](#SimState)\> | Promise used to return the result.|
- Example
**Example**
```
let promise = sim.getSimState(0);
promise.then(data => {
```
let promise = sim.getSimState(0);
promise.then(data => {
console.log(`getSimState success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
}).catch(err => {
console.log(`getSimState fail, promise: err->${JSON.stringify(err)}`);
});
```
});
```
## sim.getSimGid1<a name=sim.getSimGid1-callback></a>
......@@ -325,20 +324,20 @@ Obtains the group identifier level 1 (GID1) of the SIM card in the specified slo
Before using this API, you must declare the **ohos.permission.GET\_TELEPHONY\_STATE** permission.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | ----------------------- | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback\<string\> | Yes| Callback used to return the result.|
| Name| Type| Mandatory| Description|
| -------- | ----------------------- | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| callback | AsyncCallback\<string\> | Yes| Callback used to return the result.|
- Example
**Example**
```
sim.getSimGid1(0, (err, data) => {
```
sim.getSimGid1(0, (err, data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
});
```
## sim.getSimGid1<a name=sim.getSimGid1-promise></a>
......@@ -349,28 +348,28 @@ Obtains the GID1 of the SIM card in the specified slot. This function uses a pro
Before using this API, you must declare the **ohos.permission.GET\_TELEPHONY\_STATE** permission.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | -------------------------------------- |
| slotId | number | Yes| Card slot ID. The options are as follows: <br/> - **0**: slot 1 <br/> - **1**: slot 2|
- Return values
**Return value**
| Type| Description|
| ----------------- | ------------------------------------------------------------ |
| Promise\<string\> | Promise used to return the result.|
| Type| Description|
| ----------------- | ------------------------------------------------------------ |
| Promise\<string\> | Promise used to return the result.|
- Example
**Example**
```
let promise = sim.getSimGid1(0);
promise.then(data => {
```
let promise = sim.getSimGid1(0);
promise.then(data => {
console.log(`getSimGid1 success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
}).catch(err => {
console.log(`getSimGid1 fail, promise: err->${JSON.stringify(err)}`);
});
```
});
```
## SimState<a name=SimState></a>
......
# SMS
>**Note:**
>**NOTE**
>
>The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
......@@ -16,24 +16,24 @@ createMessage\(pdu: Array<number\>, specification: string, callback: AsyncCallba
Creates an SMS message instance based on the protocol data unit (PDU) and the specified SMS protocol. This function uses an asynchronous callback to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| ------------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ |
| pdu | Array&lt;number&gt; | Yes| Protocol data unit, which is obtained from the received SMS message.|
| specification | string | Yes| SMS protocol type. The options are as follows: <br/> - **3gpp**: GSM/UMTS/LTE SMS<br/> - **3gpp2**: CDMA SMS|
| callback | AsyncCallback&lt;[ShortMessage](#ShortMessage)&gt; | Yes| Callback used to return the result.|
| Name| Type| Mandatory| Description|
| ------------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ |
| pdu | Array&lt;number&gt; | Yes| PDU, which is obtained from the received SMS message.|
| specification | string | Yes| SMS protocol type. The options are as follows: <br/> - **3gpp**: GSM/UMTS/LTE SMS<br/> - **3gpp2**: CDMA SMS|
| callback | AsyncCallback&lt;[ShortMessage](#ShortMessage)&gt; | Yes| Callback used to return the result.|
- Example
**Example**
```
const specification = '3gpp';
// Display PDUs using numbers in an array, for example, [0x08, 0x91, ...].
const pdu = [0x08, 0x91];
sms.createMessage(pdu, specification, (err, data) => {
```
const specification = '3gpp';
// Display PDUs using numbers in an array, for example, [0x08, 0x91, ...].
const pdu = [0x08, 0x91];
sms.createMessage(pdu, specification, (err, data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
});
```
## sms.createMessage<a name=sms.createMessage-promise></a>
......@@ -42,32 +42,32 @@ createMessage\(pdu: Array<number\>, specification: string\): Promise<ShortMessag
Creates an SMS message instance based on the PDU and the specified SMS protocol. This function uses a promise to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| ------------- | ------------------- | ---- | ------------------------------------------------------------ |
| pdu | Array&lt;number&gt; | Yes| Protocol data unit, which is obtained from the received SMS message.|
| specification | string | Yes| SMS protocol type. The options are as follows: <br/> - **3gpp**: GSM/UMTS/LTE SMS<br/> - **3gpp2**: CDMA SMS|
| Name| Type| Mandatory| Description|
| ------------- | ------------------- | ---- | ------------------------------------------------------------ |
| pdu | Array&lt;number&gt; | Yes| PDU, which is obtained from the received SMS message.|
| specification | string | Yes| SMS protocol type. The options are as follows: <br/> - **3gpp**: GSM/UMTS/LTE SMS<br/> - **3gpp2**: CDMA SMS|
- Return values
**Return value**
| Type| Description|
| -------------------------------------------- | --------------------------------- |
| Promise&lt;[ShortMessage](#ShortMessage)&gt; | Promise used to return the result.|
| Type| Description|
| -------------------------------------------- | --------------------------------- |
| Promise&lt;[ShortMessage](#ShortMessage)&gt; | Promise used to return the result.|
- Example
**Example**
```
const specification = '3gpp';
// Display PDUs using numbers in an array, for example, [0x08, 0x91, ...].
const pdu = [0x08, 0x91];
let promise = sms.createMessage(pdu, specification);
promise.then(data => {
```
const specification = '3gpp';
// Display PDUs using numbers in an array, for example, [0x08, 0x91, ...].
const pdu = [0x08, 0x91];
let promise = sms.createMessage(pdu, specification);
promise.then(data => {
console.log(`createMessage success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
}).catch(err => {
console.error(`createMessage fail, promise: err->${JSON.stringify(err)}`);
});
```
});
```
## sms.sendMessage
......@@ -77,29 +77,29 @@ Sends an SMS message.
Before using this API, you must declare the **ohos.permission.SEND_MESSAGES** permission.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| options | [SendMessageOptions](#SendMessageOptions) | Yes| Options (including the callback) for sending an SMS message. For details, see [SendMessageOptions](#SendMessageOptions).|
| Name| Type| Mandatory| Description|
| ------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| options | [SendMessageOptions](#SendMessageOptions) | Yes| Options (including the callback) for sending an SMS message. For details, see [SendMessageOptions](#SendMessageOptions).|
- Example
**Example**
```
let sendCallback = function (err, data) {
```
let sendCallback = function (err, data) {
console.log(`sendCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
}
let deliveryCallback = function (err, data) {
}
let deliveryCallback = function (err, data) {
console.log(`deliveryCallback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
}
let slotId = 0;
let content ='SMS message content';
let destinationHost = '+861xxxxxxxxxx';
let serviceCenter = '+861xxxxxxxxxx';
let destinationPort = 1000;
let options = {slotId, content, destinationHost, serviceCenter, destinationPort, sendCallback, deliveryCallback};
sms.sendMessage(options);
```
}
let slotId = 0;
let content ='SMS message content';
let destinationHost = '+861xxxxxxxxxx';
let serviceCenter = '+861xxxxxxxxxx';
let destinationPort = 1000;
let options = {slotId, content, destinationHost, serviceCenter, destinationPort, sendCallback, deliveryCallback};
sms.sendMessage(options);
```
## sms.getDefaultSmsSlotId<sup>7+</sup><a name=sms.getDefaultSmsSlotId-callback></a>
......@@ -108,19 +108,19 @@ getDefaultSmsSlotId\(callback: AsyncCallback<number\>\): void
Obtains the default slot of the SIM card used to send SMS messages. This function uses an asynchronous callback to return the result.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | ---------------------------------------- |
| callback | AsyncCallback&lt;number&gt; | Yes| Callback used to return the result. <br/> - **0**: slot 1<br/> - **1**: slot 2|
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | ---------------------------------------- |
| callback | AsyncCallback&lt;number&gt; | Yes| Callback used to return the result, <br/> - **0**: slot 1 <br/> - **1**: slot 2|
- Example
**Example**
```
sms.getDefaultSmsSlotId((err, data) => {
```
sms.getDefaultSmsSlotId((err, data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
});
```
## sms.getDefaultSmsSlotId<sup>7+</sup><a name=sms.getDefaultSmsSlotId-promise></a>
......@@ -129,22 +129,22 @@ getDefaultSmsSlotId\(\): Promise<number\>
Obtains the default slot of the SIM card used to send SMS messages. This function uses a promise to return the result.
- Return values
**Return value**
| Type| Description|
| --------------- | ------------------------------------------------------------ |
| Promise<number> | Promise used to return the result.<br/> - **0**: slot 1<br/> - **1**: slot 2|
| Type| Description|
| --------------- | ------------------------------------------------------------ |
| Promise<number> | Promise used to return the result.<br/> - **0**: slot 1<br/> - **1**: slot 2|
- Example
**Example**
```
let promise = call.getDefaultSmsSlotId();
promise.then(data => {
```
let promise = call.getDefaultSmsSlotId();
promise.then(data => {
console.log(`getDefaultSmsSlotId success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
}).catch(err => {
console.error(`getDefaultSmsSlotId fail, promise: err->${JSON.stringify(err)}`);
});
```
});
```
## sms.setSmscAddr<sup>7+</sup><a name=sms.setSmscAddr-callback></a>
......@@ -155,23 +155,23 @@ Sets the short message service center (SMSC) address. This function uses an asyn
Before using this API, you must declare the **ohos.permission.SET\_TELEPHONY\_STATE** permission.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | ------------------------- | ---- | ----------------------------------------- |
| slotId | number | Yes| SIM card slot ID. The options are as follows: <br/> - **0**: slot 1<br/> - **1**: slot 2|
| smscAddr | string | Yes| SMSC address. |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
| Name| Type| Mandatory| Description|
| -------- | ------------------------- | ---- | ----------------------------------------- |
| slotId | number | Yes| SIM card slot ID: <br/> - **0**: slot 1<br/> - **1**: slot 2|
| smscAddr | string | Yes| SMSC address. |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
- Example
**Example**
```
let slotId = 0;
let smscAddr = '+861xxxxxxxxxx';
sms.setSmscAddr(slotId, smscAddr, (err,data) => {
```
let slotId = 0;
let smscAddr = '+861xxxxxxxxxx';
sms.setSmscAddr(slotId, smscAddr, (err,data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
});
```
## sms.setSmscAddr<sup>7+</sup><a name=sms.setSmscAddr-promise></a>
......@@ -182,31 +182,31 @@ Sets the SMSC address. This function uses a promise to return the result.
Before using this API, you must declare the **ohos.permission.SET\_TELEPHONY\_STATE** permission.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | ------ | ---- | ----------------------------------------- |
| slotId | number | Yes| SIM card slot ID. The options are as follows: <br/> - **0**: slot 1<br/> - **1**: slot 2|
| smscAddr | string | Yes| SMSC address.|
| Name| Type| Mandatory| Description|
| -------- | ------ | ---- | ----------------------------------------- |
| slotId | number | Yes| SIM card slot ID: <br/> - **0**: slot 1<br/> - **1**: slot 2|
| smscAddr | string | Yes| SMSC address. |
- Return values
**Return value**
| Type| Description|
| ------------------- | ------------------------------- |
| Promise&lt;void&gt; | Promise used to return the result.|
| Type| Description|
| ------------------- | ------------------------------- |
| Promise&lt;void&gt; | Promise used to return the result.|
- Example
**Example**
```
let slotId = 0;
let smscAddr = '+861xxxxxxxxxx';
let promise = sms.setSmscAddr(slotId, smscAddr);
promise.then(data => {
```
let slotId = 0;
let smscAddr = '+861xxxxxxxxxx';
let promise = sms.setSmscAddr(slotId, smscAddr);
promise.then(data => {
console.log(`setSmscAddr success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
}).catch(err => {
console.error(`setSmscAddr fail, promise: err->${JSON.stringify(err)}`);
});
```
});
```
## sms.getSmscAddr<sup>7+</sup><a name=sms.getSmscAddr-callback></a>
......@@ -217,21 +217,21 @@ Obtains the SMSC address. This function uses an asynchronous callback to return
Before using this API, you must declare the **ohos.permission.GET\_TELEPHONY\_STATE** permission.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | ----------------------------------------- |
| slotId | number | Yes| SIM card slot ID. The options are as follows: <br/> - **0**: slot 1<br/> - **1**: slot 2|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the result.|
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | ----------------------------------------- |
| slotId | number | Yes| SIM card slot ID: <br/> - **0**: slot 1<br/> - **1**: slot 2|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the result.|
- Example
**Example**
```
let slotId = 0;
sms.getSmscAddr(slotId, (err, data) => {
```
let slotId = 0;
sms.getSmscAddr(slotId, (err, data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
});
```
## sms.getSmscAddr<sup>7+</sup><a name=sms.getSmscAddr-promise></a>
......@@ -242,29 +242,29 @@ Obtains the SMSC address. This function uses a promise to return the result.
Before using this API, you must declare the **ohos.permission.GET\_TELEPHONY\_STATE** permission.
- Parameters
**Parameters**
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | ----------------------------------------- |
| slotId | number | Yes| SIM card slot ID. The options are as follows: <br/> - **0**: slot 1<br/> - **1**: slot 2|
| Name| Type| Mandatory| Description|
| ------ | ------ | ---- | ----------------------------------------- |
| slotId | number | Yes| SIM card slot ID: <br/> - **0**: slot 1<br/> - **1**: slot 2|
- Return values
**Return value**
| Type| Description|
| --------------------- | --------------------------------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
| Type| Description|
| --------------------- | --------------------------------------------- |
| Promise&lt;string&gt; | Promise used to return the result.|
- Example
**Example**
```
let slotId = 0;
let promise = sms.getSmscAddr(slotId);
promise.then(data => {
```
let slotId = 0;
let promise = sms.getSmscAddr(slotId);
promise.then(data => {
console.log(`getSmscAddr success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
}).catch(err => {
console.error(`getSmscAddr fail, promise: err->${JSON.stringify(err)}`);
});
```
});
```
## ShortMessage<a name=ShortMessage></a>
......@@ -281,13 +281,13 @@ Defines an SMS message instance.
| isSmsStatusReportMessage | boolean | Whether the received SMS message is an SMS delivery status report. The default value is **false**. <br/>SMS delivery status report: a message sent from the SMSC to show the current status of the SMS message you delivered.|
| messageClass | [ShortMessageClass](#ShortMessageClass) | SMS message type.|
| pdu | Array&lt;number&gt; | PDU in the SMS message.|
|protocolId|number|Protocol identifier used for delivering the SMS message.|
|scAddress|string|SMSC address.|
|scTimestamp|number|SMSC timestamp.|
|status|number|SMS message status sent by the SMSC in the **SMS-STATUS-REPORT** message.|
|userRawData|Array&lt;number&gt;|User data excluding the data header.|
|visibleMessageBody|string|SMS message body.|
|visibleRawAddress|string|Sender address to be displayed on the UI.|
| protocolId | number | ID of the protocol used for sending SMS messages.|
| scAddress | string | Address of the short message service center (SMSC).|
| scTimestamp | number | SMSC timestamp.|
| status | number | SMS message status sent by the SMSC in the **SMS-STATUS-REPORT** message.|
| userRawData | Array&lt;number&gt; | User data except the data header.|
| visibleMessageBody | string | SMS message body.|
| visibleRawAddress | string | Sender address.|
## ShortMessageClass<a name=ShortMessageClass></a>
......@@ -315,7 +315,7 @@ For example, you can specify the SMS message type by the optional parameter **co
| destinationHost | string | Yes| Destination address of the SMS message.|
| content | string \| Array&lt;number&gt; | Yes| SMS message type. If the content is comprised of character strings, the SMS message is a text message. If the content is comprised of byte arrays, the SMS message is a data message.|
| serviceCenter | string | No| SMSC address. By default, the SMSC address in the SIM card is used.|
| destinationPort | number | No| Destination port of the SMS message. This parameter is mandatory only for a data message. |
| destinationPort | number | No| Destination port of the SMS message. This parameter is mandatory only for a data message. Otherwise, it is optional.|
| sendCallback | AsyncCallback&lt;[ISendShortMessageCallback](#ISendShortMessageCallback)&gt; | No| Callback used to return the SMS message sending result. For details, see [ISendShortMessageCallback](#ISendShortMessageCallback).|
| deliveryCallback | AsyncCallback&lt;[IDeliveryShortMessageCallback](#IDeliveryShortMessageCallback)&gt; | No| Callback used to return the SMS message delivery report. For details, see [IDeliveryShortMessageCallback](#IDeliveryShortMessageCallback).|
......@@ -333,7 +333,7 @@ Provides the callback for the SMS message delivery report. It consists of three
## IDeliveryShortMessageCallback<a name=IDeliveryShortMessageCallback></a>
Provides the callback for the SMS message delivery report.
Provides the callback for the SMS message delivery report. Return the SMS delivery report.
| Name| Type| Mandatory| Description|
| ------ | ------------------- | ---- | -------------- |
......@@ -346,7 +346,7 @@ Enumerates SMS message sending results.
| Name| Value| Description|
| ------------------------------------ | ---- | ------------------------------------------------------ |
| SEND_SMS_SUCCESS | 0 | The SMS message is sent successfully.|
| SEND_SMS_SUCCESS | 0 | SMS message sent successfully.|
| SEND_SMS_FAILURE_UNKNOWN | 1 | Failed to send the SMS message due to unknown reasons.|
| SEND_SMS_FAILURE_RADIO_OFF | 2 | Failed to send the SMS message because the modem is shut down.|
| SEND_SMS_FAILURE_SERVICE_UNAVAILABLE | 3 | Failed to send the SMS message because the network is unavailable or SMS message sending or receiving is not supported.|
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册