The 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.
Figure 1 Architecture of the state registry module
/base/telephony/state_registry # State registry service
├─ figures # Figures of readme files
├─ frameworks # Framework layer
│ ├─ js # JS code
│ └─ native # Native code
├─ interfaces # APIs
│ ├─ innerkits # Internal APIs
│ └─ kits # External APIs \(such as JS APIs\)
├─ sa_profile # SA profile
├─ services # Service code
└─ test # Test code
├─ mock # Simulation test
└─ unittest # Unit test
Table 1 Registration APIs
API | Description |
---|---|
function on(type: String, options: { slotId?: number }, callback: AsyncCallback<T>): void; | Registers an observer. |
function off(type: String, callback?: AsyncCallback<T>): void; | Deregisters an observer. |
Different subscription events are distinguished by the type parameter. The following table lists the related type parameters.
Table 2 Description of type parameters
Parameter | Description | Required Permission |
---|---|---|
networkStateChange | Network status change event | ohos.permission.GET_NETWORK_INFO |
signalInfoChange | Signal change event | None |
cellInfoChange | Cell information change event | ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION |
cellularDataConnectionStateChange | Cellular data connection status change event | None |
cellularDataFlowChange | Cellular data flow change event | None |
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 |
simStateChange | SIM card status change event | None |
The function of registering an observer for call status change events is used as an example. The process is as follows:
Call the on method with the type parameter specified to register an observer for different types of events.
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.
Call the off method to deregister the observer. After the observer is deregistered, no callback will be received.
// Import the observer package.
import observer from '@ohos.telephony.observer';
// Registers an observer.
observer.on('callStateChange', {slotId: 1}, (err, value) => {
if (err) {
// If the API call failed, err is not empty.
console.error(`failed, because ${err.message}`);
return;
}
// If the API call succeeded, err is empty.
console.log(`success on. number is ` + value.number + ", state is " + value.state);
});
// Deregister the observer.
observer.off('callStateChange', (err, value) => {
if (err) {
// If the API call failed, err is not empty.
console.error(`failed, because ${err.message}`);
return;
}
// If the API call succeeded, err is empty.
console.log(`success off`);
});
telephony_state_registry
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
Activity
Community
Health
Trend
Influence
:Code submit frequency
:React/respond to issue & PR etc.
:Well-balanced team members and collaboration
:Recent popularity of project
:Star counts, download counts etc.