• Home
Name Date Size #Lines LOC

..--

figures/12-May-2024-

frameworks/12-May-2024-2,5812,062

interfaces/12-May-2024-365101

sa_profile/12-May-2024-4728

service/12-May-2024-1,4371,204

test/12-May-2024-1,044788

BUILD.gnD12-May-20241.9 KiB5849

LICENSED12-May-20249.9 KiB177150

README.mdD12-May-20246 KiB11582

README_zh.mdD12-May-20245.9 KiB12692

bundle.jsonD12-May-20242.8 KiB8785

README.md

1# State Registry<a name="EN-US_TOPIC_0000001152064139"></a>
2
3
4## Introduction<a name="section117mcpsimp"></a>
5
6The state registry module provides APIs to register and deregister an observer that listens for various callback events of the telephony subsystem. Such events include but are not limited to the following: network status change, signal strength change, cell information change, cellular data connection status change, and call status change.
7
8**Figure  1**  Architecture of the state registry module<a name="fig13267152558"></a>
9![](figures/state-registry-module.architecture.png)
10
11## Directory Structure<a name="section124mcpsimp"></a>
12
13```
14/base/telephony/state_registry      # State registry service
15├─ figures                          # Figures of readme files
16├─ frameworks                       # Framework layer
17│  ├─ js                            # JS code
18│  └─ native                        # Native code
19├─ interfaces                       # APIs
20│  ├─ innerkits                     # Internal APIs
21│  └─ kits                          # External APIs \(such as JS APIs\)
22├─ sa_profile                       # SA profile
23├─ service                          # Service code
24└─ test                             # Test code
25   ├─ mock                          # Simulation test
26   └─ unittest                      # Unit test
27```
28
29## Constraints<a name="section128mcpsimp"></a>
30
31-   Programming language: JavaScript
32-   Software constraints: this service needs to work with the telephony core service \(core\_service\).
33-   Hardware constraints: the accommodating device must be equipped with a modem and a SIM card capable of independent cellular communication.
34-   The API for registering an observer for the SIM card status takes effect only when SIM cards are in position. If SIM cards are removed, no callback events will be received. Your application can call the  **getSimState**  API to check whether SIM cards are in position.
35
36## Usage<a name="section134mcpsimp"></a>
37
38### Available APIs<a name="section136mcpsimp"></a>
39
40**Table  1**  Registration APIs
41
42<a name="table165976561598"></a>
43
44| API                                                          | Description              |
45| ------------------------------------------------------------ | ------------------------ |
46| function on(type: String, options: { slotId?: number }, callback: AsyncCallback\<T\>): void; | Registers an observer.   |
47| function off(type: String, callback?: AsyncCallback\<T\>): void; | Deregisters an observer. |
48
49## Usage Guidelines<a name="section163mcpsimp"></a>
50
51### Parameters of C APIs<a name="section1099113151207"></a>
52
53Different subscription events are distinguished by the  **type**  parameter. The following table lists the related  **type**  parameters.
54
55**Table  2**  Description of type parameters
56
57<a name="table1234838197"></a>
58
59| Parameter                         | Description                                                  | Required Permission              |
60| --------------------------------- | ------------------------------------------------------------ | -------------------------------- |
61| networkStateChange                | Network status change event                                  | ohos.permission.GET_NETWORK_INFO |
62| signalInfoChange                  | Signal change event                                          | None                             |
63| cellInfoChange                    | Cell information change event                                | ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION         |
64| cellularDataConnectionStateChange | Cellular data connection status change event                 | None                             |
65| cellularDataFlowChange            | Cellular data flow change event                              | None                             |
66| callStateChange                   | Call status change event, in which the value of **phoneNumber** is empty if the user does not have the required permission. | ohos.permission.READ_CALL_LOG    |
67
68### Sample Code<a name="section1558565082915"></a>
69
70The function of registering an observer for call status change events is used as an example. The process is as follows:
71
721.  Call the  **on**  method with the  **type**  parameter specified to register an observer for different types of events.
732.  Check whether the registration is successful. If  **err**  is empty in the received callback, the registration is successful. If  **err**  is not empty, the registration has failed. Obtain the required data from  **value**  if the registration is successful.
743.  Call the  **off**  method to deregister the observer. After the observer is deregistered, no callback will be received.
75
76    ```
77    // Import the observer package.
78    import observer from '@ohos.telephony.observer';
79
80    // Registers an observer.
81    observer.on('callStateChange', {slotId: 1}, (err, value) => {
82      if (err) {
83        // If the API call failed, err is not empty.
84        console.error(`failed, because ${err.message}`);
85        return;
86      }
87      // If the API call succeeded, err is empty.
88      console.log(`success on. number is ` + value.number + ", state is " + value.state);
89    });
90
91    // Deregister the observer.
92    observer.off('callStateChange', (err, value) => {
93      if (err) {
94        // If the API call failed, err is not empty.
95        console.error(`failed, because ${err.message}`);
96        return;
97      }
98      // If the API call succeeded, err is empty.
99      console.log(`success off`);
100    });
101    ```
102
103
104## Repositories Involved<a name="section206mcpsimp"></a>
105
106[Telephony](https://gitee.com/openharmony/docs/blob/master/en/readme/telephony.md)
107
108**telephony_state_registry**
109
110[telephony_core_service](https://gitee.com/openharmony/telephony_core_service/blob/master/README.md)
111
112[telephony_cellular_call](https://gitee.com/openharmony/telephony_cellular_call/blob/master/README.md)
113
114[telephony_call_manager](https://gitee.com/openharmony/telephony_call_manager/blob/master/README.md)
115

README_zh.md

1# 状态注册<a name="ZH-CN_TOPIC_0000001152064139"></a>
2
3-   [简介](#section117mcpsimp)
4-   [目录](#section124mcpsimp)
5-   [约束](#section128mcpsimp)
6-   [说明](#section134mcpsimp)
7    -   [接口说明](#section136mcpsimp)
8
9-   [使用说明](#section163mcpsimp)
10    -   [订阅接口参数说明](#section1099113151207)
11    -   [接口调用代码示例](#section1558565082915)
12
13-   [相关仓](#section206mcpsimp)
14
15## 简介<a name="section117mcpsimp"></a>
16
17状态注册主要负责提供电话服务子系统各种消息事件的订阅以及取消订阅的API。事件类型包括网络状态变化、信号强度变化、小区信息变化、蜂窝数据连接状态变化、通话状态变化等等。
18
19**图 1**  状态注册架构图<a name="fig13267152558"></a>
20![](figures/状态注册-架构图.png)
21
22## 目录<a name="section124mcpsimp"></a>
23
24```
25/base/telephony/state_registry      # 状态注册转发服务
26├─ figures                          # Readme资源文件
27├─ frameworks                       # 框架层目录
28│  ├─ js                            # js相关代码
29│  └─ native                        # native相关代码
30├─ interfaces                       # 接口目录
31│  ├─ innerkits                     # 部件间的内部接口
32│  └─ kits                          # 对应用提供的接口(例如JS接口)
33├─ sa_profile                       # 启动配置文件
34├─ service                          # 服务内部代码
35└─ test                             # 测试相关
36   ├─ mock                          # 模拟测试相关代码
37   └─ unittest                      # 单元测试相关代码
38```
39
40## 约束<a name="section128mcpsimp"></a>
41
42-   开发语言:JavaScript。
43-   软件约束:需要与以下服务配合使用:Telephony核心服务(core\_service)。
44-   硬件约束:需要搭载的设备支持以下硬件:可以进行独立蜂窝通信的Modem以及SIM卡。
45-   使用场景:注册获取SIM卡状态接口仅针对有SIM卡在位场景生效,若用户拔出SIM卡,则接收不到回调事件。应用可通过调用getSimState接口来确定当前卡槽是否有卡在位。
46
47## 说明<a name="section134mcpsimp"></a>
48
49### 接口说明<a name="section136mcpsimp"></a>
50
51**表 1**  注册接口
52
53<a name="table165976561598"></a>
54
55| 接口定义                                                     | **接口描述** |
56| ------------------------------------------------------------ | ------------ |
57| function on(type: String, options: { slotId?: number }, callback: AsyncCallback\<T\>): void; | 开启订阅     |
58| function off(type: String, callback?: AsyncCallback\<T\>): void; | 关闭订阅     |
59
60## 使用说明<a name="section163mcpsimp"></a>
61
62### 订阅接口参数说明<a name="section1099113151207"></a>
63
64不同订阅事件通过type进行区分,type列表如下:
65
66**表 2**  type参数说明
67
68<a name="table1234838197"></a>
69
70| type参数                          | 说明                                                  | 所需权限                         |
71| --------------------------------- | ----------------------------------------------------- | -------------------------------- |
72| networkStateChange                | 网络状态变化事件                                      | ohos.permission.GET_NETWORK_INFO |
73| signalInfoChange                  | 信号变化事件                                          | 无                               |
74| cellInfoChange                    | 小区信息变化事件                                      | ohos.permission.LOCATIONohos.permission.APPROXIMATELY_LOCATION         |
75| cellularDataConnectionStateChange | 蜂窝数据连接状态事件                                  | 无                               |
76| cellularDataFlowChange            | 蜂窝数据流变化事件                                    | 无                               |
77| callStateChange                   | 通话状态变化事件                                      | ohos.permission.READ_CALL_LOG    |
78| simStateChange                    | SIM卡状态变化事件                                    | ohos.permission.READ_SIM_STATE    |
79
80### 接口调用代码示例<a name="section1558565082915"></a>
81
82以订阅通话状态变化事件为例,主要步骤和代码如下:
83
841.  不同的事件指定不同的type,传入参数,调用on方法开启订阅。
852.  收到回调后,若err为空,则订阅成功,否则订阅失败。订阅成功则可以从value中获取数据。
863.  可以通过off方法取消订阅。取消订阅之后,不会再收到任何回调。
87
88    ```
89    // 引入包名
90    import observer from '@ohos.telephony.observer';
91
92    // 开启订阅
93    observer.on('callStateChange', {slotId: 1}, (err, value) => {
94      if (err) {
95        // 接口调用失败,err非空
96        console.error(`failed, because ${err.message}`);
97        return;
98      }
99      // 接口调用成功,err为空
100      console.log(`success on. number is ` + value.number + ", state is " + value.state);
101    });
102
103    // 关闭订阅
104    observer.off('callStateChange', (err, value) => {
105      if (err) {
106        // 接口调用失败,err非空
107        console.error(`failed, because ${err.message}`);
108        return;
109      }
110      // 接口调用成功,err为空
111      console.log(`success off`);
112    });
113    ```
114
115
116## 相关仓<a name="section206mcpsimp"></a>
117
118[电话服务子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E7%94%B5%E8%AF%9D%E6%9C%8D%E5%8A%A1%E5%AD%90%E7%B3%BB%E7%BB%9F.md)
119
120**telephony_state_registry**
121
122[telephony_core_service](https://gitee.com/openharmony/telephony_core_service/blob/master/README_zh.md)
123
124[telephony_cellular_call](https://gitee.com/openharmony/telephony_cellular_call/blob/master/README_zh.md)
125
126[telephony_call_manager](https://gitee.com/openharmony/telephony_call_manager/blob/master/README_zh.md)