Network status monitoring
In application development, there will be situations where network status is monitored. For example, when watching short videos, if the device network switches from WiFi to mobile network, user-friendly prompts should be given at this time, such as paying attention to data consumption, etc. In response to this scenario, the ArkUI open framework @ohos.telephony.observer
added support for network status monitoring in the module, and also @ohos.telephony.radio
added getNetworkState()
a method in the module to directly obtain the current network status.
constraint
To monitor network status, you need to apply for the ohos.permission.GET_NETWORK_INFO permission.
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
},
{
"name": "ohos.permission.GET_NETWORK_INFO"
}
],
Complete source code
import observer from '@ohos.telephony.observer';
// 引入库
import