openharmony 鸿蒙 js-apis-observer-sys

2025-06-12 浏览 (1)

@ohos.telephony.observer (Observer) (System API)

The observer module provides event subscription management functions. With the APIs provided by this module, you can register or unregister an observer that listens cell information events (for the SIM card in the specified slot).

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.

This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.telephony.observer (Observer).

Modules to Import

import { observer } from '@kit.TelephonyKit';

observer.on('cellInfoChange')8+

on(type: 'cellInfoChange', callback: Callback<Array<CellInformation>>): void

Registers an observer for cell information change events. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION

System capability: SystemCapability.Telephony.StateRegistry

Parameters

NameTypeMandatoryDescription
typestringYesCell information change event. This field has a fixed value of cellInfoChange.
callbackCallback<Array<CellInformation>>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Service connection failed.
8300003System internal error.
8300999Unknown error.

Example

import { radio } from '@kit.TelephonyKit';

observer.on('cellInfoChange', (data: Array<radio.CellInformation>) => {
    console.log("on cellInfoChange, data:" + JSON.stringify(data));
});

observer.on('cellInfoChange')8+

on(type: 'cellInfoChange', options: ObserverOptions, callback: Callback<Array<CellInformation>>): void

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 execution result.

System API: This is a system API.

Required permissions: ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION

System capability: SystemCapability.Telephony.StateRegistry

Parameters

NameTypeMandatoryDescription
typestringYesCell information change event. This field has a fixed value of cellInfoChange.
optionsObserverOptionsYesEvent subscription parameters.
callbackCallback<Array<CellInformation>>YesCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Service connection failed.
8300003System internal error.
8300999Unknown error.

Example

import { radio } from '@kit.TelephonyKit';

let options: observer.ObserverOptions = {
    slotId: 0
}
observer.on('cellInfoChange', options, (data: Array<radio.CellInformation>) => {
    console.log("on cellInfoChange, data:" + JSON.stringify(data));
});

observer.off('cellInfoChange')8+

off(type: 'cellInfoChange', callback?: Callback<Array<CellInformation>>): void

Unregisters the observer for cell information 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 API: This is a system API.

System capability: SystemCapability.Telephony.StateRegistry

Parameters

NameTypeMandatoryDescription
typestringYesCell information change event. This field has a fixed value of cellInfoChange.
callbackCallback<Array<CellInformation>>NoCallback used to return the result.

Error codes

For details about the error codes, see Universal Error Codes and Telephony Error Codes.

IDError Message
202Non-system applications use system APIs.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
8300001Invalid parameter value.
8300002Service connection failed.
8300003System internal error.
8300999Unknown error.

Example

import { radio } from '@kit.TelephonyKit';

let callback: (data: Array<radio.CellInformation>) => void = (data: Array<radio.CellInformation>) => {
    console.log("on cellInfoChange, data:" + JSON.stringify(data));
}
observer.on('cellInfoChange', 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('cellInfoChange', callback);
observer.off('cellInfoChange');

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Telephony Kit

harmony 鸿蒙Telephony_NetworkState

harmony 鸿蒙Telephony Error Codes

harmony 鸿蒙js-apis-call-sys

harmony 鸿蒙@ohos.telephony.call (Call)

harmony 鸿蒙@ohos.telephony.esim (eSIM Management) (System API)

harmony 鸿蒙@ohos.telephony.esim (eSIM Management)

harmony 鸿蒙@ohos.telephony.observer (Observer)

harmony 鸿蒙@ohos.telephony.radio (Radio) (System API)

harmony 鸿蒙@ohos.telephony.radio (Network Search)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/5lyKfV