• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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├─ services                         # 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| simStateChange                    | SIM card status change event                                 | None                             |
68
69### Sample Code<a name="section1558565082915"></a>
70
71The function of registering an observer for call status change events is used as an example. The process is as follows:
72
731.  Call the  **on**  method with the  **type**  parameter specified to register an observer for different types of events.
742.  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.
753.  Call the  **off**  method to deregister the observer. After the observer is deregistered, no callback will be received.
76
77    ```
78    // Import the observer package.
79    import observer from '@ohos.telephony.observer';
80
81    // Registers an observer.
82    observer.on('callStateChange', {slotId: 1}, (err, value) => {
83      if (err) {
84        // If the API call failed, err is not empty.
85        console.error(`failed, because ${err.message}`);
86        return;
87      }
88      // If the API call succeeded, err is empty.
89      console.log(`success on. number is ` + value.number + ", state is " + value.state);
90    });
91
92    // Deregister the observer.
93    observer.off('callStateChange', (err, value) => {
94      if (err) {
95        // If the API call failed, err is not empty.
96        console.error(`failed, because ${err.message}`);
97        return;
98      }
99      // If the API call succeeded, err is empty.
100      console.log(`success off`);
101    });
102    ```
103
104
105## Repositories Involved<a name="section206mcpsimp"></a>
106
107[Telephony](https://gitee.com/openharmony/docs/blob/master/en/readme/telephony.md)
108
109**telephony_state_registry**
110
111[telephony_core_service](https://gitee.com/openharmony/telephony_core_service/blob/master/README.md)
112
113[telephony_cellular_data](https://gitee.com/openharmony/telephony_cellular_data/blob/master/README.md)
114
115[telephony_cellular_call](https://gitee.com/openharmony/telephony_cellular_call/blob/master/README.md)
116
117[telephony_call_manager](https://gitee.com/openharmony/telephony_call_manager/blob/master/README.md)
118