openharmony 鸿蒙 js-apis-net-statistics-sys

2025-06-16 浏览 (1)

@ohos.net.statistics (流量管理)(系统接口)

流量管理模块,支持基于网卡/UID 的实时流量统计和历史流量统计查询能力。

说明: 本模块首批接口从 API version 10 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 当前页面仅包含本模块的系统接口,其他公开接口参见@ohos.net.statistics (流量管理)

导入模块

import { statistics } from '@kit.NetworkKit';

statistics.on('netStatsChange')10+

on(type: 'netStatsChange', callback: Callback<NetStatsChangeInfo>): void

订阅流量改变事件通知。

系统接口:此接口为系统接口。

需要权限:ohos.permission.GET_NETWORK_STATS

系统能力:SystemCapability.Communication.NetManager.Core

参数:

参数名类型必填说明
typestring订阅事件,固定为'netStatsChange'。
callbackCallback<NetStatsChangeInfo>当流量有改变时触发回调函数。

错误码:

以下错误码的详细介绍参见statistics 错误码

错误码 ID错误信息
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100002Failed to connect to the service.
2100003System internal error.

示例:

import { statistics } from '@kit.NetworkKit';

class IFace {
  iface: string = ""
  uid?: number = 0
}
statistics.on('netStatsChange', (data: IFace) => {
  console.log('on netStatsChange' + JSON.stringify(data));
});

statistics.off('netStatsChange')10+

off(type: 'netStatsChange', callback?: Callback<NetStatsChangeInfo>): void

取消订阅流量改变事件通知。

系统接口:此接口为系统接口。

需要权限:ohos.permission.GET_NETWORK_STATS

系统能力:SystemCapability.Communication.NetManager.Core

参数:

参数名类型必填说明
typestring注销订阅事件,固定为'netStatsChange'。
callbackCallback<NetStatsChangeInfo>当流量有改变时触发回调函数。

错误码:

以下错误码的详细介绍参见statistics 错误码

错误码 ID错误信息
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100002Failed to connect to the service.
2100003System internal error.

示例:

import { statistics } from '@kit.NetworkKit';

class IFace {
  iface: string = ""
  uid?: number = 0
}
let callback: (data: IFace) => void = (data: IFace) => {
    console.log("on netStatsChange, iFace:" + data.iface + " uid: " + data.uid);
}
statistics.on('netStatsChange', callback);
// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
statistics.off('netStatsChange', callback);
statistics.off('netStatsChange');

statistics.getTrafficStatsByIface10+

getTrafficStatsByIface(ifaceInfo: IfaceInfo, callback: AsyncCallback<NetStatsInfo>): void

获取指定网卡历史流量信息,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.GET_NETWORK_STATS

系统能力:SystemCapability.Communication.NetManager.Core

参数:

参数名类型必填说明
ifaceInfoIfaceInfo指定查询的网卡信息,参见IfaceInfo
callbackAsyncCallback<NetStatsInfo>回调函数。成功时 statsInfo 返回包含网卡历史流量信息,error 为 undefined,否则为错误对象。

错误码:

以下错误码的详细介绍参见statistics 错误码

错误码 ID错误信息
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Failed to connect to the service.
2100003System internal error.
2103017Failed to read the database.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
import { statistics } from '@kit.NetworkKit';

let iFaceInfo: statistics.IfaceInfo|null = null;
if (iFaceInfo) {
  statistics.getTrafficStatsByIface(iFaceInfo as statistics.IfaceInfo, (error: BusinessError, statsInfo: statistics.NetStatsInfo) => {
    console.error(JSON.stringify(error));
    console.log(
      "getTrafficStatsByIface bytes of received = " +
      JSON.stringify(statsInfo.rxBytes)
    );
    console.log(
      "getTrafficStatsByIface bytes of sent = " +
      JSON.stringify(statsInfo.txBytes)
    );
    console.log(
      "getTrafficStatsByIface packets of received = " +
      JSON.stringify(statsInfo.rxPackets)
    );
    console.log(
      "getTrafficStatsByIface packets of sent = " +
      JSON.stringify(statsInfo.txPackets)
    );
  });
}

statistics.getTrafficStatsByIface10+

getTrafficStatsByIface(ifaceInfo: IfaceInfo): Promise<NetStatsInfo>

获取指定网卡历史流量信息,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.GET_NETWORK_STATS

系统能力:SystemCapability.Communication.NetManager.Core

参数名类型必填说明
ifaceInfoIfaceInfo指定查询的网卡信息,参见IfaceInfo

返回值:

类型说明
Promise<NetStatsInfo>以 Promise 形式返回获取结果,返回网卡历史流量信息。

错误码:

以下错误码的详细介绍参见statistics 错误码

错误码 ID错误信息
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Failed to connect to the service.
2100003System internal error.
2103017Failed to read the database.

示例:

import { statistics } from '@kit.NetworkKit';

let iFaceInfo: statistics.IfaceInfo|null = null;
if (iFaceInfo) {
  statistics.getTrafficStatsByIface(iFaceInfo as statistics.IfaceInfo).then((statsInfo: statistics.NetStatsInfo) => {
    console.log(
      "getTrafficStatsByIface bytes of received = " +
      JSON.stringify(statsInfo.rxBytes)
    );
    console.log(
      "getTrafficStatsByIface bytes of sent = " +
      JSON.stringify(statsInfo.txBytes)
    );
    console.log(
      "getTrafficStatsByIface packets of received = " +
      JSON.stringify(statsInfo.rxPackets)
    );
    console.log(
      "getTrafficStatsByIface packets of sent = " +
      JSON.stringify(statsInfo.txPackets)
    );
  });
}

statistics.getTrafficStatsByUid10+

getTrafficStatsByUid(uidInfo: UidInfo, callback: AsyncCallback<NetStatsInfo>): void

获取指定应用历史流量信息,使用 callback 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.GET_NETWORK_STATS

系统能力:SystemCapability.Communication.NetManager.Core

参数:

参数名类型必填说明
uidInfoUidInfo指定查询的应用信息,参见UidInfo
callbackAsyncCallback<NetStatsInfo>回调函数。成功时 statsInfo 返回包含应用历史流量信息,error 为 undefined,否则为错误对象。

错误码:

以下错误码的详细介绍参见statistics 错误码

错误码 ID错误信息
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Failed to connect to the service.
2100003System internal error.
2103017Failed to read the database.

示例:

import { BusinessError } from '@kit.BasicServicesKit';
import { statistics } from '@kit.NetworkKit';

let uidInfo: statistics.UidInfo = {
  uid: 20010037,
  ifaceInfo: {
    iface: '',
    startTime: 1,
    endTime: 3,
  }
}

statistics.getTrafficStatsByUid(
  uidInfo,
  (error: BusinessError, statsInfo: statistics.NetStatsInfo) => {
    console.error(JSON.stringify(error));
    console.log(
      "getTrafficStatsByUid bytes of received = " +
      JSON.stringify(statsInfo.rxBytes)
    );
    console.log(
      "getTrafficStatsByUid bytes of sent = " +
      JSON.stringify(statsInfo.txBytes)
    );
    console.log(
      "getTrafficStatsByUid packets of received = " +
      JSON.stringify(statsInfo.rxPackets)
    );
    console.log(
      "getTrafficStatsByUid packets of sent = " +
      JSON.stringify(statsInfo.txPackets)
    );
  }
);

statistics.getTrafficStatsByUid10+

getTrafficStatsByUid(uidInfo: UidInfo): Promise<NetStatsInfo>

获取指定应用历史流量信息,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.GET_NETWORK_STATS

系统能力:SystemCapability.Communication.NetManager.Core

参数:

参数名类型必填说明
uidInfoUidInfo指定查询的应用信息,参见UidInfo

返回值:

类型说明
Promise<NetStatsInfo>以 Promise 形式返回获取结果,返回应用历史流量信息。

错误码:

以下错误码的详细介绍参见statistics 错误码

错误码 ID错误信息
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Failed to connect to the service.
2100003System internal error.
2103017Failed to read the database.

示例:

import statistics from '@ohos.net.statistics'

let uidInfo: statistics.UidInfo = {
  uid: 20010037,
  ifaceInfo: {
    iface: '',
    startTime: 1,
    endTime: 3,
  }
}

statistics.getTrafficStatsByUid(uidInfo).then((statsInfo: statistics.NetStatsInfo) => {
  console.log("getTrafficStatsByUid bytes of received = " + JSON.stringify(statsInfo.rxBytes));
  console.log("getTrafficStatsByUid bytes of sent = " + JSON.stringify(statsInfo.txBytes));
  console.log("getTrafficStatsByUid packets of received = " + JSON.stringify(statsInfo.rxPackets));
  console.log("getTrafficStatsByUid packets of sent = " + JSON.stringify(statsInfo.txPackets));
})

statistics.getTrafficStatsByNetwork12+

getTrafficStatsByNetwork(networkInfo: NetworkInfo): Promise<UidNetStatsInfo>

获取指定时间段内所有应用在指定网络中的流量使用详情,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.GET_NETWORK_STATS

系统能力:SystemCapability.Communication.NetManager.Core

参数:

参数名类型必填说明
networkInfoNetworkInfo指定查询的网络信息,参见NetworkInfo

返回值:

类型说明
Promise<UidNetStatsInfo>以 Promise 形式返回获取结果。返回所有应用历史流量信息。

错误码:

以下错误码的详细介绍参见statistics 错误码

错误码 ID错误信息
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Failed to connect to the service.
2100003System internal error.
2103017Failed to read the database.

示例

import { connection, statistics } from '@kit.NetworkKit';

let networkInfo: statistics.NetworkInfo = {
  type: connection.NetBearType.BEARER_CELLULAR,
  startTime: Math.floor(Date.now() / 1000) - 86400 * 7, 
  endTime: Math.floor(Date.now() / 1000) + 5,
  simId: 1,
}

statistics.getTrafficStatsByNetwork(networkInfo).then((statsInfo: statistics.UidNetStatsInfo) => {
  let rank: Map<string, object> = new Map<string, object>(Object.entries(statsInfo));
  rank.forEach((value: object, key: string) => {
    console.info("getTrafficStatsByNetwork key=" + key + ", value=" + JSON.stringify(value));
  })
})

statistics.getTrafficStatsByUidNetwork12+

getTrafficStatsByUidNetwork(uid: number, networkInfo: NetworkInfo): Promise<NetStatsInfoSequence>

获取指定时间段内,应用在指定网络中的流量使用详情,使用 Promise 方式作为异步方法。

系统接口:此接口为系统接口。

需要权限:ohos.permission.GET_NETWORK_STATS

系统能力:SystemCapability.Communication.NetManager.Core

参数:

参数名类型必填说明
uidnumber指定查询的应用 UID。
networkInfoNetworkInfo指定查询的网络信息,参见NetworkInfo

返回值:

类型说明
Promise<NetStatsInfoSequence>以 Promise 形式返回获取结果。返回应用历史流量统计信息。

错误码:

以下错误码的详细介绍参见statistics 错误码

错误码 ID错误信息
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100001Invalid parameter value.
2100002Failed to connect to the service.
2100003System internal error.
2103017Failed to read the database.

示例

import { connection, statistics } from '@kit.NetworkKit';

let uid: number = 20020147;
let networkInfo: statistics.NetworkInfo = {
  type: connection.NetBearType.BEARER_CELLULAR,
  startTime: Math.floor(Date.now() / 1000) - 86400 * 7, 
  endTime: Math.floor(Date.now() / 1000) + 5,
  simId: 1,
}

statistics.getTrafficStatsByUidNetwork(uid, networkInfo).then((statsInfoSequence: statistics.NetStatsInfoSequence) => {
  for (let i = 0; i < statsInfoSequence.length; i--) {
    console.info("getTrafficStatsByUidNetwork item:" + JSON.stringify(statsInfoSequence[i]));
  }
})

IfaceInfo10+

查询网卡历史流量参数信息。

系统接口:此接口为系统接口。

系统能力:SystemCapability.Communication.NetManager.Core

名称类型必填说明
ifacestring查询的网卡名。
startTimenumber查询的开始时间(时间戳;单位:秒)。
endTimenumber查询的结束时间(时间戳;单位:秒)。

UidInfo10+

查询应用历史流量参数信息。

系统接口:此接口为系统接口。

系统能力:SystemCapability.Communication.NetManager.Core

名称类型必填说明
ifaceInfoIfaceInfo<IfaceInfo>需查询的网卡和时间参数信息。
uidnumber需查询的应用 uid。

NetStatsInfo10+

获取的历史流量信息。

系统接口:此接口为系统接口。

系统能力:SystemCapability.Communication.NetManager.Core

名称类型必填说明
rxBytesnumber流量下行数据(单位:字节)。
txBytesnumber流量上行数据(单位:字节)。
rxPacketsnumber流量下行包个数。
txPacketsnumber流量上行包个数。

NetStatsChangeInfo11+

监听和管理网络接口的状态和使用情况。

系统接口:此接口为系统接口。

系统能力:SystemCapability.Communication.NetManager.Core

名称类型必填说明
ifacestring网卡名称。
uidnumber应用UID。

NetworkInfo12+

网络信息。

系统接口:此接口为系统接口。

系统能力:SystemCapability.Communication.NetManager.Core

名称类型必填说明
typeNetBearType网络类型。
startTimenumber开始时间戳(单位:秒)。
endTimenumber结束时间戳(单位:秒)。
simIdnumberSIM 卡 ID。

UidNetStatsInfo12+

获取的所有应用历史流量信息。

系统接口:此接口为系统接口。

系统能力:SystemCapability.Communication.NetManager.Core

名称类型必填说明
undefined[uid:number]: NetStatsInfo所有应用的历史流量信息。

NetStatsInfoSequence12+

获取的应用历史流量信息。

系统接口:此接口为系统接口。

系统能力:SystemCapability.Communication.NetManager.Core

名称类型必填说明
startTimenumber开始时间戳(单位:秒)。
endTimenumber结束时间戳(单位:秒)。
infoNetStatsInfo获取的应用历史流量信息。

你可能感兴趣的鸿蒙文章

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/NCjinn