1e41f4b71Sopenharmony_ci# @ohos.net.statistics (Traffic Management) (System API) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **statistics** module provides APIs to query real-time or historical traffic statistics by the specified network interface card (NIC) or user ID (UID). 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 7e41f4b71Sopenharmony_ci> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.net.statistics (Traffic Management)](js-apis-net-statistics.md). 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Modules to Import 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```js 12e41f4b71Sopenharmony_ciimport { statistics } from '@kit.NetworkKit'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## statistics.on('netStatsChange')<sup>10+</sup> 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_cion(type: 'netStatsChange', callback: Callback\<NetStatsChangeInfo\>): void 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciSubscribes to traffic change events. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci**System API**: This is a system API. 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.GET_NETWORK_STATS 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci**Parameters** 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 31e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- | ---- | ----------------------------------------------------------------- | 32e41f4b71Sopenharmony_ci| type | string | Yes | Event type. This field has a fixed value of **netStatsChange**. | 33e41f4b71Sopenharmony_ci| callback | Callback\<[NetStatsChangeInfo](#netstatschangeinfo11)\> | Yes | Callback invoked when the traffic changes.| 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**Error codes** 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ciFor details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md). 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci| ID| Error Message | 40e41f4b71Sopenharmony_ci| --------- | -------------------------------------------- | 41e41f4b71Sopenharmony_ci| 201 | Permission denied. | 42e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 43e41f4b71Sopenharmony_ci| 401 | Parameter error. | 44e41f4b71Sopenharmony_ci| 2100002 | Failed to connect to the service. | 45e41f4b71Sopenharmony_ci| 2100003 | System internal error. | 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**Example** 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci```js 50e41f4b71Sopenharmony_ciimport { statistics } from '@kit.NetworkKit'; 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ciclass IFace { 53e41f4b71Sopenharmony_ci iface: string = "" 54e41f4b71Sopenharmony_ci uid?: number = 0 55e41f4b71Sopenharmony_ci} 56e41f4b71Sopenharmony_cistatistics.on('netStatsChange', (data: IFace) => { 57e41f4b71Sopenharmony_ci console.log('on netStatsChange' + JSON.stringify(data)); 58e41f4b71Sopenharmony_ci}); 59e41f4b71Sopenharmony_ci``` 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci## statistics.off('netStatsChange')<sup>10+</sup> 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_cioff(type: 'netStatsChange', callback?: Callback\<NetStatsChangeInfo>): void; 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ciUnsubscribes from traffic change events. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**System API**: This is a system API. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.GET_NETWORK_STATS 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**Parameters** 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 76e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- | ---- | ----------------------------------------------------------------- | 77e41f4b71Sopenharmony_ci| type | string | Yes | Event type. This field has a fixed value of **netStatsChange**. | 78e41f4b71Sopenharmony_ci| callback | Callback\<[NetStatsChangeInfo](#netstatschangeinfo11)\> | No | Callback invoked when the traffic changes.| 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci**Error codes** 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ciFor details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md). 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci| ID| Error Message | 85e41f4b71Sopenharmony_ci| --------- | -------------------------------------------- | 86e41f4b71Sopenharmony_ci| 201 | Permission denied. | 87e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 88e41f4b71Sopenharmony_ci| 401 | Parameter error. | 89e41f4b71Sopenharmony_ci| 2100002 | Failed to connect to the service. | 90e41f4b71Sopenharmony_ci| 2100003 | System internal error. | 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci**Example** 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci```js 95e41f4b71Sopenharmony_ciimport { statistics } from '@kit.NetworkKit'; 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ciclass IFace { 98e41f4b71Sopenharmony_ci iface: string = "" 99e41f4b71Sopenharmony_ci uid?: number = 0 100e41f4b71Sopenharmony_ci} 101e41f4b71Sopenharmony_cilet callback: (data: IFace) => void = (data: IFace) => { 102e41f4b71Sopenharmony_ci console.log("on netStatsChange, iFace:" + data.iface + " uid: " + data.uid); 103e41f4b71Sopenharmony_ci} 104e41f4b71Sopenharmony_cistatistics.on('netStatsChange', callback); 105e41f4b71Sopenharmony_ci// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks. 106e41f4b71Sopenharmony_cistatistics.off('netStatsChange', callback); 107e41f4b71Sopenharmony_cistatistics.off('netStatsChange'); 108e41f4b71Sopenharmony_ci``` 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci## statistics.getTrafficStatsByIface<sup>10+</sup> 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_cigetTrafficStatsByIface(ifaceInfo: IfaceInfo, callback: AsyncCallback\<NetStatsInfo>): void; 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ciObtains the historical traffic statistics of the specified NIC. This API uses an asynchronous callback to return the result. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci**System API**: This is a system API. 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.GET_NETWORK_STATS 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci**Parameters** 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 125e41f4b71Sopenharmony_ci| --------- | ----------------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 126e41f4b71Sopenharmony_ci| ifaceInfo | [IfaceInfo](#ifaceinfo10) | Yes | NIC information. For details, see [IfaceInfo](#ifaceinfo10). | 127e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[NetStatsInfo](#netstatsinfo10)> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **statsInfo** is the historical traffic statistics of the NIC. Otherwise, **error** is an error object.| 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci**Error codes** 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ciFor details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md). 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci| ID| Error Message | 134e41f4b71Sopenharmony_ci| --------- | -------------------------------------------- | 135e41f4b71Sopenharmony_ci| 201 | Permission denied. | 136e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 137e41f4b71Sopenharmony_ci| 401 | Parameter error. | 138e41f4b71Sopenharmony_ci| 2100001 | Invalid parameter value. | 139e41f4b71Sopenharmony_ci| 2100002 | Failed to connect to the service. | 140e41f4b71Sopenharmony_ci| 2100003 | System internal error. | 141e41f4b71Sopenharmony_ci| 2103017 | Failed to read the database. | 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci**Example** 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci```js 146e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 147e41f4b71Sopenharmony_ciimport { statistics } from '@kit.NetworkKit'; 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_cilet iFaceInfo: statistics.IfaceInfo | null = null; 150e41f4b71Sopenharmony_ciif (iFaceInfo) { 151e41f4b71Sopenharmony_ci statistics.getTrafficStatsByIface(iFaceInfo as statistics.IfaceInfo, (error: BusinessError, statsInfo: statistics.NetStatsInfo) => { 152e41f4b71Sopenharmony_ci console.log(JSON.stringify(error)); 153e41f4b71Sopenharmony_ci console.log( 154e41f4b71Sopenharmony_ci "getTrafficStatsByIface bytes of received = " + 155e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.rxBytes) 156e41f4b71Sopenharmony_ci ); 157e41f4b71Sopenharmony_ci console.log( 158e41f4b71Sopenharmony_ci "getTrafficStatsByIface bytes of sent = " + 159e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.txBytes) 160e41f4b71Sopenharmony_ci ); 161e41f4b71Sopenharmony_ci console.log( 162e41f4b71Sopenharmony_ci "getTrafficStatsByIface packets of received = " + 163e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.rxPackets) 164e41f4b71Sopenharmony_ci ); 165e41f4b71Sopenharmony_ci console.log( 166e41f4b71Sopenharmony_ci "getTrafficStatsByIface packets of sent = " + 167e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.txPackets) 168e41f4b71Sopenharmony_ci ); 169e41f4b71Sopenharmony_ci }); 170e41f4b71Sopenharmony_ci} 171e41f4b71Sopenharmony_ci``` 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci## statistics.getTrafficStatsByIface<sup>10+</sup> 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_cigetTrafficStatsByIface(ifaceInfo: IfaceInfo): Promise\<NetStatsInfo>; 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ciObtains the historical traffic statistics of the specified NIC. This API uses a promise to return the result. 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci**System API**: This is a system API. 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.GET_NETWORK_STATS 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 186e41f4b71Sopenharmony_ci| --------- | ------------------------- | ---- | --------------------------------------------------- | 187e41f4b71Sopenharmony_ci| ifaceInfo | [IfaceInfo](#ifaceinfo10) | Yes | NIC information. For details, see [IfaceInfo](#ifaceinfo10).| 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci**Return value** 190e41f4b71Sopenharmony_ci| Type| Description| 191e41f4b71Sopenharmony_ci| -------- | -------- | 192e41f4b71Sopenharmony_ci| Promise\<[NetStatsInfo](#netstatsinfo10)> | Promise used to return the result, which is the historical traffic statistics of the specified NIC.| 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci**Error codes** 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ciFor details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md). 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci| ID| Error Message | 199e41f4b71Sopenharmony_ci| --------- | -------------------------------------------- | 200e41f4b71Sopenharmony_ci| 201 | Permission denied. | 201e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 202e41f4b71Sopenharmony_ci| 401 | Parameter error. | 203e41f4b71Sopenharmony_ci| 2100001 | Invalid parameter value. | 204e41f4b71Sopenharmony_ci| 2100002 | Failed to connect to the service. | 205e41f4b71Sopenharmony_ci| 2100003 | System internal error. | 206e41f4b71Sopenharmony_ci| 2103017 | Failed to read the database. | 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci**Example** 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci```js 211e41f4b71Sopenharmony_ciimport { statistics } from '@kit.NetworkKit'; 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_cilet iFaceInfo: statistics.IfaceInfo | null = null; 214e41f4b71Sopenharmony_ciif (iFaceInfo) { 215e41f4b71Sopenharmony_ci statistics.getTrafficStatsByIface(iFaceInfo as statistics.IfaceInfo).then((statsInfo: statistics.NetStatsInfo) => { 216e41f4b71Sopenharmony_ci console.log( 217e41f4b71Sopenharmony_ci "getTrafficStatsByIface bytes of received = " + 218e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.rxBytes) 219e41f4b71Sopenharmony_ci ); 220e41f4b71Sopenharmony_ci console.log( 221e41f4b71Sopenharmony_ci "getTrafficStatsByIface bytes of sent = " + 222e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.txBytes) 223e41f4b71Sopenharmony_ci ); 224e41f4b71Sopenharmony_ci console.log( 225e41f4b71Sopenharmony_ci "getTrafficStatsByIface packets of received = " + 226e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.rxPackets) 227e41f4b71Sopenharmony_ci ); 228e41f4b71Sopenharmony_ci console.log( 229e41f4b71Sopenharmony_ci "getTrafficStatsByIface packets of sent = " + 230e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.txPackets) 231e41f4b71Sopenharmony_ci ); 232e41f4b71Sopenharmony_ci }); 233e41f4b71Sopenharmony_ci} 234e41f4b71Sopenharmony_ci``` 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci## statistics.getTrafficStatsByUid<sup>10+</sup> 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_cigetTrafficStatsByUid(uidInfo: UidInfo, callback: AsyncCallback\<NetStatsInfo>): void; 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ciObtains the historical traffic statistics of the specified application. This API uses an asynchronous callback to return the result. 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci**System API**: This is a system API. 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.GET_NETWORK_STATS 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci**Parameters** 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 251e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------- | ---- | -------------------------------------------------------------------------------------- | 252e41f4b71Sopenharmony_ci| uidInfo | [UidInfo](#uidinfo10) | Yes | Application information. For details, see [UidInfo](#uidinfo10). | 253e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[NetStatsInfo](#netstatsinfo10)> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **statsInfo** is the historical traffic statistics of the application. Otherwise, **error** is an error object.| 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci**Error codes** 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ciFor details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md). 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci| ID| Error Message | 260e41f4b71Sopenharmony_ci| --------- | -------------------------------------------- | 261e41f4b71Sopenharmony_ci| 201 | Permission denied. | 262e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 263e41f4b71Sopenharmony_ci| 401 | Parameter error. | 264e41f4b71Sopenharmony_ci| 2100001 | Invalid parameter value. | 265e41f4b71Sopenharmony_ci| 2100002 | Failed to connect to the service. | 266e41f4b71Sopenharmony_ci| 2100003 | System internal error. | 267e41f4b71Sopenharmony_ci| 2103017 | Failed to read the database. | 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci**Example** 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci```js 272e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 273e41f4b71Sopenharmony_ciimport { statistics } from '@kit.NetworkKit'; 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_cilet uidInfo: statistics.UidInfo = { 276e41f4b71Sopenharmony_ci uid: 20010037, 277e41f4b71Sopenharmony_ci ifaceInfo: { 278e41f4b71Sopenharmony_ci iface: '', 279e41f4b71Sopenharmony_ci startTime: 1, 280e41f4b71Sopenharmony_ci endTime: 3, 281e41f4b71Sopenharmony_ci } 282e41f4b71Sopenharmony_ci} 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_cistatistics.getTrafficStatsByUid( 285e41f4b71Sopenharmony_ci uidInfo, 286e41f4b71Sopenharmony_ci (error: BusinessError, statsInfo: statistics.NetStatsInfo) => { 287e41f4b71Sopenharmony_ci console.log(JSON.stringify(error)); 288e41f4b71Sopenharmony_ci console.log( 289e41f4b71Sopenharmony_ci "getTrafficStatsByUid bytes of received = " + 290e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.rxBytes) 291e41f4b71Sopenharmony_ci ); 292e41f4b71Sopenharmony_ci console.log( 293e41f4b71Sopenharmony_ci "getTrafficStatsByUid bytes of sent = " + 294e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.txBytes) 295e41f4b71Sopenharmony_ci ); 296e41f4b71Sopenharmony_ci console.log( 297e41f4b71Sopenharmony_ci "getTrafficStatsByUid packets of received = " + 298e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.rxPackets) 299e41f4b71Sopenharmony_ci ); 300e41f4b71Sopenharmony_ci console.log( 301e41f4b71Sopenharmony_ci "getTrafficStatsByUid packets of sent = " + 302e41f4b71Sopenharmony_ci JSON.stringify(statsInfo.txPackets) 303e41f4b71Sopenharmony_ci ); 304e41f4b71Sopenharmony_ci } 305e41f4b71Sopenharmony_ci); 306e41f4b71Sopenharmony_ci``` 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_ci## statistics.getTrafficStatsByUid<sup>10+</sup> 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_cigetTrafficStatsByUid(uidInfo: UidInfo): Promise\<NetStatsInfo>; 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ciObtains the historical traffic statistics of the specified application. This API uses a promise to return the result. 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci**System API**: This is a system API. 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.GET_NETWORK_STATS 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci**Parameters** 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 323e41f4b71Sopenharmony_ci| ------- | --------------------- | ---- | ----------------------------------------------- | 324e41f4b71Sopenharmony_ci| uidInfo | [UidInfo](#uidinfo10) | Yes | Application information. For details, see [UidInfo](#uidinfo10).| 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci**Return value** 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci| Type | Description | 329e41f4b71Sopenharmony_ci| ----------------------------------------- | -------------------------------------------------- | 330e41f4b71Sopenharmony_ci| Promise\<[NetStatsInfo](#netstatsinfo10)> | Promise used to return the result, which is the historical traffic statistics of the specified NIC.| 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci**Error codes** 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ciFor details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md). 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci| ID| Error Message | 337e41f4b71Sopenharmony_ci| --------- | -------------------------------------------- | 338e41f4b71Sopenharmony_ci| 201 | Permission denied. | 339e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 340e41f4b71Sopenharmony_ci| 401 | Parameter error. | 341e41f4b71Sopenharmony_ci| 2100001 | Invalid parameter value. | 342e41f4b71Sopenharmony_ci| 2100002 | Failed to connect to the service. | 343e41f4b71Sopenharmony_ci| 2100003 | System internal error. | 344e41f4b71Sopenharmony_ci| 2103017 | Failed to read the database. | 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci**Example** 347e41f4b71Sopenharmony_ci 348e41f4b71Sopenharmony_ci```js 349e41f4b71Sopenharmony_ciimport statistics from '@ohos.net.statistics' 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_cilet uidInfo: statistics.UidInfo = { 352e41f4b71Sopenharmony_ci uid: 20010037, 353e41f4b71Sopenharmony_ci ifaceInfo: { 354e41f4b71Sopenharmony_ci iface: '', 355e41f4b71Sopenharmony_ci startTime: 1, 356e41f4b71Sopenharmony_ci endTime: 3, 357e41f4b71Sopenharmony_ci } 358e41f4b71Sopenharmony_ci} 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_cistatistics.getTrafficStatsByUid(uidInfo).then((statsInfo: statistics.NetStatsInfo) => { 361e41f4b71Sopenharmony_ci console.log("getTrafficStatsByUid bytes of received = " + JSON.stringify(statsInfo.rxBytes)); 362e41f4b71Sopenharmony_ci console.log("getTrafficStatsByUid bytes of sent = " + JSON.stringify(statsInfo.txBytes)); 363e41f4b71Sopenharmony_ci console.log("getTrafficStatsByUid packets of received = " + JSON.stringify(statsInfo.rxPackets)); 364e41f4b71Sopenharmony_ci console.log("getTrafficStatsByUid packets of sent = " + JSON.stringify(statsInfo.txPackets)); 365e41f4b71Sopenharmony_ci}) 366e41f4b71Sopenharmony_ci``` 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci## statistics.getTrafficStatsByNetwork<sup>12+</sup> 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_cigetTrafficStatsByNetwork(networkInfo: NetworkInfo): Promise\<UidNetStatsInfo> 371e41f4b71Sopenharmony_ci 372e41f4b71Sopenharmony_ciObtains the traffic statistics of all applications on the specified network within the specified period. This API uses a promise to return the result. 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci**System API**: This is a system API. 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.GET_NETWORK_STATS 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 379e41f4b71Sopenharmony_ci 380e41f4b71Sopenharmony_ci**Parameters** 381e41f4b71Sopenharmony_ci 382e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 383e41f4b71Sopenharmony_ci|-------------|-------------------------------|----|--------------------------------------------| 384e41f4b71Sopenharmony_ci| networkInfo | [NetworkInfo](#networkinfo12) | Yes | Network information. For details, see [NetworkInfo](#networkinfo12).| 385e41f4b71Sopenharmony_ci 386e41f4b71Sopenharmony_ci**Return value** 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ci| Type | Description | 389e41f4b71Sopenharmony_ci|-------------------------------------------------|----------------------------------| 390e41f4b71Sopenharmony_ci| Promise\<[UidNetStatsInfo](#uidnetstatsinfo12)> | Promise used to return the result, which is the historical traffic statistics of all applications.| 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ci**Error codes** 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ciFor details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md). 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ci| ID| Error Message | 397e41f4b71Sopenharmony_ci| --------- | -------------------------------------------- | 398e41f4b71Sopenharmony_ci| 201 | Permission denied. | 399e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 400e41f4b71Sopenharmony_ci| 401 | Parameter error. | 401e41f4b71Sopenharmony_ci| 2100001 | Invalid parameter value. | 402e41f4b71Sopenharmony_ci| 2100002 | Failed to connect to the service. | 403e41f4b71Sopenharmony_ci| 2100003 | System internal error. | 404e41f4b71Sopenharmony_ci| 2103017 | Failed to read the database. | 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci**Example** 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci```js 409e41f4b71Sopenharmony_ciimport { connection, statistics } from '@kit.NetworkKit'; 410e41f4b71Sopenharmony_ci 411e41f4b71Sopenharmony_cilet networkInfo: statistics.NetworkInfo = { 412e41f4b71Sopenharmony_ci type: connection.NetBearType.BEARER_CELLULAR, 413e41f4b71Sopenharmony_ci startTime: Math.floor(Date.now() / 1000) - 86400 * 7, 414e41f4b71Sopenharmony_ci endTime: Math.floor(Date.now() / 1000) + 5, 415e41f4b71Sopenharmony_ci simId: 1, 416e41f4b71Sopenharmony_ci} 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_cistatistics.getTrafficStatsByNetwork(networkInfo).then((statsInfo: statistics.UidNetStatsInfo) => { 419e41f4b71Sopenharmony_ci let rank: Map<string, object> = new Map<string, object>(Object.entries(statsInfo)); 420e41f4b71Sopenharmony_ci rank.forEach((value: object, key: string) => { 421e41f4b71Sopenharmony_ci console.info("getTrafficStatsByNetwork key=" + key + ", value=" + JSON.stringify(value)); 422e41f4b71Sopenharmony_ci }) 423e41f4b71Sopenharmony_ci}) 424e41f4b71Sopenharmony_ci``` 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ci## statistics.getTrafficStatsByUidNetwork<sup>12+</sup> 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_cigetTrafficStatsByUidNetwork(uid: number, networkInfo: NetworkInfo): Promise\<NetStatsInfoSequence> 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ciObtains the traffic statistics of the specified application on the specified network within the specified period. This API uses a promise to return the result. 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci**System API**: This is a system API. 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.GET_NETWORK_STATS 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci**Parameters** 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 441e41f4b71Sopenharmony_ci|-------------|-------------------------------|----|--------------------------------------------| 442e41f4b71Sopenharmony_ci| uid | number | Yes | Application UID. | 443e41f4b71Sopenharmony_ci| networkInfo | [NetworkInfo](#networkinfo12) | Yes | Network information. For details, see [NetworkInfo](#networkinfo12).| 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci**Return value** 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci| Type | Description | 448e41f4b71Sopenharmony_ci|-----------------------------------------------------------|----------------------------------| 449e41f4b71Sopenharmony_ci| Promise\<[NetStatsInfoSequence](#netstatsinfosequence12)> | Promise used to return the result, which is the historical traffic statistics of the specified application.| 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ci**Error codes** 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ciFor details about the error codes, see [Traffic Management Error Codes](errorcode-net-statistics.md). 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci| ID| Error Message | 456e41f4b71Sopenharmony_ci| --------- | -------------------------------------------- | 457e41f4b71Sopenharmony_ci| 201 | Permission denied. | 458e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 459e41f4b71Sopenharmony_ci| 401 | Parameter error. | 460e41f4b71Sopenharmony_ci| 2100001 | Invalid parameter value. | 461e41f4b71Sopenharmony_ci| 2100002 | Failed to connect to the service. | 462e41f4b71Sopenharmony_ci| 2100003 | System internal error. | 463e41f4b71Sopenharmony_ci| 2103017 | Failed to read the database. | 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci**Example** 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci```js 468e41f4b71Sopenharmony_ciimport { connection, statistics } from '@kit.NetworkKit'; 469e41f4b71Sopenharmony_ci 470e41f4b71Sopenharmony_cilet uid: number = 20020147; 471e41f4b71Sopenharmony_cilet networkInfo: statistics.NetworkInfo = { 472e41f4b71Sopenharmony_ci type: connection.NetBearType.BEARER_CELLULAR, 473e41f4b71Sopenharmony_ci startTime: Math.floor(Date.now() / 1000) - 86400 * 7, 474e41f4b71Sopenharmony_ci endTime: Math.floor(Date.now() / 1000) + 5, 475e41f4b71Sopenharmony_ci simId: 1, 476e41f4b71Sopenharmony_ci} 477e41f4b71Sopenharmony_ci 478e41f4b71Sopenharmony_cistatistics.getTrafficStatsByUidNetwork(uid, networkInfo).then((statsInfoSequence: statistics.NetStatsInfoSequence) => { 479e41f4b71Sopenharmony_ci for (let i = 0; i < statsInfoSequence.length; i--) { 480e41f4b71Sopenharmony_ci console.info("getTrafficStatsByUidNetwork item:" + JSON.stringify(statsInfoSequence[i])); 481e41f4b71Sopenharmony_ci } 482e41f4b71Sopenharmony_ci}) 483e41f4b71Sopenharmony_ci``` 484e41f4b71Sopenharmony_ci 485e41f4b71Sopenharmony_ci## IfaceInfo<sup>10+</sup> 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ciDefines the parameters for querying historical traffic of an NIC. 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci**System API**: This is a system API. 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 494e41f4b71Sopenharmony_ci| --------- | ------ | ---- | --------------------------------- | 495e41f4b71Sopenharmony_ci| iface | string | Yes | NIC name. | 496e41f4b71Sopenharmony_ci| startTime | number | Yes | Start time of the query, which is a timestamp in seconds.| 497e41f4b71Sopenharmony_ci| endTime | number | Yes | End time of the query, which is a timestamp in seconds.| 498e41f4b71Sopenharmony_ci 499e41f4b71Sopenharmony_ci## UidInfo<sup>10+</sup> 500e41f4b71Sopenharmony_ci 501e41f4b71Sopenharmony_ciDefines the parameters for querying historical traffic of an application. 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci**System API**: This is a system API. 504e41f4b71Sopenharmony_ci 505e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 506e41f4b71Sopenharmony_ci 507e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 508e41f4b71Sopenharmony_ci| --------- | ------------------------------------- | ---- | -------------------------- | 509e41f4b71Sopenharmony_ci| ifaceInfo | IfaceInfo\<[IfaceInfo](#ifaceinfo10)> | Yes | NIC information, including the NIC name and query time range.| 510e41f4b71Sopenharmony_ci| uid | number | Yes | Application UID. | 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci## NetStatsInfo<sup>10+</sup> 513e41f4b71Sopenharmony_ci 514e41f4b71Sopenharmony_ciDefines the historical traffic information. 515e41f4b71Sopenharmony_ci 516e41f4b71Sopenharmony_ci**System API**: This is a system API. 517e41f4b71Sopenharmony_ci 518e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 521e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ------------------------ | 522e41f4b71Sopenharmony_ci| rxBytes | number | Yes | Downlink traffic, in bytes.| 523e41f4b71Sopenharmony_ci| txBytes | number | Yes | Uplink traffic, in bytes.| 524e41f4b71Sopenharmony_ci| rxPackets | number | Yes | Number of downlink packets. | 525e41f4b71Sopenharmony_ci| txPackets | number | Yes | Number of uplink packets. | 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ci## NetStatsChangeInfo<sup>11+</sup> 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_ciDefines the NIC status and usage of an application. 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ci**System API**: This is a system API. 532e41f4b71Sopenharmony_ci 533e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 534e41f4b71Sopenharmony_ci 535e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 536e41f4b71Sopenharmony_ci| --------- | ------ | ---- | --------- | 537e41f4b71Sopenharmony_ci| iface | string | Yes | NIC name.| 538e41f4b71Sopenharmony_ci| uid | number | No | Application UID. | 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ci## NetworkInfo<sup>12+</sup> 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ciDefines the network information. 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ci**System API**: This is a system API. 545e41f4b71Sopenharmony_ci 546e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 547e41f4b71Sopenharmony_ci 548e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 549e41f4b71Sopenharmony_ci|-----------|------------------------------------------------------|----|--------------| 550e41f4b71Sopenharmony_ci| type | [NetBearType](js-apis-net-connection.md#netbeartype) | Yes | Network type. | 551e41f4b71Sopenharmony_ci| startTime | number | Yes | Start timestamp, in seconds.| 552e41f4b71Sopenharmony_ci| endTime | number | Yes | End timestamp, in seconds.| 553e41f4b71Sopenharmony_ci| simId | number | No | SIM card ID. | 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ci## UidNetStatsInfo<sup>12+</sup> 556e41f4b71Sopenharmony_ci 557e41f4b71Sopenharmony_ciDefines the historical traffic statistics of all applications. 558e41f4b71Sopenharmony_ci 559e41f4b71Sopenharmony_ci**System API**: This is a system API. 560e41f4b71Sopenharmony_ci 561e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 562e41f4b71Sopenharmony_ci 563e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 564e41f4b71Sopenharmony_ci|-----------|-----------------------------------------------|----|--------------| 565e41f4b71Sopenharmony_ci| undefined | [uid:number]: [NetStatsInfo](#netstatsinfo10) | Yes | Historical traffic statistics of all applications.| 566e41f4b71Sopenharmony_ci 567e41f4b71Sopenharmony_ci## NetStatsInfoSequence<sup>12+</sup> 568e41f4b71Sopenharmony_ci 569e41f4b71Sopenharmony_ciDefines the historical traffic statistics of the specified application. 570e41f4b71Sopenharmony_ci 571e41f4b71Sopenharmony_ci**System API**: This is a system API. 572e41f4b71Sopenharmony_ci 573e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 574e41f4b71Sopenharmony_ci 575e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 576e41f4b71Sopenharmony_ci|-----------|---------------------------------|----|--------------| 577e41f4b71Sopenharmony_ci| startTime | number | Yes | Start timestamp, in seconds.| 578e41f4b71Sopenharmony_ci| endTime | number | Yes | End timestamp, in seconds.| 579e41f4b71Sopenharmony_ci| info | [NetStatsInfo](#netstatsinfo10) | Yes | Historical traffic statistics of the specified application.| 580