1e41f4b71Sopenharmony_ci# @ohos.telephony.observer (observer)(系统接口)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci本模块提供订阅管理功能,可以订阅/取消订阅的事件包括:小区信息变化事件、指定卡槽位的小区信息变化事件。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci>**说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci>本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci>当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.telephony.observer (observer)](js-apis-observer.md)
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## 导入模块
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci```ts
14e41f4b71Sopenharmony_ciimport { observer } from '@kit.TelephonyKit';
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## observer.on('cellInfoChange')<sup>8+</sup>
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_cion\(type: \'cellInfoChange\', callback: Callback\<Array\<CellInformation\>\>\): void
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci订阅小区信息变化事件,使用callback方式作为异步方法。
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**系统接口:** 此接口为系统接口。
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.LOCATIONohos.permission.APPROXIMATELY_LOCATION
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.StateRegistry
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci**参数:**
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci| 参数名   | 类型                                                      | 必填 | 说明                                      |
33e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------------- | ---- |------------------------------------------|
34e41f4b71Sopenharmony_ci| type     | string                                                    | 是   | 小区信息变化事件,固定为'cellInfoChange'。 |
35e41f4b71Sopenharmony_ci| callback | Callback\<Array\<[CellInformation](js-apis-radio.md#cellinformation8)\>\> | 是   | 以callback形式异步返回结果。                |
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**错误码:**
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci| 错误码ID |                  错误信息                    |
42e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- |
43e41f4b71Sopenharmony_ci| 201      | Permission denied.                           |
44e41f4b71Sopenharmony_ci| 202      | Non-system applications use system APIs.     |
45e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
46e41f4b71Sopenharmony_ci| 8300001  | Invalid parameter value.                     |
47e41f4b71Sopenharmony_ci| 8300002  | Service connection failed.                   |
48e41f4b71Sopenharmony_ci| 8300003  | System internal error.                       |
49e41f4b71Sopenharmony_ci| 8300999  | Unknown error.                               |
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci**示例:**
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci```ts
54e41f4b71Sopenharmony_ciimport { radio } from '@kit.TelephonyKit';
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ciobserver.on('cellInfoChange', (data: Array<radio.CellInformation>) => {
57e41f4b71Sopenharmony_ci    console.log("on cellInfoChange, data:" + JSON.stringify(data));
58e41f4b71Sopenharmony_ci});
59e41f4b71Sopenharmony_ci```
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci## observer.on('cellInfoChange')<sup>8+</sup>
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_cion\(type: \'cellInfoChange\', options: ObserverOptions, callback: Callback\<Array\<CellInformation\>\>\): void
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci订阅指定卡槽位的小区信息变化事件,使用callback方式作为异步方法。
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci**系统接口:** 此接口为系统接口。
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.LOCATIONohos.permission.APPROXIMATELY_LOCATION
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.StateRegistry
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci**参数:**
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci| 参数名 | 类型                                               | 必填 | 说明                                      |
77e41f4b71Sopenharmony_ci| ------ |--------------------------------------------------| ---- |--------------------------------------------|
78e41f4b71Sopenharmony_ci| type     | string                                           | 是   | 小区信息变化事件,固定为'cellInfoChange'。 |
79e41f4b71Sopenharmony_ci| options  | [ObserverOptions](js-apis-observer.md#observeroptions11)            | 是   | 电话相关事件订阅参数可选项。                |
80e41f4b71Sopenharmony_ci| callback | Callback\<Array\<[CellInformation](js-apis-radio.md#cellinformation8)\>\> | 是   | 以callback形式异步返回结果。       |
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci**错误码:**
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci| 错误码ID |                  错误信息                    |
87e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- |
88e41f4b71Sopenharmony_ci| 201      | Permission denied.                           |
89e41f4b71Sopenharmony_ci| 202      | Non-system applications use system APIs.     |
90e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
91e41f4b71Sopenharmony_ci| 8300001  | Invalid parameter value.                     |
92e41f4b71Sopenharmony_ci| 8300002  | Service connection failed.                   |
93e41f4b71Sopenharmony_ci| 8300003  | System internal error.                       |
94e41f4b71Sopenharmony_ci| 8300999  | Unknown error.                               |
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci**示例:**
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci```ts
99e41f4b71Sopenharmony_ciimport { radio } from '@kit.TelephonyKit';
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_cilet options: observer.ObserverOptions = {
102e41f4b71Sopenharmony_ci    slotId: 0
103e41f4b71Sopenharmony_ci}
104e41f4b71Sopenharmony_ciobserver.on('cellInfoChange', options, (data: Array<radio.CellInformation>) => {
105e41f4b71Sopenharmony_ci    console.log("on cellInfoChange, data:" + JSON.stringify(data));
106e41f4b71Sopenharmony_ci});
107e41f4b71Sopenharmony_ci```
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci## observer.off('cellInfoChange')<sup>8+</sup>
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_cioff\(type: \'cellInfoChange\', callback?: Callback\<Array\<CellInformation\>\>\): void
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci取消订阅小区信息变化事件,使用callback方式作为异步方法。
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci>**说明:**
117e41f4b71Sopenharmony_ci>
118e41f4b71Sopenharmony_ci>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci**系统接口:** 此接口为系统接口。
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Telephony.StateRegistry
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci**参数:**
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci| 参数名   | 类型                                                      | 必填 | 说明                                                         |
127e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ |
128e41f4b71Sopenharmony_ci| type     | string                                                    | 是   | 小区信息变化事件,固定为'cellInfoChange'。                                            |
129e41f4b71Sopenharmony_ci| callback | Callback\<Array\<[CellInformation](js-apis-radio.md#cellinformation8)\>\> | 否   | 以callback形式异步返回结果。|
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci**错误码:**
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[ohos.telephony(电话子系统)错误码](errorcode-telephony.md)。
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci| 错误码ID |                  错误信息                    |
136e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- |
137e41f4b71Sopenharmony_ci| 202      | Non-system applications use system APIs.     |
138e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.                             |
139e41f4b71Sopenharmony_ci| 8300001  | Invalid parameter value.                     |
140e41f4b71Sopenharmony_ci| 8300002  | Service connection failed.                   |
141e41f4b71Sopenharmony_ci| 8300003  | System internal error.                       |
142e41f4b71Sopenharmony_ci| 8300999  | Unknown error.                               |
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci**示例:**
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci```ts
147e41f4b71Sopenharmony_ciimport { radio } from '@kit.TelephonyKit';
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_cilet callback: (data: Array<radio.CellInformation>) => void = (data: Array<radio.CellInformation>) => {
150e41f4b71Sopenharmony_ci    console.log("on cellInfoChange, data:" + JSON.stringify(data));
151e41f4b71Sopenharmony_ci}
152e41f4b71Sopenharmony_ciobserver.on('cellInfoChange', callback);
153e41f4b71Sopenharmony_ci// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
154e41f4b71Sopenharmony_ciobserver.off('cellInfoChange', callback);
155e41f4b71Sopenharmony_ciobserver.off('cellInfoChange');
156e41f4b71Sopenharmony_ci```
157e41f4b71Sopenharmony_ci
158