• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Observer
2
3> **NOTE**<br>
4> 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.
5
6
7## Modules to Import
8
9```
10import observer from '@ohos.telephony.observer'
11```
12
13## observer.on('networkStateChange')
14
15on\(type: \'networkStateChange\', callback: Callback<NetworkState\>\): void;
16
17Registers an observer for network status change events. This API uses an asynchronous callback to return the execution result.
18
19**Required permission**: ohos.permission.GET_NETWORK_INFO
20
21**System capability**: SystemCapability.Telephony.StateRegistry
22
23**Parameters**
24
25| Name  | Type                                                     | Mandatory| Description                                                        |
26| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ |
27| type     | string                                                    | Yes  | Network status change event.                                            |
28| callback | Callback\<[NetworkState](js-apis-radio.md#networkstate)\> | Yes  | Callback used to return the result. For details, see [NetworkState](js-apis-radio.md#networkstate).|
29
30**Example**
31
32```
33observer.on('networkStateChange', data =>{
34    console.log("on networkStateChange, data:" + JSON.stringify(data));
35});
36```
37
38
39## observer.on('networkStateChange')
40
41on\(type: \'networkStateChange\', options: { slotId: number }, callback: Callback<NetworkState\>\): void;
42
43Registers an observer for network status change events of the SIM card in the specified slot. This API uses an asynchronous callback to return the execution result.
44
45**Required permission**: ohos.permission.GET_NETWORK_INFO
46
47**System capability**: SystemCapability.Telephony.StateRegistry
48
49**Parameters**
50
51| Name| Type  | Mandatory| Description                                  |
52| ------ | ------ | ---- | -------------------------------------- |
53| type     | string                                    | Yes  | Network status change event.                 |
54| slotId | number | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2|
55| callback | Callback\<[NetworkState](js-apis-radio.md#networkstate)\> | Yes  | Callback used to return the result. For details, see [NetworkState](js-apis-radio.md#networkstate).|
56
57**Example**
58
59```
60observer.on('networkStateChange', {slotId: 0}, data =>{
61    console.log("on networkStateChange, data:" + JSON.stringify(data));
62});
63```
64
65
66## observer.off('networkStateChange')
67
68off\(type: \'networkStateChange\', callback?: Callback<NetworkState\>\): void;
69
70Unregisters the observer for network status change events. This API uses an asynchronous callback to return the execution result.
71
72**Required permission**: ohos.permission.GET_NETWORK_INFO
73
74>**NOTE**
75>
76>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.
77
78**System capability**: SystemCapability.Telephony.StateRegistry
79
80**Parameters**
81
82| Name  | Type                                                     | Mandatory| Description                                                        |
83| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ |
84| type     | string                                                    | Yes  | Network status change event.                                            |
85| callback | Callback\<[NetworkState](js-apis-radio.md#networkstate)\> | No  | Callback used to return the result. For details, see [NetworkState](js-apis-radio.md#networkstate).|
86
87**Example**
88
89```
90let callback = data => {
91    console.log("on networkStateChange, data:" + JSON.stringify(data));
92}
93observer.on('networkStateChange', callback);
94// 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.
95observer.off('networkStateChange', callback);
96observer.off('networkStateChange');
97```
98
99## observer.on('signalInfoChange')
100
101on\(type: \'signalInfoChange\', callback: Callback<Array<SignalInformation\>\>): void;
102
103Registers an observer for signal status change events. This API uses an asynchronous callback to return the execution result.
104
105**System capability**: SystemCapability.Telephony.StateRegistry
106
107**Parameters**
108
109| Name  | Type                                                        | Mandatory| Description                                                        |
110| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
111| type     | string                                                       | Yes  | Signal status change event.                                            |
112| 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).|
113
114**Example**
115
116```
117observer.on('signalInfoChange', data =>{
118    console.log("on signalInfoChange, data:" + JSON.stringify(data));
119});
120```
121
122
123## observer.on('signalInfoChange')
124
125on\(type: \'signalInfoChange\', options: { slotId: number }, callback: Callback<Array<SignalInformation\>\>): void;
126
127Registers an observer for signal status change events of the SIM card in the specified slot. This API uses an asynchronous callback to return the execution result.
128
129**System capability**: SystemCapability.Telephony.StateRegistry
130
131**Parameters**
132
133| Name| Type  | Mandatory| Description                                  |
134| ------ | ------ | ---- | -------------------------------------- |
135| type     | string                                    | Yes  | Signal status change event.               |
136| slotId | number | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2|
137| 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).|
138
139**Example**
140
141```
142observer.on('signalInfoChange', {slotId: 0}, data =>{
143    console.log("on signalInfoChange, data:" + JSON.stringify(data));
144});
145```
146
147
148## observer.off('signalInfoChange')
149
150off\(type: \'signalInfoChange\', callback?: Callback<Array<SignalInformation\>\>): void;
151
152Unregisters the observer for signal status change events. This API uses an asynchronous callback to return the execution result.
153
154>**NOTE**
155>
156>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.
157
158**System capability**: SystemCapability.Telephony.StateRegistry
159
160**Parameters**
161
162| Name  | Type                                                        | Mandatory| Description                                                        |
163| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
164| type     | string                                                       | Yes  | Signal status change event.                                            |
165| 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).|
166
167**Example**
168
169```
170let callback = data => {
171    console.log("on signalInfoChange, data:" + JSON.stringify(data));
172}
173observer.on('signalInfoChange', callback);
174// 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.
175observer.off('signalInfoChange', callback);
176observer.off('signalInfoChange');
177```
178
179
180## observer.on('callStateChange')
181
182on(type: 'callStateChange', callback: Callback\<{ state: CallState, number: string }\>): void;
183
184Registers an observer for call status change events. This API uses an asynchronous callback to return the execution result.
185
186**Required permission**: ohos.permission.READ_CALL_LOG
187
188**System capability**: SystemCapability.Telephony.StateRegistry
189
190**Parameters**
191
192| Name  | Type                                                        | Mandatory| Description                                                        |
193| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
194| type     | string                                                       | Yes  | Call status change event.                                            |
195| 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.|
196
197**Example**
198
199```
200observer.on('callStateChange', value =>{
201    console.log("on callStateChange, state:" + value.state + ", number:" + value.number);
202});
203```
204
205
206## observer.on('callStateChange')
207
208on(type: 'callStateChange', options: { slotId: number }, callback: Callback<{ state:CallState, number: string }>): void;
209
210Registers an observer for call status change events. This API uses an asynchronous callback to return the execution result.
211
212**Required permission**: ohos.permission.READ_CALL_LOG
213
214**System capability**: SystemCapability.Telephony.StateRegistry
215
216**Parameters**
217
218| Name  | Type                                                        | Mandatory| Description                                                        |
219| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
220| type     | string                                                       | Yes  | Call status change event.                                            |
221| slotId   | number                                                       | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2                      |
222| 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.|
223
224**Example**
225
226```
227observer.on('callStateChange', {slotId: 0}, value =>{
228    console.log("on callStateChange, state:" + value.state + ", number:" + value.number);
229});
230```
231
232
233## observer.off('callStateChange')
234
235off(type: 'callStateChange', callback?: Callback<{ state: CallState, number: string }>): void;
236
237Unregisters the observer for call status change events. This API uses an asynchronous callback to return the execution result.
238
239**Required permission**: ohos.permission.READ_CALL_LOG
240
241>**NOTE**
242>
243>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.
244
245**System capability**: SystemCapability.Telephony.StateRegistry
246
247**Parameters**
248
249| Name  | Type                                                        | Mandatory| Description                                                        |
250| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
251| type     | string                                                       | Yes  | Call status change event.                                            |
252| 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.|
253
254**Example**
255
256```
257let callback = value => {
258    console.log("on callStateChange, state:" + value.state + ", number:" + value.number);
259}
260observer.on('callStateChange', callback);
261// 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.
262observer.off('callStateChange', callback);
263observer.off('callStateChange');
264```
265
266
267## observer.on('cellularDataConnectionStateChange')<sup>7+</sup>
268
269on\(type: 'cellularDataConnectionStateChange', callback: Callback\<{ state: DataConnectState, network: RatType}\>\): void;
270
271Registers an observer for connection status change events of the cellular data link. This API uses an asynchronous callback to return the result.
272
273**System capability**: SystemCapability.Telephony.StateRegistry
274
275**Parameters**
276
277| Name  | Type                                                        | Mandatory| Description                                                        |
278| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
279| type     | string                                                       | Yes  | Connection status change event of the cellular data link.                                    |
280| 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).|
281
282**Example**
283
284```
285observer.on('cellularDataConnectionStateChange', value =>{
286    console.log("on cellularDataConnectionStateChange, state:" + value.state + ", network:" + value.network);
287});
288```
289
290
291## observer.on('cellularDataConnectionStateChange')<sup>7+</sup>
292
293on\(type: 'cellularDataConnectionStateChange', options: { slotId: number }, callback: Callback\<{ state: DataConnectState, network: RatType }\>\): void;
294
295Registers an observer for connection status change events of the cellular data link over the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
296
297**System capability**: SystemCapability.Telephony.StateRegistry
298
299**Parameters**
300
301| Name  | Type                                                        | Mandatory| Description                                                        |
302| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
303| type     | string                                                       | Yes  | Connection status change event of the cellular data link.                                    |
304| slotId   | number                                                       | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2                      |
305| 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).|
306
307**Example**
308
309```
310observer.on('cellularDataConnectionStateChange', {slotId: 0}, value =>{
311    console.log("on cellularDataConnectionStateChange, state:" + value.state + ", network:" + value.network);
312});
313```
314
315
316## observer.off('cellularDataConnectionStateChange')<sup>7+</sup>
317
318off\(type: 'cellularDataConnectionStateChange',  callback?: Callback\<{ state: DataConnectState, network: RatType}\>\): void;
319
320Unregisters the observer for connection status change events of the cellular data link. This API uses an asynchronous callback to return the result.
321
322>**NOTE**
323>
324>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.
325
326**System capability**: SystemCapability.Telephony.StateRegistry
327
328**Parameters**
329
330| Name  | Type                                                        | Mandatory| Description                                                        |
331| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
332| type     | string                                                       | Yes  | Connection status change event of the cellular data link.                                    |
333| 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).|
334
335**Example**
336
337```
338let callback = value => {
339    console.log("on cellularDataConnectionStateChange, state:" + value.state + ", network:" + value.network);
340}
341observer.on('cellularDataConnectionStateChange', callback);
342// 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.
343observer.off('cellularDataConnectionStateChange', callback);
344observer.off('cellularDataConnectionStateChange');
345```
346
347
348## observer.on('cellularDataFlowChange')<sup>7+</sup>
349
350on\(type: 'cellularDataFlowChange', callback: Callback\<DataFlowType\>\): void;
351
352Registers 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.
353
354**System capability**: SystemCapability.Telephony.StateRegistry
355
356**Parameters**
357
358| Name  | Type                                                        | Mandatory| Description                                                        |
359| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
360| type     | string                                                       | Yes  | Uplink and downlink data flow status change event of the cellular data service.                      |
361| 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).|
362
363**Example**
364
365```
366observer.on('cellularDataFlowChange', data =>{
367    console.log("on networkStateChange, data:" + JSON.stringify(data));
368});
369```
370
371
372## observer.on('cellularDataFlowChange')<sup>7+</sup>
373
374on\(type: 'cellularDataFlowChange', options: { slotId: number },  callback: Callback\<DataFlowType\>\): void;
375
376Registers 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.
377
378**System capability**: SystemCapability.Telephony.StateRegistry
379
380**Parameters**
381
382| Name  | Type                                                        | Mandatory| Description                                                        |
383| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
384| type     | string                                                       | Yes  | Uplink and downlink data flow status change event of the cellular data service.                          |
385| slotId   | number                                                       | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2                      |
386| 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).|
387
388**Example**
389
390```
391observer.on('cellularDataFlowChange', {slotId: 0}, data =>{
392    console.log("on cellularDataFlowChange, data:" + JSON.stringify(data));
393});
394```
395
396
397## observer.off('cellularDataFlowChange')<sup>7+</sup>
398
399off\(type: 'cellularDataFlowChange', callback?: Callback\<DataFlowType\>\): void;
400
401Unregisters 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.
402
403>**NOTE**
404>
405>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.
406
407**System capability**: SystemCapability.Telephony.StateRegistry
408
409**Parameters**
410
411| Name  | Type                                                        | Mandatory| Description                                                        |
412| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
413| type     | string                                                       | Yes  | Uplink and downlink data flow status change event of the cellular data service.                          |
414| 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).|
415
416**Example**
417
418```
419let callback = data => {
420    console.log("on cellularDataFlowChange, data:" + JSON.stringify(data));
421}
422observer.on('cellularDataFlowChange', callback);
423// 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.
424observer.off('cellularDataFlowChange', callback);
425observer.off('cellularDataFlowChange');
426```
427
428
429## observer.on('simStateChange')<sup>7+</sup>
430
431on\(type: 'simStateChange', callback: Callback\<SimStateData\>\): void;
432
433Registers an observer for SIM card status change events. This API uses an asynchronous callback to return the result.
434
435**System capability**: SystemCapability.Telephony.StateRegistry
436
437**Parameters**
438
439| Name  | Type                                                        | Mandatory| Description                                                        |
440| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
441| type     | string                                                       | Yes  | SIM card status change event.                                    |
442| callback | Callback\<[SimStateData](#simstatedata7)\> | Yes  | Callback used to return the result.|
443
444**Example**
445
446```
447observer.on('simStateChange', data =>{
448    console.log("on simStateChange, data:" + JSON.stringify(data));
449});
450```
451
452
453## observer.on('simStateChange')<sup>7+</sup>
454
455on\(type: 'simStateChange', options: { slotId: number }, callback: Callback\<SimStateData\>\): void;
456
457Registers an observer for status change events of the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
458
459**System capability**: SystemCapability.Telephony.StateRegistry
460
461**Parameters**
462
463| Name  | Type                                                        | Mandatory| Description                                                        |
464| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
465| type     | string                                                       | Yes  | SIM card status change event.                                    |
466| slotId   | number                                                       | Yes  | Card slot ID. <br>- **0**: card slot 1<br>- **1**: card slot 2                      |
467| callback | Callback\<[SimStateData](#simstatedata7)\> | Yes  | Callback used to return the result.|
468
469**Example**
470
471```
472observer.on('simStateChange', {slotId: 0}, data =>{
473    console.log("on simStateChange, data:" + JSON.stringify(data));
474});
475```
476
477
478## observer.off('simStateChange')<sup>7+</sup>
479
480off\(type: 'simStateChange', callback?: Callback\<SimStateData\>\): void;
481
482Unregisters the observer for SIM card status change events. This API uses an asynchronous callback to return the result.
483
484>**NOTE**
485>
486>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.
487
488**System capability**: SystemCapability.Telephony.StateRegistry
489
490**Parameters**
491
492| Name  | Type                                                        | Mandatory| Description                                                        |
493| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
494| type     | string                                                       | Yes  | SIM card status change event.                                            |
495| callback | Callback\<[SimStateData](#simstatedata7)\> | No  | Callback used to return the result.|
496
497**Example**
498
499```
500let callback = data => {
501    console.log("on simStateChange, data:" + JSON.stringify(data));
502}
503observer.on('simStateChange', callback);
504// 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.
505observer.off('simStateChange', callback);
506observer.off('simStateChange');
507```
508
509
510## LockReason<sup>8+</sup>
511
512Enumerates SIM card lock types.
513
514**System capability**: SystemCapability.Telephony.StateRegistry
515
516| Name       | Value  | Description             |
517| ----------- | ---- | ----------------- |
518| SIM_NONE    | 0    | No lock.           |
519| SIM_PIN     | 1    | PIN lock.          |
520| SIM_PUK     | 2    | PUK lock.          |
521| SIM_PN_PIN  | 3    | Network PIN lock.      |
522| SIM_PN_PUK  | 4    | Network PUK lock.      |
523| SIM_PU_PIN  | 5    | Subnet PIN lock.      |
524| SIM_PU_PUK  | 6    | Subnet PUK lock.      |
525| SIM_PP_PIN  | 7    | Service provider PIN lock.|
526| SIM_PP_PUK  | 8    | Service provider PUK lock.|
527| SIM_PC_PIN  | 9    | Organization PIN lock.      |
528| SIM_PC_PUK  | 10   | Organization PUK lock.      |
529| SIM_SIM_PIN | 11   | SIM PIN lock.      |
530| SIM_SIM_PUK | 12   | SIM PUK lock.      |
531
532
533## SimStateData<sup>7+</sup>
534
535Enumerates SIM card types and states.
536
537**System capability**: SystemCapability.Telephony.StateRegistry
538
539| Name           | Type                 | Description                                                        |
540| ----------------- | --------------------- | ------------------------------------------------------------ |
541| type  | [CardType](js-apis-sim.md#cardtype) | SIM card type. For details, see [CardType](js-apis-sim.md#cardtype).|
542| state | [SimState](js-apis-sim.md#simstate) | SIM card status. For details, see [SimState](js-apis-sim.md#simstate).|
543| reason<sup>8+</sup>       | [LockReason](#lockreason8) | SIM card lock type.|
544