js-apis-observer.md 26.5 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.telephony.observer (Observer)
S
shawn_he 已提交
2

S
shawn_he 已提交
3
The **observer** module provides event subscription management functions. You can register or unregister an observer that listens for the following events: network status change, signal status change, call status change, cellular data connection status, uplink and downlink data flow status of cellular data services, and SIM status change.
S
shawn_he 已提交
4

S
shawn_he 已提交
5 6 7 8 9 10 11
>**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.


## Modules to Import

S
shawn_he 已提交
12
```js
S
shawn_he 已提交
13 14 15
import observer from '@ohos.telephony.observer'
```

S
shawn_he 已提交
16
## observer.on('networkStateChange')
S
shawn_he 已提交
17 18 19

on\(type: \'networkStateChange\', callback: Callback<NetworkState\>\): void;

S
shawn_he 已提交
20
Registers an observer for network status change events. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
21 22 23 24 25 26 27

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
28 29 30 31
| Name  | Type                                                     | Mandatory| Description                                                        |
| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                                                    | Yes  | Network status change event.                                            |
| callback | Callback\<[NetworkState](js-apis-radio.md#networkstate)\> | Yes  | Callback used to return the result. For details, see [NetworkState](js-apis-radio.md#networkstate).|
S
shawn_he 已提交
32 33 34

**Example**

S
shawn_he 已提交
35
```js
S
shawn_he 已提交
36 37 38 39 40 41
observer.on('networkStateChange', data =>{ 
    console.log("on networkStateChange, data:" + JSON.stringify(data));
});
```


S
shawn_he 已提交
42
## observer.on('networkStateChange')
S
shawn_he 已提交
43 44 45

on\(type: \'networkStateChange\', options: { slotId: number }, callback: Callback<NetworkState\>\): void;

S
shawn_he 已提交
46
Registers an observer for network status change events of the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
47 48 49 50 51 52 53

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
54
| Name| Type  | Mandatory| Description                                  |
S
shawn_he 已提交
55
| ------ | ------ | ---- | -------------------------------------- |
S
shawn_he 已提交
56 57 58
| type     | string                                    | Yes  | Network status change event.                 |
| slotId | number | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2|
| callback | Callback\<[NetworkState](js-apis-radio.md#networkstate)\> | Yes  | Callback used to return the result. For details, see [NetworkState](js-apis-radio.md#networkstate).|
S
shawn_he 已提交
59 60 61

**Example**

S
shawn_he 已提交
62
```js
S
shawn_he 已提交
63 64 65 66 67 68
observer.on('networkStateChange', {slotId: 0}, data =>{ 
    console.log("on networkStateChange, data:" + JSON.stringify(data));
});
```


S
shawn_he 已提交
69
## observer.off('networkStateChange')
S
shawn_he 已提交
70 71 72

off\(type: \'networkStateChange\', callback?: Callback<NetworkState\>\): void;

S
shawn_he 已提交
73
Unregisters the observer for network status change events. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
74 75 76 77 78 79 80 81 82

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
83 84 85 86
| Name  | Type                                                     | Mandatory| Description                                                        |
| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ |
| type     | string                                                    | Yes  | Network status change event.                                            |
| callback | Callback\<[NetworkState](js-apis-radio.md#networkstate)\> | No  | Callback used to return the result. For details, see [NetworkState](js-apis-radio.md#networkstate).|
S
shawn_he 已提交
87 88 89

**Example**

S
shawn_he 已提交
90
```js
S
shawn_he 已提交
91 92 93 94 95 96 97 98 99
let callback = data => {
    console.log("on networkStateChange, data:" + JSON.stringify(data));
}
observer.on('networkStateChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('networkStateChange', callback);
observer.off('networkStateChange');
```

S
shawn_he 已提交
100
## observer.on('signalInfoChange')
S
shawn_he 已提交
101 102 103

on\(type: \'signalInfoChange\', callback: Callback<Array<SignalInformation\>\>): void;

S
shawn_he 已提交
104
Registers an observer for signal status change events. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
105 106 107 108 109

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
110
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
111
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
112 113
| type     | string                                                       | Yes  | Signal status change event.                                            |
| callback | Callback<Array<[SignalInformation](js-apis-radio.md#signalinformation)\>\> | Yes  | Callback used to return the result. For details, see [SignalInformation](js-apis-radio.md#signalinformation).|
S
shawn_he 已提交
114 115 116

**Example**

S
shawn_he 已提交
117
```js
S
shawn_he 已提交
118 119 120 121 122 123
observer.on('signalInfoChange', data =>{ 
    console.log("on signalInfoChange, data:" + JSON.stringify(data));
});
```


S
shawn_he 已提交
124
## observer.on('signalInfoChange')
S
shawn_he 已提交
125 126 127

on\(type: \'signalInfoChange\', options: { slotId: number }, callback: Callback<Array<SignalInformation\>\>): void;

S
shawn_he 已提交
128
Registers an observer for signal status change events of the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
129 130 131 132 133

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
134
| Name| Type  | Mandatory| Description                                  |
S
shawn_he 已提交
135
| ------ | ------ | ---- | -------------------------------------- |
S
shawn_he 已提交
136 137 138
| type     | string                                    | Yes  | Signal status change event.               |
| slotId | number | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2|
| callback | Callback<Array<[SignalInformation](js-apis-radio.md#signalinformation)\>\> | Yes  | Callback used to return the result. For details, see [SignalInformation](js-apis-radio.md#signalinformation).|
S
shawn_he 已提交
139 140 141

**Example**

S
shawn_he 已提交
142
```js
S
shawn_he 已提交
143 144 145 146 147 148
observer.on('signalInfoChange', {slotId: 0}, data =>{ 
    console.log("on signalInfoChange, data:" + JSON.stringify(data));
});
```


S
shawn_he 已提交
149
## observer.off('signalInfoChange')
S
shawn_he 已提交
150 151 152

off\(type: \'signalInfoChange\', callback?: Callback<Array<SignalInformation\>\>): void;

S
shawn_he 已提交
153
Unregisters the observer for signal status change events. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
154 155 156 157 158 159 160 161 162

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
163
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
164
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
165 166
| type     | string                                                       | Yes  | Signal status change event.                                            |
| callback | Callback<Array<[SignalInformation](js-apis-radio.md#signalinformation)\>\> | No  | Callback used to return the result. For details, see [SignalInformation](js-apis-radio.md#signalinformation).|
S
shawn_he 已提交
167 168 169

**Example**

S
shawn_he 已提交
170
```js
S
shawn_he 已提交
171 172 173 174 175 176 177 178 179 180
let callback = data => {
    console.log("on signalInfoChange, data:" + JSON.stringify(data));
}
observer.on('signalInfoChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('signalInfoChange', callback);
observer.off('signalInfoChange');
```


S
shawn_he 已提交
181
## observer.on('callStateChange')
S
shawn_he 已提交
182

S
shawn_he 已提交
183
on(type: 'callStateChange', callback: Callback\<{ state: CallState, number: string }\>): void;
S
shawn_he 已提交
184

S
shawn_he 已提交
185
Registers an observer for call status change events. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
186 187 188 189 190

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
191
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
192
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
193 194
| type     | string                                                       | Yes  | Call status change event.                                            |
| callback | Callback\<{ state: [CallState](js-apis-call.md#callstate), number: string }\> | Yes  | Callback function. For details, see [CallState](js-apis-call.md#callstate) in call.<br>**number**: phone number.|
S
shawn_he 已提交
195 196 197

**Example**

S
shawn_he 已提交
198
```js
S
shawn_he 已提交
199 200 201 202 203 204
observer.on('callStateChange', value =>{ 
    console.log("on callStateChange, state:" + value.state + ", number:" + value.number);
});
```


S
shawn_he 已提交
205
## observer.on('callStateChange')
S
shawn_he 已提交
206

S
shawn_he 已提交
207
on(type: 'callStateChange', options: { slotId: number }, callback: Callback<{ state:CallState, number: string }>): void;
S
shawn_he 已提交
208

S
shawn_he 已提交
209
Registers an observer for call status change events. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
210 211 212 213 214

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
215
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
216
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
217 218 219
| type     | string                                                       | Yes  | Call status change event.                                            |
| slotId   | number                                                       | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2                      |
| callback | Callback\<{ state: [CallState](js-apis-call.md#callstate), number: string }\> | Yes  | Callback function. For details, see [CallState](js-apis-call.md#callstate) in call.<br>**number**: phone number.|
S
shawn_he 已提交
220 221 222

**Example**

S
shawn_he 已提交
223
```js
S
shawn_he 已提交
224 225 226 227 228 229
observer.on('callStateChange', {slotId: 0}, value =>{ 
    console.log("on callStateChange, state:" + value.state + ", number:" + value.number);
});
```


S
shawn_he 已提交
230
## observer.off('callStateChange')
S
shawn_he 已提交
231

S
shawn_he 已提交
232
off(type: 'callStateChange', callback?: Callback<{ state: CallState, number: string }>): void;
S
shawn_he 已提交
233

S
shawn_he 已提交
234
Unregisters the observer for call status change events. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
235 236 237 238 239 240 241 242 243

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

S
shawn_he 已提交
244
| Name  | Type                                                        | Mandatory| Description                                                        |
S
shawn_he 已提交
245
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
246 247
| type     | string                                                       | Yes  | Call status change event.                                            |
| callback | Callback\<{ state: [CallState](js-apis-call.md#callstate), number: string }\> | No  | Callback function. For details, see [CallState](js-apis-call.md#callstate) in call.<br>**number**: phone number.|
S
shawn_he 已提交
248 249 250

**Example**

S
shawn_he 已提交
251
```js
S
shawn_he 已提交
252 253 254 255 256 257 258 259
let callback = value => {
    console.log("on callStateChange, state:" + value.state + ", number:" + value.number);
}
observer.on('callStateChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('callStateChange', callback);
observer.off('callStateChange');
```
S
shawn_he 已提交
260 261 262 263 264 265


## observer.on('cellularDataConnectionStateChange')<sup>7+</sup>

on\(type: 'cellularDataConnectionStateChange', callback: Callback\<{ state: DataConnectState, network: RatType}\>\): void;

S
shawn_he 已提交
266
Registers an observer for connection status change events of the cellular data connection.This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
267 268 269 270 271 272 273

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
274
| type     | string                                                       | Yes  | Connection status change event of the cellular data connection.                                    |
S
shawn_he 已提交
275
| callback | Callback\<{ state: [DataConnectState](js-apis-telephony-data.md#dataconnectstate), network: [RatType](js-apis-radio.md#radiotechnology) }\> | Yes  | Callback used to return the result. For details, see [DataConnectState](js-apis-telephony-data.md#dataconnectstate) and [RadioTechnology](js-apis-radio.md#radiotechnology).|
S
shawn_he 已提交
276 277 278

**Example**

S
shawn_he 已提交
279
```js
S
shawn_he 已提交
280 281 282 283 284 285 286 287 288 289
observer.on('cellularDataConnectionStateChange', value =>{
    console.log("on cellularDataConnectionStateChange, state:" + value.state + ", network:" + value.network);
});
```


## observer.on('cellularDataConnectionStateChange')<sup>7+</sup>

on\(type: 'cellularDataConnectionStateChange', options: { slotId: number }, callback: Callback\<{ state: DataConnectState, network: RatType }\>\): void;

S
shawn_he 已提交
290
Registers an observer for connection status change events of the cellular data connection over the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
291 292 293 294 295 296 297

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
298
| type     | string                                                       | Yes  | Connection status change event of the cellular data connection.                                   |
S
shawn_he 已提交
299
| slotId   | number                                                       | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2                      |
S
shawn_he 已提交
300
| callback | Callback\<{ state: [DataConnectState](js-apis-telephony-data.md#dataconnectstate), network: [RatType](js-apis-radio.md#radiotechnology) }\> | Yes  | Callback used to return the result. For details, see [DataConnectState](js-apis-telephony-data.md#dataconnectstate) and [RadioTechnology](js-apis-radio.md#radiotechnology).|
S
shawn_he 已提交
301 302 303

**Example**

S
shawn_he 已提交
304
```js
S
shawn_he 已提交
305 306 307 308 309 310 311 312 313 314
observer.on('cellularDataConnectionStateChange', {slotId: 0}, value =>{
    console.log("on cellularDataConnectionStateChange, state:" + value.state + ", network:" + value.network);
});
```


## observer.off('cellularDataConnectionStateChange')<sup>7+</sup>

off\(type: 'cellularDataConnectionStateChange',  callback?: Callback\<{ state: DataConnectState, network: RatType}\>\): void;

S
shawn_he 已提交
315
Unregisters the observer for connection status change events of the cellular data connection.This API uses an asynchronous callback to return the result.
S
shawn_he 已提交
316 317 318 319 320 321 322 323 324 325 326

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
327
| type     | string                                                       | Yes  | Connection status change event of the cellular data connection.                                   |
S
shawn_he 已提交
328
| callback | Callback\<{ state: [DataConnectState](js-apis-telephony-data.md#dataconnectstate), network: [RatType](js-apis-radio.md#radiotechnology) }\> | No  | Callback used to return the result. For details, see [DataConnectState](js-apis-telephony-data.md#dataconnectstate) and [RadioTechnology](js-apis-radio.md#radiotechnology).|
S
shawn_he 已提交
329 330 331

**Example**

S
shawn_he 已提交
332
```js
S
shawn_he 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
let callback = value => {
    console.log("on cellularDataConnectionStateChange, state:" + value.state + ", network:" + value.network);
}
observer.on('cellularDataConnectionStateChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('cellularDataConnectionStateChange', callback);
observer.off('cellularDataConnectionStateChange');
```


## observer.on('cellularDataFlowChange')<sup>7+</sup>

on\(type: 'cellularDataFlowChange', callback: Callback\<DataFlowType\>\): void;

Registers an observer for the uplink and downlink data flow status change events of the cellular data service. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | Uplink and downlink data flow status change event of the cellular data service.                      |
| callback | Callback\<[DataFlowType](js-apis-telephony-data.md#dataflowtype)\> | Yes  | Callback used to return the result. For details, see [DataFlowType](js-apis-telephony-data.md#dataflowtype).|

**Example**

S
shawn_he 已提交
360
```js
S
shawn_he 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
observer.on('cellularDataFlowChange', data =>{
    console.log("on networkStateChange, data:" + JSON.stringify(data));
});
```


## observer.on('cellularDataFlowChange')<sup>7+</sup>

on\(type: 'cellularDataFlowChange', options: { slotId: number },  callback: Callback\<DataFlowType\>\): void;

Registers an observer for the uplink and downlink data flow status change events of the cellular data service on the SIM card in the specified slot. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | Uplink and downlink data flow status change event of the cellular data service.                          |
| slotId   | number                                                       | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2                      |
| callback | Callback\<[DataFlowType](js-apis-telephony-data.md#dataflowtype)\> | Yes  | Callback used to return the result. For details, see [DataFlowType](js-apis-telephony-data.md#dataflowtype).|

**Example**

S
shawn_he 已提交
385
```js
S
shawn_he 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
observer.on('cellularDataFlowChange', {slotId: 0}, data =>{
    console.log("on cellularDataFlowChange, data:" + JSON.stringify(data));
});
```


## observer.off('cellularDataFlowChange')<sup>7+</sup>

off\(type: 'cellularDataFlowChange', callback?: Callback\<DataFlowType\>\): void;

Unregisters the observer for the uplink and downlink data flow status change events of the cellular data service. This API uses an asynchronous callback to return the result.

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | Uplink and downlink data flow status change event of the cellular data service.                          |
| callback | Callback\<[DataFlowType](js-apis-telephony-data.md#dataflowtype)\> | No  | Callback used to return the result. For details, see [DataFlowType](js-apis-telephony-data.md#dataflowtype).|

**Example**

S
shawn_he 已提交
413
```js
S
shawn_he 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
let callback = data => {
    console.log("on cellularDataFlowChange, data:" + JSON.stringify(data));
}
observer.on('cellularDataFlowChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('cellularDataFlowChange', callback);
observer.off('cellularDataFlowChange');
```


## observer.on('simStateChange')<sup>7+</sup>

on\(type: 'simStateChange', callback: Callback\<SimStateData\>\): void;

Registers an observer for SIM card status change events. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | SIM card status change event.                                    |
| callback | Callback\<[SimStateData](#simstatedata7)\> | Yes  | Callback used to return the result.|

**Example**

S
shawn_he 已提交
441
```js
S
shawn_he 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
observer.on('simStateChange', data =>{
    console.log("on simStateChange, data:" + JSON.stringify(data));
});
```


## observer.on('simStateChange')<sup>7+</sup>

on\(type: 'simStateChange', options: { slotId: number }, callback: Callback\<SimStateData\>\): void;

Registers an observer for status change events of the SIM card in the specified slot. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | SIM card status change event.                                    |
| slotId   | number                                                       | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2                      |
| callback | Callback\<[SimStateData](#simstatedata7)\> | Yes  | Callback used to return the result.|

**Example**

S
shawn_he 已提交
466
```js
S
shawn_he 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
observer.on('simStateChange', {slotId: 0}, data =>{
    console.log("on simStateChange, data:" + JSON.stringify(data));
});
```


## observer.off('simStateChange')<sup>7+</sup>

off\(type: 'simStateChange', callback?: Callback\<SimStateData\>\): void;

Unregisters the observer for SIM card status change events. This API uses an asynchronous callback to return the result.

>**NOTE**
>
>You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.

**System capability**: SystemCapability.Telephony.StateRegistry

**Parameters**

| Name  | Type                                                        | Mandatory| Description                                                        |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| type     | string                                                       | Yes  | SIM card status change event.                                            |
| callback | Callback\<[SimStateData](#simstatedata7)\> | No  | Callback used to return the result.|

**Example**

S
shawn_he 已提交
494
```js
S
shawn_he 已提交
495 496 497 498 499 500 501 502 503 504 505 506
let callback = data => {
    console.log("on simStateChange, data:" + JSON.stringify(data));
}
observer.on('simStateChange', callback);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
observer.off('simStateChange', callback);
observer.off('simStateChange');
```


## LockReason<sup>8+</sup>

S
shawn_he 已提交
507
Enumerates SIM card lock types.
S
shawn_he 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

**System capability**: SystemCapability.Telephony.StateRegistry

| Name       | Value  | Description             |
| ----------- | ---- | ----------------- |
| SIM_NONE    | 0    | No lock.           |
| SIM_PIN     | 1    | PIN lock.          |
| SIM_PUK     | 2    | PUK lock.          |
| SIM_PN_PIN  | 3    | Network PIN lock.      |
| SIM_PN_PUK  | 4    | Network PUK lock.      |
| SIM_PU_PIN  | 5    | Subnet PIN lock.      |
| SIM_PU_PUK  | 6    | Subnet PUK lock.      |
| SIM_PP_PIN  | 7    | Service provider PIN lock.|
| SIM_PP_PUK  | 8    | Service provider PUK lock.|
| SIM_PC_PIN  | 9    | Organization PIN lock.      |
| SIM_PC_PUK  | 10   | Organization PUK lock.      |
| SIM_SIM_PIN | 11   | SIM PIN lock.      |
| SIM_SIM_PUK | 12   | SIM PUK lock.      |


## SimStateData<sup>7+</sup>

Enumerates SIM card types and states.

**System capability**: SystemCapability.Telephony.StateRegistry

S
shawn_he 已提交
534 535 536 537 538
| Name     | Type    | Mandatory | Description |
| -------- | ------- | --------- | ----------- |
| type  | [CardType](js-apis-sim.md#cardtype) | Yes| SIM card type. For details, see [CardType](js-apis-sim.md#cardtype).|
| state | [SimState](js-apis-sim.md#simstate) | Yes| SIM card status. For details, see [SimState](js-apis-sim.md#simstate).|
| reason<sup>8+</sup>       | [LockReason](#lockreason8) | Yes| SIM card lock type.|