Hongmeng Network Status Monitoring [Nut Pie]

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.observeradded support for network status monitoring in the module, and also @ohos.telephony.radioadded 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

Guess you like

Origin blog.csdn.net/qq_39132095/article/details/132794300