openharmony 鸿蒙 js-apis-system-network

2025-06-12 浏览 (1)

@system.network (Network State)

NOTE

  • The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The APIs of this module are no longer maintained since API version 8. You are advised to use '@ohos.net.connection'.

Modules to Import

import network from '@system.network';

Required Permissions

ohos.permission.GET_WIFI_INFO

ohos.permission.GET_NETWORK_INFO

network.getType3+

getType(options?: {
  success?: (data: NetworkResponse) => void;
  fail?: (data: any, code: number) => void;
  complete?: () => void;
}): void

Obtains the network type of this device.

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
successFunctionNoCalled when the API call is successful. The return value is defined by NetworkResponse.
failFunctionNoCalled when an API call fails.
completeFunctionNoCalled when an API call is complete.

Error codes:

Error CodeDescription
602The current permission is not declared.

Example

export default class Network {
  getType() {
    network.getType({
      success: (data) => {
        console.log('success get network type:' + data.type);
      }
    });
  }
}

network.subscribe3+

subscribe(options?:{
  success?: (data: NetworkResponse) => void;
  fail?: (data: any, code: number) => void;
}): void

Listens to the network connection state of this device. If this API is called multiple times, the last call takes effect.

System capability: SystemCapability.Communication.NetManager.Core

Parameters

NameTypeMandatoryDescription
successFunctionNoCalled when the network state changes. The return value is defined by NetworkResponse.
failFunctionNoCalled when an API call fails.

Error codes:

Error CodeDescription
602The current permission is not declared.
200Subscription failed.

Example

export default class Network {
  subscribe() {
    network.subscribe({
      success: (data) => {
        console.log('success get network type:' + data.type);
      }
    });
  }
}

network.unsubscribe3+

unsubscribe(): void

Cancels listening to the network connection state of this device.

System capability: SystemCapability.Communication.NetManager.Core

Example

import network from '@system.network';

network.unsubscribe();

NetworkResponse3+

System capability: SystemCapability.Communication.NetManager.Core

NameTypeMandatoryDescription
meteredbooleanNoWhether to charge by traffic.
typestringYesNetwork type. The value can be 2G, 3G, 4G, 5G, WiFi, or none.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Network Kit

harmony 鸿蒙NetConn_ConnectionProperties

harmony 鸿蒙NetConn_HttpProxy

harmony 鸿蒙NetConn_NetAddr

harmony 鸿蒙NetConn_NetCapabilities

harmony 鸿蒙NetConn_NetConnCallback

harmony 鸿蒙NetConn_NetHandle

harmony 鸿蒙NetConn_NetHandleList

harmony 鸿蒙NetConn_NetSpecifier

harmony 鸿蒙NetConn_Route

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