• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.net.statistics (流量管理)
2
3流量管理模块,支持基于网卡/UID 的实时流量统计和历史流量统计查询能力。
4
5> **说明:**
6> 本模块首批接口从 API version 10 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
7
8## 导入模块
9
10```js
11import statistics from "@ohos.net.statistics";
12```
13
14## statistics.getIfaceRxBytes<sup>10+</sup>
15
16getIfaceRxBytes(nic: string, callback: AsyncCallback\<number>): void;
17
18获取指定网卡实时下行流量,使用 callback 方式作为异步方法。
19
20**系统能力**:SystemCapability.Communication.NetManager.Core
21
22**参数:**
23
24| 参数名   | 类型                   | 必填 | 说明                                                                                                                    |
25| -------- | ---------------------- | ---- | ----------------------------------------------------------------------------------------------------------------------- |
26| nic      | string                 | 是   | 指定查询的网卡名。                                                                                                      |
27| callback | AsyncCallback\<number> | 是   | 回调函数。当成功获取网卡实时下行流量时,error 为 undefined,stats 为获取到的网卡实时下行流量(单位:字节);否则为错误对象 |
28
29**错误码:**
30
31以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
32
33| 错误码 ID | 错误信息                                     |
34| --------- | -------------------------------------------- |
35| 401       | Parameter error.                             |
36| 2100002   | Operation failed. Cannot connect to service. |
37| 2100003   | System internal error.                       |
38| 2103005   | Failed to read map.                          |
39| 2103011   | Failed to create map.                        |
40| 2103012   | Get iface name failed.                       |
41
42**示例:**
43
44```js
45import { BusinessError } from '@ohos.base';
46import statistics from '@ohos.net.statistics';
47
48statistics.getIfaceRxBytes("wlan0", (error: BusinessError, stats: number) => {
49  console.log(JSON.stringify(error));
50  console.log(JSON.stringify(stats));
51});
52```
53
54## statistics.getIfaceRxBytes<sup>10+</sup>
55
56getIfaceRxBytes(nic: string): Promise\<number>;
57
58获取指定网卡实时下行流量,使用 Promise 方式作为异步方法。
59
60**系统能力**:SystemCapability.Communication.NetManager.Core
61
62**参数:**
63
64| 参数名 | 类型   | 必填 | 说明               |
65| ------ | ------ | ---- | ------------------ |
66| nic    | string | 是   | 指定查询的网卡名。 |
67
68**返回值:**
69| 类型 | 说明 |
70| -------- | -------- |
71| Promise\<number> | 以 Promise 形式返回获取结果。返回网卡实时下行流量(单位:字节)。 |
72
73**错误码:**
74
75以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
76
77| 错误码 ID | 错误信息                                     |
78| --------- | -------------------------------------------- |
79| 401       | Parameter error.                             |
80| 2100002   | Operation failed. Cannot connect to service. |
81| 2100003   | System internal error.                       |
82| 2103005   | Failed to read map.                          |
83| 2103011   | Failed to create map.                        |
84| 2103012   | Get iface name failed.                       |
85
86**示例:**
87
88```js
89import statistics from '@ohos.net.statistics';
90
91statistics.getIfaceRxBytes("wlan0").then((stats: number) => {
92  console.log(JSON.stringify(stats));
93});
94```
95
96## statistics.getIfaceTxBytes<sup>10+</sup>
97
98getIfaceTxBytes(nic: string, callback: AsyncCallback\<number>): void;
99
100获取指定网卡实时上行流量,使用 callback 方式作为异步方法。
101
102**系统能力**:SystemCapability.Communication.NetManager.Core
103
104**参数:**
105
106| 参数名   | 类型                   | 必填 | 说明                                                                                                                    |
107| -------- | ---------------------- | ---- | ----------------------------------------------------------------------------------------------------------------------- |
108| nic      | string                 | 是   | 指定查询的网卡名。                                                                                                      |
109| callback | AsyncCallback\<number> | 是   | 回调函数。当成功获取网卡实时上行流量时,error 为 undefined,stats 为获取到的网卡实时上行流量(单位:字节);否则为错误对象 |
110
111**错误码:**
112
113以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
114
115| 错误码 ID | 错误信息                                     |
116| --------- | -------------------------------------------- |
117| 401       | Parameter error.                             |
118| 2100002   | Operation failed. Cannot connect to service. |
119| 2100003   | System internal error.                       |
120| 2103005   | Failed to read map.                          |
121| 2103011   | Failed to create map.                        |
122| 2103012   | Get iface name failed.                       |
123
124**示例:**
125
126```js
127import { BusinessError } from '@ohos.base';
128import statistics from '@ohos.net.statistics';
129
130statistics.getIfaceTxBytes("wlan0", (error: BusinessError, stats: number) => {
131  console.log(JSON.stringify(error));
132  console.log(JSON.stringify(stats));
133});
134```
135
136## statistics.getIfaceTxBytes<sup>10+</sup>
137
138getIfaceTxBytes(nic: string): Promise\<number>;
139
140获取指定网卡实时上行流量,使用 Promise 方式作为异步方法。
141
142**系统能力**:SystemCapability.Communication.NetManager.Core
143
144**参数:**
145
146| 参数名 | 类型   | 必填 | 说明               |
147| ------ | ------ | ---- | ------------------ |
148| nic    | string | 是   | 指定查询的网卡名。 |
149
150**返回值:**
151| 类型 | 说明 |
152| -------- | -------- |
153| Promise\<number> | 以 Promise 形式返回获取结果。返回网卡实时上行流量(单位:字节)。 |
154
155**错误码:**
156
157以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
158
159| 错误码 ID | 错误信息                                     |
160| --------- | -------------------------------------------- |
161| 401       | Parameter error.                             |
162| 2100002   | Operation failed. Cannot connect to service. |
163| 2100003   | System internal error.                       |
164| 2103005   | Failed to read map.                          |
165| 2103011   | Failed to create map.                        |
166| 2103012   | Get iface name failed.                       |
167
168**示例:**
169
170```js
171import statistics from '@ohos.net.statistics';
172
173statistics.getIfaceTxBytes("wlan0").then((stats: number) => {
174  console.log(JSON.stringify(stats));
175});
176```
177
178## statistics.getCellularRxBytes<sup>10+</sup>
179
180getCellularRxBytes(callback: AsyncCallback\<number>): void;
181
182获取蜂窝实时下行流量,使用 callback 方式作为异步方法。
183
184**系统能力**:SystemCapability.Communication.NetManager.Core
185
186**参数:**
187
188| 参数名   | 类型                   | 必填 | 说明                                                                                                                    |
189| -------- | ---------------------- | ---- | ----------------------------------------------------------------------------------------------------------------------- |
190| callback | AsyncCallback\<number> | 是   | 回调函数。当成功获取蜂窝实时下行流量时,error 为 undefined,stats 为获取到的蜂窝实时下行流量(单位:字节);否则为错误对象 |
191
192**错误码:**
193
194以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
195
196| 错误码 ID | 错误信息                                     |
197| --------- | -------------------------------------------- |
198| 2100002   | Operation failed. Cannot connect to service. |
199| 2100003   | System internal error.                       |
200| 2103005   | Failed to read map.                          |
201| 2103011   | Failed to create map.                        |
202| 2103012   | Get iface name failed.                       |
203
204**示例:**
205
206```js
207import { BusinessError } from '@ohos.base';
208import statistics from '@ohos.net.statistics';
209
210statistics.getCellularRxBytes((error: BusinessError, stats: number) => {
211  console.log(JSON.stringify(error));
212  console.log(JSON.stringify(stats));
213});
214```
215
216## statistics.getCellularRxBytes<sup>10+</sup>
217
218getCellularRxBytes(): Promise\<number>;
219
220获取蜂窝实时下行流量,使用 Promise 方式作为异步方法。
221
222**系统能力**:SystemCapability.Communication.NetManager.Core
223
224**返回值:**
225| 类型 | 说明 |
226| -------- | -------- |
227| Promise\<number> | 以 Promise 形式返回获取结果。返回蜂窝实时下行流量(单位:字节)。 |
228
229**错误码:**
230
231以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
232
233| 错误码 ID | 错误信息                                     |
234| --------- | -------------------------------------------- |
235| 2100002   | Operation failed. Cannot connect to service. |
236| 2100003   | System internal error.                       |
237| 2103005   | Failed to read map.                          |
238| 2103011   | Failed to create map.                        |
239| 2103012   | Get iface name failed.                       |
240
241**示例:**
242
243```js
244import statistics from '@ohos.net.statistics';
245
246statistics.getCellularRxBytes().then((stats: number) => {
247  console.log(JSON.stringify(stats));
248});
249```
250
251## statistics.getCellularTxBytes<sup>10+</sup>
252
253getCellularTxBytes(callback: AsyncCallback\<number>): void;
254
255获取蜂窝实时上行流量,使用 callback 方式作为异步方法。
256
257**系统能力**:SystemCapability.Communication.NetManager.Core
258
259**参数:**
260
261| 参数名   | 类型                   | 必填 | 说明                                                                                                                    |
262| -------- | ---------------------- | ---- | ----------------------------------------------------------------------------------------------------------------------- |
263| callback | AsyncCallback\<number> | 是   | 回调函数。当成功获取蜂窝实时上行流量时,error 为 undefined,stats 为获取到的蜂窝实时上行流量(单位:字节);否则为错误对象 |
264
265**错误码:**
266
267以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
268
269| 错误码 ID | 错误信息                                     |
270| --------- | -------------------------------------------- |
271| 2100002   | Operation failed. Cannot connect to service. |
272| 2100003   | System internal error.                       |
273| 2103005   | Failed to read map.                          |
274| 2103011   | Failed to create map.                        |
275| 2103012   | Get iface name failed.                       |
276
277**示例:**
278
279```js
280import { BusinessError } from '@ohos.base';
281import statistics from '@ohos.net.statistics';
282
283statistics.getCellularTxBytes((error: BusinessError, stats: number) => {
284  console.log(JSON.stringify(error));
285  console.log(JSON.stringify(stats));
286});
287```
288
289## statistics.getCellularTxBytes<sup>10+</sup>
290
291getCellularTxBytes(): Promise\<number>;
292
293获取蜂窝实时上行流量,使用 Promise 方式作为异步方法。
294
295**系统能力**:SystemCapability.Communication.NetManager.Core
296
297**返回值:**
298| 类型 | 说明 |
299| -------- | -------- |
300| Promise\<number> | 以 Promise 形式返回获取结果。返回蜂窝实时上行流量(单位:字节)。 |
301
302**错误码:**
303
304以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
305
306| 错误码 ID | 错误信息                                     |
307| --------- | -------------------------------------------- |
308| 2100002   | Operation failed. Cannot connect to service. |
309| 2100003   | System internal error.                       |
310| 2103005   | Failed to read map.                          |
311| 2103011   | Failed to create map.                        |
312| 2103012   | Get iface name failed.                       |
313
314**示例:**
315
316```js
317import statistics from '@ohos.net.statistics';
318
319statistics.getCellularTxBytes().then((stats: number) => {
320  console.log(JSON.stringify(stats));
321});
322```
323
324## statistics.getAllRxBytes<sup>10+</sup>
325
326getAllRxBytes(callback: AsyncCallback\<number>): void;
327
328获取所有网卡实时下行流量,使用 callback 方式作为异步方法。
329
330**系统能力**:SystemCapability.Communication.NetManager.Core
331
332**参数:**
333
334| 参数名   | 类型                   | 必填 | 说明                                                                                                                          |
335| -------- | ---------------------- | ---- | ----------------------------------------------------------------------------------------------------------------------------- |
336| callback | AsyncCallback\<number> | 是   | 回调函数。当成功获取所有网卡实时下行流量,error 为 undefined,stats 为获取到的所有网卡实时下行流量(单位:字节);否则为错误对象 |
337
338**错误码:**
339
340以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
341
342| 错误码 ID | 错误信息                                     |
343| --------- | -------------------------------------------- |
344| 2100002   | Operation failed. Cannot connect to service. |
345| 2100003   | System internal error.                       |
346| 2103005   | Failed to read map.                          |
347| 2103011   | Failed to create map.                        |
348
349**示例:**
350
351```js
352import statistics from '@ohos.net.statistics';
353import { BusinessError } from '@ohos.base';
354
355statistics.getAllRxBytes((error: BusinessError, stats: number) => {
356  console.log(JSON.stringify(error));
357  console.log(JSON.stringify(stats));
358});
359```
360
361## statistics.getAllRxBytes<sup>10+</sup>
362
363getAllRxBytes(): Promise\<number>;
364
365获取所有网卡实时下行流量,使用 Promise 方式作为异步方法。
366
367**系统能力**:SystemCapability.Communication.NetManager.Core
368
369**返回值:**
370| 类型 | 说明 |
371| -------- | -------- |
372| Promise\<number> | 以 Promise 形式返回获取结果。返回所有网卡实时下行流量(单位:字节)。 |
373
374**错误码:**
375
376以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
377
378| 错误码 ID | 错误信息                                     |
379| --------- | -------------------------------------------- |
380| 2100002   | Operation failed. Cannot connect to service. |
381| 2100003   | System internal error.                       |
382| 2103005   | Failed to read map.                          |
383| 2103011   | Failed to create map.                        |
384
385**示例:**
386
387```js
388import statistics from '@ohos.net.statistics';
389
390statistics.getCellularRxBytes().then((stats: number) => {
391  console.log(JSON.stringify(stats));
392});
393```
394
395## statistics.getAllTxBytes<sup>10+</sup>
396
397getAllTxBytes(callback: AsyncCallback\<number>): void;
398
399获取所有网卡实时上行流量,使用 callback 方式作为异步方法。
400
401**系统能力**:SystemCapability.Communication.NetManager.Core
402
403**参数:**
404
405| 参数名   | 类型                   | 必填 | 说明                                                                                                                          |
406| -------- | ---------------------- | ---- | ----------------------------------------------------------------------------------------------------------------------------- |
407| callback | AsyncCallback\<number> | 是   | 回调函数。当成功获取所有网卡实时上行流量,error 为 undefined,stats 为获取到的所有网卡实时上行流量(单位:字节);否则为错误对象 |
408
409**错误码:**
410
411以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
412
413| 错误码 ID | 错误信息                                     |
414| --------- | -------------------------------------------- |
415| 2100002   | Operation failed. Cannot connect to service. |
416| 2100003   | System internal error.                       |
417| 2103005   | Failed to read map.                          |
418| 2103011   | Failed to create map.                        |
419
420**示例:**
421
422```js
423import { BusinessError } from '@ohos.base';
424import statistics from '@ohos.net.statistics';
425
426statistics.getAllTxBytes((error: BusinessError, stats: number) => {
427  console.log(JSON.stringify(error));
428  console.log(JSON.stringify(stats));
429});
430```
431
432## statistics.getAllTxBytes<sup>10+</sup>
433
434getAllTxBytes(): Promise\<number>;
435
436获取所有网卡实时上行流量,使用 Promise 方式作为异步方法。
437
438**系统能力**:SystemCapability.Communication.NetManager.Core
439
440**返回值:**
441| 类型 | 说明 |
442| -------- | -------- |
443| Promise\<number> | 以 Promise 形式返回获取结果。返回所有网卡实时上行流量(单位:字节)。 |
444
445**错误码:**
446
447以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
448
449| 错误码 ID | 错误信息                                     |
450| --------- | -------------------------------------------- |
451| 2100002   | Operation failed. Cannot connect to service. |
452| 2100003   | System internal error.                       |
453| 2103005   | Failed to read map.                          |
454| 2103011   | Failed to create map.                        |
455
456**示例:**
457
458```js
459import statistics from '@ohos.net.statistics';
460
461statistics.getAllTxBytes().then((stats: number) => {
462  console.log(JSON.stringify(stats));
463});
464```
465
466## statistics.getUidRxBytes<sup>10+</sup>
467
468getUidRxBytes(uid: number, callback: AsyncCallback\<number>): void;
469
470获取指定应用实时下行流量,使用 callback 方式作为异步方法。
471
472**系统能力**:SystemCapability.Communication.NetManager.Core
473
474**参数:**
475
476| 参数名   | 类型                   | 必填 | 说明                                                                                                                    |
477| -------- | ---------------------- | ---- | ----------------------------------------------------------------------------------------------------------------------- |
478| uid      | number                 | 是   | 指定查询的应用 uid。                                                                                                    |
479| callback | AsyncCallback\<number> | 是   | 回调函数。当成功获取应用实时下行流量时,error 为 undefined,stats 为获取到的应用实时下行流量(单位:字节);否则为错误对象 |
480
481**错误码:**
482
483以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
484
485| 错误码 ID | 错误信息                                     |
486| --------- | -------------------------------------------- |
487| 401       | Parameter error.                             |
488| 2100002   | Operation failed. Cannot connect to service. |
489| 2100003   | System internal error.                       |
490| 2103005   | Failed to read map.                          |
491| 2103011   | Failed to create map.                        |
492
493**示例:**
494
495```js
496import { BusinessError } from '@ohos.base';
497import statistics from '@ohos.net.statistics';
498
499statistics.getUidRxBytes(20010038, (error: BusinessError, stats: number) => {
500  console.log(JSON.stringify(error));
501  console.log(JSON.stringify(stats));
502});
503```
504
505## statistics.getUidRxBytes<sup>10+</sup>
506
507getUidRxBytes(uid: number): Promise\<number>;
508
509获取指定应用实时下行流量,使用 Promise 方式作为异步方法。
510
511**系统能力**:SystemCapability.Communication.NetManager.Core
512
513**参数:**
514
515| 参数名 | 类型   | 必填 | 说明                 |
516| ------ | ------ | ---- | -------------------- |
517| uid    | number | 是   | 指定查询的应用 uid。 |
518
519**返回值:**
520| 类型 | 说明 |
521| -------- | -------- |
522| Promise\<number> | 以 Promise 形式返回获取结果。返回指定应用实时下行流量(单位:字节)。 |
523
524**错误码:**
525
526以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
527
528| 错误码 ID | 错误信息                                     |
529| --------- | -------------------------------------------- |
530| 401       | Parameter error.                             |
531| 2100002   | Operation failed. Cannot connect to service. |
532| 2100003   | System internal error.                       |
533| 2103005   | Failed to read map.                          |
534| 2103011   | Failed to create map.                        |
535
536**示例:**
537
538```js
539import statistics from '@ohos.net.statistics';
540
541statistics.getUidRxBytes(20010038).then((stats: number) => {
542  console.log(JSON.stringify(stats));
543});
544```
545
546## statistics.getUidTxBytes<sup>10+</sup>
547
548getUidTxBytes(uid: number, callback: AsyncCallback\<number>): void;
549
550获取指定应用实时上行流量,使用 callback 方式作为异步方法。
551
552**系统能力**:SystemCapability.Communication.NetManager.Core
553
554**参数:**
555
556| 参数名   | 类型                   | 必填 | 说明                                                                                                                    |
557| -------- | ---------------------- | ---- | ----------------------------------------------------------------------------------------------------------------------- |
558| uid      | number                 | 是   | 指定查询的应用 uid。                                                                                                    |
559| callback | AsyncCallback\<number> | 是   | 回调函数。当成功获取应用实时上行流量时,error 为 undefined,stats 为获取到的应用实时上行流量(单位:字节);否则为错误对象 |
560
561**错误码:**
562
563以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
564
565| 错误码 ID | 错误信息                                     |
566| --------- | -------------------------------------------- |
567| 401       | Parameter error.                             |
568| 2100002   | Operation failed. Cannot connect to service. |
569| 2100003   | System internal error.                       |
570| 2103005   | Failed to read map.                          |
571| 2103011   | Failed to create map.                        |
572
573**示例:**
574
575```js
576import { BusinessError } from '@ohos.base';
577import statistics from '@ohos.net.statistics';
578
579statistics.getUidTxBytes(20010038, (error: BusinessError, stats: number) => {
580  console.log(JSON.stringify(error));
581  console.log(JSON.stringify(stats));
582});
583```
584
585## statistics.getUidTxBytes<sup>10+</sup>
586
587getUidTxBytes(uid: number): Promise\<number>;
588
589获取指定应用实时上行流量,使用 Promise 方式作为异步方法。
590
591**系统能力**:SystemCapability.Communication.NetManager.Core
592
593**参数:**
594
595| 参数名 | 类型   | 必填 | 说明                 |
596| ------ | ------ | ---- | -------------------- |
597| uid    | number | 是   | 指定查询的应用 uid。 |
598
599**返回值:**
600| 类型 | 说明 |
601| -------- | -------- |
602| Promise\<number> | 以 Promise 形式返回获取结果。返回指定应用实时上行流量(单位:字节)。 |
603
604**错误码:**
605
606以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
607
608| 错误码 ID | 错误信息                                     |
609| --------- | -------------------------------------------- |
610| 401       | Parameter error.                             |
611| 2100002   | Operation failed. Cannot connect to service. |
612| 2100003   | System internal error.                       |
613| 2103005   | Failed to read map.                          |
614| 2103011   | Failed to create map.                        |
615
616**示例:**
617
618```js
619import statistics from '@ohos.net.statistics';
620
621statistics.getUidTxBytes(20010038).then((stats: number) => {
622  console.log(JSON.stringify(stats));
623});
624```
625
626## statistics.on('netStatsChange')<sup>10+</sup>
627
628on(type: 'netStatsChange', callback: Callback\<{ iface: string, uid?: number }>): void
629
630订阅流量改变事件通知。
631
632**系统接口**:此接口为系统接口。
633
634**需要权限**:ohos.permission.GET_NETWORK_STATS
635
636**系统能力**:SystemCapability.Communication.NetManager.Core
637
638**参数:**
639
640| 参数名   | 类型                                        | 必填 | 说明                                                               |
641| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------------ |
642| type     | string                                      | 是   | 订阅事件,固定为'netStatsChange'。                                 |
643| callback | Callback\<{ iface: string, uid?: number }\> | 是   | 当流量有改变时触发回调函数。<br>iface:网卡名称。<br>uid:应用 uid |
644
645**错误码:**
646
647以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
648
649| 错误码 ID | 错误信息                                     |
650| --------- | -------------------------------------------- |
651| 201       | Permission denied.                           |
652| 202       | Non-system applications use system APIs.     |
653| 401       | Parameter error.                             |
654| 2100002   | Operation failed. Cannot connect to service. |
655| 2100003   | System internal error.                       |
656
657**示例:**
658
659```js
660import statistics from '@ohos.net.statistics';
661
662class IFace {
663  iface: string = ""
664  uid?: number = 0
665}
666statistics.on('netStatsChange', (data: IFace) => {
667  console.log('on netStatsChange' + JSON.stringify(data));
668});
669```
670
671## statistics.off('netStatsChange')<sup>10+</sup>
672
673off(type: 'netStatsChange', callback?: Callback\<{ iface: string, uid?: number }>): void;
674
675取消订阅流量改变事件通知。
676
677**系统接口**:此接口为系统接口。
678
679**需要权限**:ohos.permission.GET_NETWORK_STATS
680
681**系统能力**:SystemCapability.Communication.NetManager.Core
682
683**参数:**
684
685| 参数名   | 类型                                        | 必填 | 说明                                                               |
686| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------------ |
687| type     | string                                      | 是   | 注销订阅事件,固定为'netStatsChange'。                             |
688| callback | Callback\<{ iface: string, uid?: number }\> | 否   | 当流量有改变时触发回调函数。<br>iface:网卡名称。<br>uid:应用 uid |
689
690**错误码:**
691
692以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
693
694| 错误码 ID | 错误信息                                     |
695| --------- | -------------------------------------------- |
696| 201       | Permission denied.                           |
697| 202       | Non-system applications use system APIs.     |
698| 401       | Parameter error.                             |
699| 2100002   | Operation failed. Cannot connect to service. |
700| 2100003   | System internal error.                       |
701
702**示例:**
703
704```js
705import statistics from '@ohos.net.statistics';
706
707class IFace {
708  iface: string = ""
709  uid?: number = 0
710}
711let callback: (data: IFace) => void = (data: IFace) => {
712    console.log("on netStatsChange, iFace:" + data.iface + " uid: " + data.uid);
713}
714statistics.on('netStatsChange', callback);
715// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
716statistics.off('netStatsChange', callback);
717statistics.off('netStatsChange');
718```
719
720## statistics.getTrafficStatsByIface<sup>10+</sup>
721
722getTrafficStatsByIface(ifaceInfo: IfaceInfo, callback: AsyncCallback\<NetStatsInfo>): void;
723
724获取指定网卡历史流量信息,使用 callback 方式作为异步方法。
725
726**系统接口**:此接口为系统接口。
727
728**需要权限**:ohos.permission.GET_NETWORK_STATS
729
730**系统能力**:SystemCapability.Communication.NetManager.Core
731
732**参数:**
733
734| 参数名    | 类型                                            | 必填 | 说明                                                                                    |
735| --------- | ----------------------------------------------- | ---- | --------------------------------------------------------------------------------------- |
736| ifaceInfo | [IfaceInfo](#ifaceinfo10)                       | 是   | 指定查询的网卡信息,参见[IfaceInfo](#ifaceinfo10)。                                     |
737| callback  | AsyncCallback\<[NetStatsInfo](#netstatsinfo10)> | 是   | 回调函数。成功时 statsInfo 返回包含网卡历史流量信息,error 为 undefined,否则为错误对象 |
738
739**错误码:**
740
741以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
742
743| 错误码 ID | 错误信息                                     |
744| --------- | -------------------------------------------- |
745| 201       | Permission denied.                           |
746| 202       | Non-system applications use system APIs.     |
747| 401       | Parameter error.                             |
748| 2100001   | Invalid parameter value.                     |
749| 2100002   | Operation failed. Cannot connect to service. |
750| 2100003   | System internal error.                       |
751| 2103017   | Read data from database failed.              |
752
753**示例:**
754
755```js
756import { BusinessError } from '@ohos.base';
757import statistics from '@ohos.net.statistics';
758
759let iFaceInfo: statistics.IfaceInfo
760
761statistics.getTrafficStatsByIface(iFaceInfo, (error: BusinessError, statsInfo: statistics.NetStatsInfo) => {
762  console.log(JSON.stringify(error));
763  console.log(
764    "getTrafficStatsByIface bytes of received = " +
765    JSON.stringify(statsInfo.rxBytes)
766  );
767  console.log(
768    "getTrafficStatsByIface bytes of sent = " +
769    JSON.stringify(statsInfo.txBytes)
770  );
771  console.log(
772    "getTrafficStatsByIface packets of received = " +
773    JSON.stringify(statsInfo.rxPackets)
774  );
775  console.log(
776    "getTrafficStatsByIface packets of sent = " +
777    JSON.stringify(statsInfo.txPackets)
778  );
779});
780```
781
782## statistics.getTrafficStatsByIface<sup>10+</sup>
783
784getTrafficStatsByIface(ifaceInfo: IfaceInfo): Promise\<NetStatsInfo>;
785
786获取指定网卡历史流量信息,使用 Promise 方式作为异步方法。
787
788**系统接口**:此接口为系统接口。
789
790**需要权限**:ohos.permission.GET_NETWORK_STATS
791
792**系统能力**:SystemCapability.Communication.NetManager.Core
793
794| 参数名    | 类型                      | 必填 | 说明                                                |
795| --------- | ------------------------- | ---- | --------------------------------------------------- |
796| ifaceInfo | [IfaceInfo](#ifaceinfo10) | 是   | 指定查询的网卡信息,参见[IfaceInfo](#ifaceinfo10)。 |
797
798**返回值:**
799| 类型 | 说明 |
800| -------- | -------- |
801| Promise\<[NetStatsInfo](#netstatsinfo10)> | 以 Promise 形式返回获取结果,返回网卡历史流量信息。 |
802
803**错误码:**
804
805以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
806
807| 错误码 ID | 错误信息                                     |
808| --------- | -------------------------------------------- |
809| 201       | Permission denied.                           |
810| 202       | Non-system applications use system APIs.     |
811| 401       | Parameter error.                             |
812| 2100001   | Invalid parameter value.                     |
813| 2100002   | Operation failed. Cannot connect to service. |
814| 2100003   | System internal error.                       |
815| 2103017   | Read data from database failed.              |
816
817**示例:**
818
819```js
820import statistics from '@ohos.net.statistics';
821
822let iFaceInfo: statistics.IfaceInfo
823statistics.getTrafficStatsByIface(iFaceInfo).then((statsInfo: statistics.NetStatsInfo) => {
824  console.log(
825    "getTrafficStatsByIface bytes of received = " +
826    JSON.stringify(statsInfo.rxBytes)
827  );
828  console.log(
829    "getTrafficStatsByIface bytes of sent = " +
830    JSON.stringify(statsInfo.txBytes)
831  );
832  console.log(
833    "getTrafficStatsByIface packets of received = " +
834    JSON.stringify(statsInfo.rxPackets)
835  );
836  console.log(
837    "getTrafficStatsByIface packets of sent = " +
838    JSON.stringify(statsInfo.txPackets)
839  );
840});
841```
842
843## statistics.getTrafficStatsByUid<sup>10+</sup>
844
845getTrafficStatsByUid(uidInfo: UidInfo, callback: AsyncCallback\<NetStatsInfo>): void;
846
847获取指定应用历史流量信息,使用 callback 方式作为异步方法。
848
849**系统接口**:此接口为系统接口。
850
851**需要权限**:ohos.permission.GET_NETWORK_STATS
852
853**系统能力**:SystemCapability.Communication.NetManager.Core
854
855**参数:**
856
857| 参数名   | 类型                                            | 必填 | 说明                                                                                    |
858| -------- | ----------------------------------------------- | ---- | --------------------------------------------------------------------------------------- |
859| uidInfo  | [UidInfo](#uidinfo10)                           | 是   | 指定查询的应用信息,参见[UidInfo](#uidinfo10)。                                         |
860| callback | AsyncCallback\<[NetStatsInfo](#netstatsinfo10)> | 是   | 回调函数。成功时 statsInfo 返回包含应用历史流量信息,error 为 undefined,否则为错误对象 |
861
862**错误码:**
863
864以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
865
866| 错误码 ID | 错误信息                                     |
867| --------- | -------------------------------------------- |
868| 201       | Permission denied.                           |
869| 202       | Non-system applications use system APIs.     |
870| 401       | Parameter error.                             |
871| 2100001   | Invalid parameter value.                     |
872| 2100002   | Operation failed. Cannot connect to service. |
873| 2100003   | System internal error.                       |
874| 2103017   | Read data from database failed.              |
875
876**示例:**
877
878```js
879import { BusinessError } from '@ohos.base';
880import statistics from '@ohos.net.statistics';
881
882let uidInfo: statistics.UidInfo
883uidInfo.uid = 20010037
884
885statistics.getTrafficStatsByUid(
886  uidInfo,
887  (error: BusinessError, statsInfo: statistics.NetStatsInfo) => {
888    console.log(JSON.stringify(error));
889    console.log(
890      "getTrafficStatsByUid bytes of received = " +
891      JSON.stringify(statsInfo.rxBytes)
892    );
893    console.log(
894      "getTrafficStatsByUid bytes of sent = " +
895      JSON.stringify(statsInfo.txBytes)
896    );
897    console.log(
898      "getTrafficStatsByUid packets of received = " +
899      JSON.stringify(statsInfo.rxPackets)
900    );
901    console.log(
902      "getTrafficStatsByUid packets of sent = " +
903      JSON.stringify(statsInfo.txPackets)
904    );
905  }
906);
907```
908
909## statistics.getTrafficStatsByUid<sup>10+</sup>
910
911getTrafficStatsByUid(uidInfo: UidInfo): Promise\<NetStatsInfo>;
912
913获取指定应用历史流量信息,使用 Promise 方式作为异步方法。
914
915**系统接口**:此接口为系统接口。
916
917**需要权限**:ohos.permission.GET_NETWORK_STATS
918
919**系统能力**:SystemCapability.Communication.NetManager.Core
920
921**参数:**
922
923| 参数名  | 类型                  | 必填 | 说明                                            |
924| ------- | --------------------- | ---- | ----------------------------------------------- |
925| uidInfo | [UidInfo](#uidinfo10) | 是   | 指定查询的应用信息,参见[UidInfo](#uidinfo10)。 |
926
927**返回值:**
928
929| 类型                                      | 说明                                               |
930| ----------------------------------------- | -------------------------------------------------- |
931| Promise\<[NetStatsInfo](#netstatsinfo10)> | 以 Promise 形式返回获取结果,返回应用历史流量信息。 |
932
933**错误码:**
934
935以下错误码的详细介绍参见[statistics 错误码](../errorcodes/errorcode-net-statistics.md)。
936
937| 错误码 ID | 错误信息                                     |
938| --------- | -------------------------------------------- |
939| 201       | Permission denied.                           |
940| 202       | Non-system applications use system APIs.     |
941| 401       | Parameter error.                             |
942| 2100001   | Invalid parameter value.                     |
943| 2100002   | Operation failed. Cannot connect to service. |
944| 2100003   | System internal error.                       |
945| 2103017   | Read data from database failed.              |
946
947**示例:**
948
949```js
950import statistics from '@ohos.net.statistics'
951
952let uidInfo: statistics.UidInfo
953uidInfo.uid = 20010037
954
955statistics.getTrafficStatsByUid(uidInfo).then((statsInfo: statistics.NetStatsInfo) => {
956  console.log("getTrafficStatsByUid bytes of received = " + JSON.stringify(statsInfo.rxBytes));
957  console.log("getTrafficStatsByUid bytes of sent = " + JSON.stringify(statsInfo.txBytes));
958  console.log("getTrafficStatsByUid packets of received = " + JSON.stringify(statsInfo.rxPackets));
959  console.log("getTrafficStatsByUid packets of sent = " + JSON.stringify(statsInfo.txPackets));
960})
961```
962
963## IfaceInfo<sup>10+</sup>
964
965查询网卡历史流量参数信息。
966
967**系统接口**:此接口为系统接口。
968
969**系统能力**:SystemCapability.Communication.NetManager.Core
970
971| 名称      | 类型   | 必填 | 说明                              |
972| --------- | ------ | ---- | --------------------------------- |
973| iface     | string | 是   | 查询的网卡名。                    |
974| startTime | number | 是   | 查询的开始时间(时间戳;单位:秒)。 |
975| endTime   | number | 是   | 查询的结束时间(时间戳;单位:秒)。 |
976
977## UidInfo<sup>10+</sup>
978
979查询应用历史流量参数信息。
980
981**系统接口**:此接口为系统接口。
982
983**系统能力**:SystemCapability.Communication.NetManager.Core
984
985| 名称      | 类型                                  | 必填 | 说明                       |
986| --------- | ------------------------------------- | ---- | -------------------------- |
987| ifaceInfo | IfaceInfo\<[IfaceInfo](#ifaceinfo10)> | 是   | 需查询的网卡和时间参数信息 |
988| uid       | number                                | 是   | 需查询的应用 uid           |
989
990## NetStatsInfo<sup>10+</sup>
991
992获取的历史流量信息。
993
994**系统接口**:此接口为系统接口。
995
996**系统能力**:SystemCapability.Communication.NetManager.Core
997
998| 名称      | 类型   | 必填 | 说明                    |
999| --------- | ------ | ---- | ----------------------- |
1000| rxBytes   | number | 是   | 流量下行数据(单位:字节) |
1001| txBytes   | number | 是   | 流量上行数据(单位:字节) |
1002| rxPackets | number | 是   | 流量下行包个数          |
1003| txPackets | number | 是   | 流量上行包个数          |
1004