• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.net.policy (Network Policy Management)
2
3The **policy** module provides APIs for managing network policies, through which you can control and manage the data volume used.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```js
12import policy from '@ohos.net.policy'
13```
14
15## policy.setBackgroundPolicy
16
17setBackgroundPolicy(isAllowed: boolean, callback: AsyncCallback\<void>): void
18
19Sets a background network policy. This API uses an asynchronous callback to return the result.
20
21**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
22
23**System capability**: SystemCapability.Communication.NetManager.Core
24
25**Parameters**
26
27| Name  | Type                                   | Mandatory| Description      |
28| -------- | --------------------------------------- | ---- | ---------- |
29| isAllowed | boolean | Yes  | Whether applications running in the background are allowed to use mobile data.|
30| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
31
32**Error codes**
33
34| ID| Error Message                                     |
35| ------- | -------------------------------------------- |
36| 201     | Permission denied.                           |
37| 401     | Parameter error.                             |
38| 2100001 | Invalid parameter value.                |
39| 2100002 | Operation failed. Cannot connect to service.|
40| 2100003 | System internal error.                  |
41
42**Example**
43
44```js
45policy.setBackgroundPolicy(Boolean(Number.parseInt(this.isBoolean))), (error, data) => {
46  this.callBack(error, data);
47  console.log(JSON.stringify(error))
48  console.log(JSON.stringify(data))
49}
50)
51;
52```
53
54## policy.setBackgroundPolicy
55
56setBackgroundPolicy(isAllowed: boolean): Promise\<void>
57
58Sets a background network policy. This API uses a promise to return the result.
59
60**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
61
62**System capability**: SystemCapability.Communication.NetManager.Core
63
64**Parameters**
65
66| Name  | Type                                   | Mandatory| Description      |
67| -------- | --------------------------------------- | ---- | ---------- |
68| isAllowed | boolean | Yes  | Whether applications running in the background are allowed to use mobile data.|
69
70**Error codes**
71
72| ID| Error Message                                     |
73| ------- | -------------------------------------------- |
74| 201     | Permission denied.                           |
75| 401     | Parameter error.                             |
76| 2100001 | Invalid parameter value.                |
77| 2100002 | Operation failed. Cannot connect to service.|
78| 2100003 | System internal error.                  |
79
80**Return value**
81
82| Type                             | Description                                 |
83| --------------------------------- | ------------------------------------- |
84| Promise\<void> | Promise used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
85
86**Example**
87
88```js
89policy.setBackgroundPolicy(Boolean(Number.parseInt(this.isBoolean))).then(function (error, data) {
90  console.log(JSON.stringify(error))
91  console.log(JSON.stringify(data))
92})
93```
94
95## policy.isBackgroundAllowed
96
97isBackgroundAllowed(callback: AsyncCallback\<boolean>): void
98
99Obtains the background network policy. This API uses an asynchronous callback to return the result.
100
101**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
102
103**System capability**: SystemCapability.Communication.NetManager.Core
104
105**Parameters**
106
107| Name  | Type                                   | Mandatory| Description      |
108| -------- | --------------------------------------- | ---- | ---------- |
109| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. If the operation is successful, **true** is returned, which means that applications running in the background are allowed to use mobile data. If the operation fails, an error message is returned.|
110
111**Error codes**
112
113| ID| Error Message                                     |
114| ------- | -------------------------------------------- |
115| 201     | Permission denied.                           |
116| 2100002 | Operation failed. Cannot connect to service.|
117| 2100003 | System internal error.                  |
118
119**Example**
120
121```js
122policy.isBackgroundAllowed((error, data) => {
123  this.callBack(error, data);
124  console.log(JSON.stringify(error))
125  console.log(JSON.stringify(data))
126});
127```
128
129## policy.isBackgroundAllowed
130
131isBackgroundAllowed(): Promise\<boolean>;
132
133Obtains the background network policy. This API uses a promise to return the result.
134
135**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
136
137**System capability**: SystemCapability.Communication.NetManager.Core
138
139**Return value**
140
141| Type                             | Description                                 |
142| --------------------------------- | ------------------------------------- |
143| Promise\<boolean> | Promise used to return the result. If the operation is successful, **true** is returned, which means that applications running in the background are allowed to use mobile data. If the operation fails, an error message is returned.|
144
145**Error codes**
146
147| ID| Error Message                                     |
148| ------- | -------------------------------------------- |
149| 201     | Permission denied.                           |
150| 2100002 | Operation failed. Cannot connect to service.|
151| 2100003 | System internal error.                  |
152
153**Example**
154
155```js
156policy.isBackgroundAllowed().then(function (error, data) {
157  console.log(JSON.stringify(error))
158  console.log(JSON.stringify(data))
159})
160
161```
162
163## policy.setPolicyByUid
164
165setPolicyByUid(uid: number, policy: NetUidPolicy, callback: AsyncCallback\<void>): void
166
167Sets an application-specific network policy. This API uses an asynchronous callback to return the result.
168
169**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
170
171**System capability**: SystemCapability.Communication.NetManager.Core
172
173**Parameters**
174
175| Name  | Type                                   | Mandatory| Description      |
176| -------- | --------------------------------------- | ---- | ---------- |
177| uid | number | Yes  | Unique ID of the application.|
178| policy | [NetUidPolicy](#netuidpolicy) | Yes| Application-specific network policy to set.|
179| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
180
181**Error codes**
182
183| ID| Error Message                                     |
184| ------- | -------------------------------------------- |
185| 201     | Permission denied.                           |
186| 401     | Parameter error.                             |
187| 2100001 | Invalid parameter value.                |
188| 2100002 | Operation failed. Cannot connect to service.|
189| 2100003 | System internal error.                  |
190
191**Example**
192
193```js
194let param = {
195  uid: Number.parseInt(this.firstParam), policy: Number.parseInt(this.currentNetUidPolicy)
196}
197policy.setPolicyByUid(Number.parseInt(this.firstParam), Number.parseInt(this.currentNetUidPolicy), (error, data) => {
198  this.callBack(error, data);
199});
200```
201
202## policy.setPolicyByUid
203
204setPolicyByUid(uid: number, policy: NetUidPolicy): Promise\<void>;
205
206Sets an application-specific network policy. This API uses a promise to return the result.
207
208**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
209
210**System capability**: SystemCapability.Communication.NetManager.Core
211
212**Parameters**
213
214| Name  | Type                                   | Mandatory| Description      |
215| -------- | --------------------------------------- | ---- | ---------- |
216| uid | number | Yes  | Unique ID of the application.|
217| policy | [NetUidPolicy](#netuidpolicy) | Yes| Application-specific network policy to set.|
218
219**Return value**
220
221| Type                             | Description                                 |
222| --------------------------------- | ------------------------------------- |
223| Promise\<void> | Promise used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
224
225**Error codes**
226
227| ID| Error Message                                     |
228| ------- | -------------------------------------------- |
229| 201     | Permission denied.                           |
230| 401     | Parameter error.                             |
231| 2100001 | Invalid parameter value.                |
232| 2100002 | Operation failed. Cannot connect to service.|
233| 2100003 | System internal error.                  |
234
235**Example**
236
237```js
238let param = {
239  uid: Number.parseInt(this.firstParam), policy: Number.parseInt(this.currentNetUidPolicy)
240}
241policy.setPolicyByUid(Number.parseInt(this.firstParam), Number.parseInt(this.currentNetUidPolicy)).then(function (error, data) {
242  console.log(JSON.stringify(error))
243  console.log(JSON.stringify(data))
244})
245
246```
247
248## policy.getPolicyByUid
249
250getPolicyByUid(uid: number, callback: AsyncCallback\<NetUidPolicy>): void
251
252Obtains an application-specific network policy by **uid**. This API uses an asynchronous callback to return the result.
253
254**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
255
256**System capability**: SystemCapability.Communication.NetManager.Core
257
258**Parameters**
259
260| Name  | Type                                   | Mandatory| Description      |
261| -------- | --------------------------------------- | ---- | ---------- |
262| uid | number | Yes| Unique ID of the application.|
263| callback | AsyncCallback\<[NetUidPolicy](#netuidpolicy)> | Yes  | Callback used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
264
265**Error codes**
266
267| ID| Error Message                                     |
268| ------- | -------------------------------------------- |
269| 201     | Permission denied.                           |
270| 401     | Parameter error.                             |
271| 2100001 | Invalid parameter value.                |
272| 2100002 | Operation failed. Cannot connect to service.|
273| 2100003 | System internal error.                  |
274
275**Example**
276
277```js
278policy.getPolicyByUid(Number.parseInt(this.firstParam), (error, data) => {
279  this.callBack(error, data);
280});
281```
282
283## policy.getPolicyByUid
284
285getPolicyByUid(uid: number): Promise\<NetUidPolicy>;
286
287Obtains an application-specific network policy by **uid**. This API uses a promise to return the result.
288
289**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
290
291**System capability**: SystemCapability.Communication.NetManager.Core
292
293**Parameters**
294
295| Name  | Type                                   | Mandatory| Description      |
296| -------- | --------------------------------------- | ---- | ---------- |
297| uid | number | Yes| Unique ID of the application.|
298
299**Return value**
300
301| Type                             | Description                                 |
302| --------------------------------- | ------------------------------------- |
303| Promise\<[NetUidPolicy](#netuidpolicy)> | Promise used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
304
305**Error codes**
306
307| ID| Error Message                                     |
308| ------- | -------------------------------------------- |
309| 201     | Permission denied.                           |
310| 401     | Parameter error.                             |
311| 2100001 | Invalid parameter value.                |
312| 2100002 | Operation failed. Cannot connect to service.|
313| 2100003 | System internal error.                  |
314
315**Example**
316
317```js
318policy.getPolicyByUid(Number.parseInt(this.firstParam)).then(function (error, data) {
319  console.log(JSON.stringify(error))
320  console.log(JSON.stringify(data))
321})
322
323```
324
325## policy.getUidsByPolicy
326
327getUidsByPolicy(policy: NetUidPolicy, callback: AsyncCallback\<Array\<number>>): void
328
329Obtains the UID array of applications configured with a certain application-specific network policy. This API uses an asynchronous callback to return the result.
330
331**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
332
333**System capability**: SystemCapability.Communication.NetManager.Core
334
335**Parameters**
336
337| Name  | Type                                   | Mandatory| Description      |
338| -------- | --------------------------------------- | ---- | ---------- |
339| policy | [NetUidPolicy](#netuidpolicy) | Yes| Target application-specific network policy.|
340| callback | AsyncCallback\<Array\<number>> | Yes  | Callback used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
341
342**Error codes**
343
344| ID| Error Message                                     |
345| ------- | -------------------------------------------- |
346| 201     | Permission denied.                           |
347| 401     | Parameter error.                             |
348| 2100001 | Invalid parameter value.                |
349| 2100002 | Operation failed. Cannot connect to service.|
350| 2100003 | System internal error.                  |
351
352**Example**
353
354```js
355policy.getUidsByPolicy(Number.parseInt(this.currentNetUidPolicy), (error, data) => {
356  this.callBack(error, data);
357});
358```
359
360## policy.getUidsByPolicy
361
362function getUidsByPolicy(policy: NetUidPolicy): Promise\<Array\<number>>;
363
364Obtains the UID array of applications configured with a certain application-specific network policy. This API uses a promise to return the result.
365
366**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
367
368**System capability**: SystemCapability.Communication.NetManager.Core
369
370**Parameters**
371
372| Name  | Type                                   | Mandatory| Description      |
373| -------- | --------------------------------------- | ---- | ---------- |
374| policy | [NetUidPolicy](#netuidpolicy) | Yes| Target application-specific network policy.|
375
376**Return value**
377
378| Type                             | Description                                 |
379| --------------------------------- | ------------------------------------- |
380| Promise\<Array\<number>> | Promise used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
381
382**Error codes**
383
384| ID| Error Message                                     |
385| ------- | -------------------------------------------- |
386| 201     | Permission denied.                           |
387| 401     | Parameter error.                             |
388| 2100001 | Invalid parameter value.                |
389| 2100002 | Operation failed. Cannot connect to service.|
390| 2100003 | System internal error.                  |
391
392**Example**
393
394```js
395policy.getUidsByPolicy(Number.parseInt(this.firstParam)).then(function (error, data) {
396  console.log(JSON.stringify(error))
397  console.log(JSON.stringify(data))
398})
399
400```
401
402## policy.getNetQuotaPolicies
403
404getNetQuotaPolicies(callback: AsyncCallback\<Array\<NetQuotaPolicy>>): void
405
406Obtains the network quota policies. This API uses an asynchronous callback to return the result.
407
408**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
409
410**System capability**: SystemCapability.Communication.NetManager.Core
411
412**Parameters**
413
414| Name  | Type                                   | Mandatory| Description      |
415| -------- | --------------------------------------- | ---- | ---------- |
416| callback | AsyncCallback\<Array\<[NetQuotaPolicy](#netquotapolicy)>> | Yes  | Callback used to return the result.|
417
418**Error codes**
419
420| ID| Error Message                                     |
421| ------- | -------------------------------------------- |
422| 201     | Permission denied.                           |
423| 2100002 | Operation failed. Cannot connect to service.|
424| 2100003 | System internal error.                  |
425
426**Example**
427
428```js
429policy.getNetQuotaPolicies((error, data) => {
430  this.callBack(error, data);
431});
432```
433
434## policy.getNetQuotaPolicies
435
436getNetQuotaPolicies(): Promise\<Array\<NetQuotaPolicy>>;
437
438Obtains the network quota policies. This API uses a promise to return the result.
439
440**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
441
442**System capability**: SystemCapability.Communication.NetManager.Core
443
444**Return value**
445
446| Type                             | Description                                 |
447| --------------------------------- | ------------------------------------- |
448| Promise\<Array\<[NetQuotaPolicy](#netquotapolicy)>> | Promise used to return the result.|
449
450**Error codes**
451
452| ID| Error Message                                     |
453| ------- | -------------------------------------------- |
454| 201     | Permission denied.                           |
455| 2100002 | Operation failed. Cannot connect to service.|
456| 2100003 | System internal error.                  |
457
458**Example**
459
460```js
461policy.getNetQuotaPolicies().then(function (error, data) {
462  console.log(JSON.stringify(error))
463  console.log(JSON.stringify(data))
464})
465
466```
467
468## policy.setNetQuotaPolicies
469
470setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>, callback: AsyncCallback\<void>): void
471
472Sets an array of network quota policies. This API uses an asynchronous callback to return the result.
473
474**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
475
476**System capability**: SystemCapability.Communication.NetManager.Core
477
478**Parameters**
479
480| Name  | Type                                   | Mandatory| Description      |
481| -------- | --------------------------------------- | ---- | ---------- |
482| quotaPolicies | Array\<[NetQuotaPolicy](#netquotapolicy)> | Yes| An array of network quota policies to set.|
483| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
484
485**Error codes**
486
487| ID| Error Message                                     |
488| ------- | -------------------------------------------- |
489| 201     | Permission denied.                           |
490| 401     | Parameter error.                             |
491| 2100001 | Invalid parameter value.                |
492| 2100002 | Operation failed. Cannot connect to service.|
493| 2100003 | System internal error.                  |
494
495**Example**
496
497```js
498let param = {
499  netType: Number.parseInt(this.netType),
500  iccid: this.iccid,
501  ident: this.ident,
502  periodDuration: this.periodDuration,
503  warningBytes: Number.parseInt(this.warningBytes),
504  limitBytes: Number.parseInt(this.limitBytes),
505  lastWarningRemind: this.lastWarningRemind,
506  lastLimitRemind: this.lastLimitRemind,
507  metered: Boolean(Number.parseInt(this.metered)),
508  limitAction: this.limitAction
509};
510this.netQuotaPolicyList.push(param);
511
512policy.setNetQuotaPolicies(this.netQuotaPolicyList, (error, data) => {
513  this.callBack(error, data);
514});
515```
516
517## policy.setNetQuotaPolicies
518
519setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>): Promise\<void>;
520
521Sets an array of network quota policies. This API uses a promise to return the result.
522
523**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
524
525**System capability**: SystemCapability.Communication.NetManager.Core
526
527**Parameters**
528
529| Name  | Type                                   | Mandatory| Description      |
530| -------- | --------------------------------------- | ---- | ---------- |
531| quotaPolicies | Array\<[NetQuotaPolicy](#netquotapolicy)> | Yes| An array of network quota policies to set.|
532
533**Error codes**
534
535| ID| Error Message                                     |
536| ------- | -------------------------------------------- |
537| 201     | Permission denied.                           |
538| 401     | Parameter error.                             |
539| 2100001 | Invalid parameter value.                |
540| 2100002 | Operation failed. Cannot connect to service.|
541| 2100003 | System internal error.                  |
542
543**Return value**
544
545| Type                             | Description                                 |
546| --------------------------------- | ------------------------------------- |
547| Promise\<void> | Promise used to return the result.|
548
549**Example**
550
551```js
552let param = {
553  netType: Number.parseInt(this.netType),
554  iccid: this.iccid,
555  ident: this.ident,
556  periodDuration: this.periodDuration,
557  warningBytes: Number.parseInt(this.warningBytes),
558  limitBytes: Number.parseInt(this.limitBytes),
559  lastWarningRemind: this.lastWarningRemind,
560  lastLimitRemind: this.lastLimitRemind,
561  metered: Boolean(Number.parseInt(this.metered)),
562  limitAction: this.limitAction
563};
564this.netQuotaPolicyList.push(param);
565
566policy.setNetQuotaPolicies(this.netQuotaPolicyList).then(function (error, data) {
567  console.log(JSON.stringify(error))
568  console.log(JSON.stringify(data))
569})
570```
571
572## policy.restoreAllPolicies
573
574restoreAllPolicies(iccid: string, callback: AsyncCallback\<void>): void
575
576Restores all the policies (cellular network, background network, firewall, and application-specific network policies) for the given SIM card. This API uses an asynchronous callback to return the result.
577
578**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
579
580**System capability**: SystemCapability.Communication.NetManager.Core
581
582**Parameters**
583
584| Name  | Type                                   | Mandatory| Description      |
585| -------- | --------------------------------------- | ---- | ---------- |
586| iccid | string | Yes| SIM card ID.|
587| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
588
589**Error codes**
590
591| ID| Error Message                                     |
592| ------- | -------------------------------------------- |
593| 201     | Permission denied.                           |
594| 401     | Parameter error.                             |
595| 2100001 | Invalid parameter value.                |
596| 2100002 | Operation failed. Cannot connect to service.|
597| 2100003 | System internal error.                  |
598
599**Example**
600
601```js
602this.firstParam = iccid;
603policy.restoreAllPolicies(this.firstParam, (error, data) => {
604  this.callBack(error, data);
605});
606```
607
608## policy.restoreAllPolicies
609
610restoreAllPolicies(iccid: string): Promise\<void>;
611
612Restores all the policies (cellular network, background network, firewall, and application-specific network policies) for the given SIM card. This API uses a promise to return the result.
613
614**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
615
616**System capability**: SystemCapability.Communication.NetManager.Core
617
618**Parameters**
619
620| Name  | Type                                   | Mandatory| Description      |
621| -------- | --------------------------------------- | ---- | ---------- |
622| iccid | string | Yes| SIM card ID.|
623
624**Return value**
625
626| Type                             | Description                                 |
627| --------------------------------- | ------------------------------------- |
628| Promise\<void> | Promise used to return the result.|
629
630**Error codes**
631
632| ID| Error Message                                     |
633| ------- | -------------------------------------------- |
634| 201     | Permission denied.                           |
635| 401     | Parameter error.                             |
636| 2100001 | Invalid parameter value.                |
637| 2100002 | Operation failed. Cannot connect to service.|
638| 2100003 | System internal error.                  |
639
640**Example**
641
642```js
643this.firstParam = iccid;
644policy.restoreAllPolicies(this.firstParam).then(function (error, data) {
645  console.log(JSON.stringify(error))
646  console.log(JSON.stringify(data))
647})
648
649```
650
651## policy.isUidNetAllowed
652
653isUidNetAllowed(uid: number, isMetered: boolean, callback: AsyncCallback\<boolean>): void
654
655Checks whether an application is allowed to access metered networks. This API uses an asynchronous callback to return the result.
656
657**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
658
659**System capability**: SystemCapability.Communication.NetManager.Core
660
661**Parameters**
662
663| Name  | Type                                   | Mandatory| Description      |
664| -------- | --------------------------------------- | ---- | ---------- |
665| uid | number | Yes| Unique ID of the application.|
666| isMetered | boolean | Yes| Whether the network is a metered network.|
667| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. The value **true** means that the application is allowed to access metered networks, and **false** means the opposite.|
668
669**Error codes**
670
671| ID| Error Message                                     |
672| ------- | -------------------------------------------- |
673| 201     | Permission denied.                           |
674| 401     | Parameter error.                             |
675| 2100001 | Invalid parameter value.                |
676| 2100002 | Operation failed. Cannot connect to service.|
677| 2100003 | System internal error.                  |
678
679**Example**
680
681```js
682
683let param = {
684  uid: Number.parseInt(this.firstParam), isMetered: Boolean(Number.parseInt(this.isBoolean))
685}
686policy.isUidNetAllowed(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean)), (error, data) => {
687  this.callBack(error, data);
688});
689```
690
691## policy.isUidNetAllowed
692
693isUidNetAllowed(uid: number, isMetered: boolean): Promise\<boolean>;
694
695Checks whether an application is allowed to access metered networks. This API uses a promise to return the result.
696
697**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
698
699**System capability**: SystemCapability.Communication.NetManager.Core
700
701**Parameters**
702
703| Name  | Type                                   | Mandatory| Description      |
704| -------- | --------------------------------------- | ---- | ---------- |
705| uid | number | Yes| Unique ID of the application.|
706| isMetered | boolean | Yes| Whether the network is a metered network.|
707
708**Return value**
709
710| Type                             | Description                                 |
711| --------------------------------- | ------------------------------------- |
712| Promise\<boolean> | Promise used to return the result.|
713
714**Error codes**
715
716| ID| Error Message                                     |
717| ------- | -------------------------------------------- |
718| 201     | Permission denied.                           |
719| 401     | Parameter error.                             |
720| 2100001 | Invalid parameter value.                |
721| 2100002 | Operation failed. Cannot connect to service.|
722| 2100003 | System internal error.                  |
723
724**Example**
725
726```js
727
728let param = {
729  uid: Number.parseInt(this.firstParam), isMetered: Boolean(Number.parseInt(this.isBoolean))
730}
731policy.isUidNetAllowed(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then(function (error, data) {
732  console.log(JSON.stringify(error))
733  console.log(JSON.stringify(data))
734})
735
736```
737
738## policy.isUidNetAllowed
739
740isUidNetAllowed(uid: number, iface: string, callback: AsyncCallback\<boolean>): void
741
742Checks whether an application is allowed to access the given network. This API uses an asynchronous callback to return the result.
743
744**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
745
746**System capability**: SystemCapability.Communication.NetManager.Core
747
748**Parameters**
749
750| Name  | Type                                   | Mandatory| Description      |
751| -------- | --------------------------------------- | ---- | ---------- |
752| uid | number | Yes| Unique ID of the application.|
753| iface | string | Yes| Name of the target network.|
754| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. The value **true** means that the application is allowed to access the given network, and **false** means the opposite.|
755
756**Error codes**
757
758| ID| Error Message                                     |
759| ------- | -------------------------------------------- |
760| 201     | Permission denied.                           |
761| 401     | Parameter error.                             |
762| 2100001 | Invalid parameter value.                |
763| 2100002 | Operation failed. Cannot connect to service.|
764| 2100003 | System internal error.                  |
765
766**Example**
767
768```js
769
770let param = {
771  uid: Number.parseInt(this.firstParam), iface: this.secondParam
772}
773policy.isUidNetAllowed(Number.parseInt(this.firstParam), this.secondParam, (error, data) => {
774  this.callBack(error, data);
775});
776```
777
778## policy.isUidNetAllowed
779
780isUidNetAllowed(uid: number, iface: string): Promise\<boolean>;
781
782Checks whether an application is allowed to access the given network. This API uses a promise to return the result.
783
784**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
785
786**System capability**: SystemCapability.Communication.NetManager.Core
787
788**Parameters**
789
790| Name  | Type                                   | Mandatory| Description      |
791| -------- | --------------------------------------- | ---- | ---------- |
792| uid | number | Yes| Unique ID of the application.|
793| iface | string | Yes| Name of the target network.|
794
795**Return value**
796
797| Type                             | Description                                 |
798| --------------------------------- | ------------------------------------- |
799| Promise\<boolean> | Promise used to return the result.|
800
801**Error codes**
802
803| ID| Error Message                                     |
804| ------- | -------------------------------------------- |
805| 201     | Permission denied.                           |
806| 401     | Parameter error.                             |
807| 2100001 | Invalid parameter value.                |
808| 2100002 | Operation failed. Cannot connect to service.|
809| 2100003 | System internal error.                  |
810
811**Example**
812
813```js
814let param = {
815  uid: Number.parseInt(this.firstParam), iface: this.secondParam
816}
817policy.isUidNetAllowed(Number.parseInt(this.firstParam), this.secondParam).then(function (error, data) {
818  console.log(JSON.stringify(error))
819  console.log(JSON.stringify(data))
820})
821
822```
823
824## policy.setDeviceIdleAllowList
825
826setDeviceIdleAllowList(uid: number, isAllowed: boolean, callback: AsyncCallback\<void>): void
827
828Sets whether to add an application to the device idle allowlist. This API uses an asynchronous callback to return the result.
829
830**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
831
832**System capability**: SystemCapability.Communication.NetManager.Core
833
834**Parameters**
835
836| Name  | Type                                   | Mandatory| Description      |
837| -------- | --------------------------------------- | ---- | ---------- |
838| uid | number | Yes| Unique ID of the application.|
839| isAllowed | boolean | Yes| Whether to add the application to the allowlist.|
840| callback | callback: AsyncCallback\<void> | Yes  | Callback used to return the result.|
841
842**Error codes**
843
844| ID| Error Message                                     |
845| ------- | -------------------------------------------- |
846| 201     | Permission denied.                           |
847| 401     | Parameter error.                             |
848| 2100001 | Invalid parameter value.                |
849| 2100002 | Operation failed. Cannot connect to service.|
850| 2100003 | System internal error.                  |
851
852**Example**
853
854```js
855let param = {
856  uid: Number.parseInt(this.firstParam), isAllowed: Boolean(Number.parseInt(this.isBoolean))
857}
858policy.setDeviceIdleAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean)), (error, data) => {
859  this.callBack(error, data);
860});
861```
862
863## policy.setDeviceIdleAllowList
864
865setDeviceIdleAllowList(uid: number, isAllowed: boolean): Promise\<void>;
866
867Sets whether to add an application to the device idle allowlist. This API uses a promise to return the result.
868
869**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
870
871**System capability**: SystemCapability.Communication.NetManager.Core
872
873**Parameters**
874
875| Name  | Type                                   | Mandatory| Description      |
876| -------- | --------------------------------------- | ---- | ---------- |
877| uid | number | Yes| Unique ID of the application.|
878| isAllowed | boolean | Yes| Whether to add the application to the allowlist.|
879
880**Return value**
881
882| Type                             | Description                                 |
883| --------------------------------- | ------------------------------------- |
884| Promise\<void> | Promise used to return the result.|
885
886**Error codes**
887
888| ID| Error Message                                     |
889| ------- | -------------------------------------------- |
890| 201     | Permission denied.                           |
891| 401     | Parameter error.                             |
892| 2100001 | Invalid parameter value.                |
893| 2100002 | Operation failed. Cannot connect to service.|
894| 2100003 | System internal error.                  |
895
896**Example**
897
898```js
899let param = {
900  uid: Number.parseInt(this.firstParam), isAllowed: Boolean(Number.parseInt(this.isBoolean))
901}
902policy.setDeviceIdleAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then(function (error, data) {
903  console.log(JSON.stringify(error))
904  console.log(JSON.stringify(data))
905})
906
907```
908
909## policy.getDeviceIdleAllowList
910
911getDeviceIdleAllowList(callback: AsyncCallback\<Array\<number>>): void
912
913Obtains the UID array of applications that are on the device idle allowlist. This API uses an asynchronous callback to return the result.
914
915**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
916
917**System capability**: SystemCapability.Communication.NetManager.Core
918
919**Parameters**
920
921| Name  | Type                                   | Mandatory| Description      |
922| -------- | --------------------------------------- | ---- | ---------- |
923| callback | AsyncCallback\<Array\<number>> | Yes  | Callback used to return the result.|
924
925**Error codes**
926
927| ID| Error Message                                     |
928| ------- | -------------------------------------------- |
929| 201     | Permission denied.                           |
930| 2100002 | Operation failed. Cannot connect to service.|
931| 2100003 | System internal error.                  |
932
933**Example**
934
935```js
936policy.getDeviceIdleAllowList((error, data) => {
937  this.callBack(error, data);
938});
939```
940
941## policy.getDeviceIdleAllowList
942
943getDeviceIdleAllowList(): Promise\<Array\<number>>;
944
945Obtains the UID array of applications that are on the device idle allowlist. This API uses a promise to return the result.
946
947**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
948
949**System capability**: SystemCapability.Communication.NetManager.Core
950
951**Return value**
952
953| Type                             | Description                                 |
954| --------------------------------- | ------------------------------------- |
955| Promise\<Array\<number>> | Promise used to return the result.|
956
957**Error codes**
958
959| ID| Error Message                                     |
960| ------- | -------------------------------------------- |
961| 201     | Permission denied.                           |
962| 2100002 | Operation failed. Cannot connect to service.|
963| 2100003 | System internal error.                  |
964
965**Example**
966
967```js
968policy.getDeviceIdleAllowList().then(function (error, data) {
969  console.log(JSON.stringify(error))
970  console.log(JSON.stringify(data))
971})
972```
973
974## policy.getBackgroundPolicyByUid
975
976getBackgroundPolicyByUid(uid: number, callback: AsyncCallback\<NetBackgroundPolicy>): void
977
978Obtains the background network policies configured for the given application. This API uses an asynchronous callback to return the result.
979
980**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
981
982**System capability**: SystemCapability.Communication.NetManager.Core
983
984**Parameters**
985
986| Name  | Type                                   | Mandatory| Description      |
987| -------- | --------------------------------------- | ---- | ---------- |
988| uid | number | Yes| Unique ID of the application.|
989| callback | AsyncCallback\<[NetBackgroundPolicy](#netbackgroundpolicy)> | Yes  | Callback used to return the result.|
990
991**Error codes**
992
993| ID| Error Message                                     |
994| ------- | -------------------------------------------- |
995| 201     | Permission denied.                           |
996| 401     | Parameter error.                             |
997| 2100001 | Invalid parameter value.                |
998| 2100002 | Operation failed. Cannot connect to service.|
999| 2100003 | System internal error.                  |
1000
1001**Example**
1002
1003```js
1004this.firstParam = uid
1005policy.getBackgroundPolicyByUid(Number.parseInt(this.firstParam), (error, data) => {
1006  this.callBack(error, data);
1007});
1008```
1009
1010## policy.getBackgroundPolicyByUid
1011
1012getBackgroundPolicyByUid(uid: number): Promise\<NetBackgroundPolicy>;
1013
1014Obtains the background network policies configured for the given application. This API uses a promise to return the result.
1015
1016**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1017
1018**System capability**: SystemCapability.Communication.NetManager.Core
1019
1020**Parameters**
1021
1022| Name  | Type                                   | Mandatory| Description      |
1023| -------- | --------------------------------------- | ---- | ---------- |
1024| uid | number | Yes| Unique ID of the application.|
1025
1026**Return value**
1027
1028| Type                             | Description                                 |
1029| --------------------------------- | ------------------------------------- |
1030| Promise\<[NetBackgroundPolicy](#netbackgroundpolicy)> | Promise used to return the result.|
1031
1032**Error codes**
1033
1034| ID| Error Message                                     |
1035| ------- | -------------------------------------------- |
1036| 201     | Permission denied.                           |
1037| 401     | Parameter error.                             |
1038| 2100001 | Invalid parameter value.                |
1039| 2100002 | Operation failed. Cannot connect to service.|
1040| 2100003 | System internal error.                  |
1041
1042**Example**
1043
1044```js
1045this.firstParam = uid
1046policy.getBackgroundPolicyByUid(Number.parseInt(this.firstParam)).then(function (error, data) {
1047  console.log(JSON.stringify(error))
1048  console.log(JSON.stringify(data))
1049})
1050```
1051
1052## policy.resetPolicies
1053
1054resetPolicies(iccid: string, callback: AsyncCallback\<void>): void
1055
1056Restores all the policies (cellular network, background network, firewall, and application-specific network policies) for the given SIM card. This API uses an asynchronous callback to return the result.
1057
1058**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1059
1060**System capability**: SystemCapability.Communication.NetManager.Core
1061
1062**Parameters**
1063
1064| Name  | Type                                   | Mandatory| Description      |
1065| -------- | --------------------------------------- | ---- | ---------- |
1066| iccid | string | Yes| SIM card ID.|
1067| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
1068
1069**Error codes**
1070
1071| ID| Error Message                                     |
1072| ------- | -------------------------------------------- |
1073| 201     | Permission denied.                           |
1074| 401     | Parameter error.                             |
1075| 2100001 | Invalid parameter value.                |
1076| 2100002 | Operation failed. Cannot connect to service.|
1077| 2100003 | System internal error.                  |
1078
1079**Example**
1080
1081```js
1082this.firstParam = iccid
1083policy.resetPolicies(this.firstParam, (error, data) => {
1084  this.callBack(error, data);
1085});
1086```
1087
1088## policy.resetPolicies
1089
1090resetPolicies(iccid: string): Promise\<void>;
1091
1092Restores all the policies (cellular network, background network, firewall, and application-specific network policies) for the given SIM card. This API uses a promise to return the result.
1093
1094**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1095
1096**System capability**: SystemCapability.Communication.NetManager.Core
1097
1098**Parameters**
1099
1100| Name  | Type                                   | Mandatory| Description      |
1101| -------- | --------------------------------------- | ---- | ---------- |
1102| iccid | string | Yes| SIM card ID.|
1103
1104**Return value**
1105
1106| Type                             | Description                                 |
1107| --------------------------------- | ------------------------------------- |
1108| Promise\<void> | Promise used to return the result.|
1109
1110**Error codes**
1111
1112| ID| Error Message                                     |
1113| ------- | -------------------------------------------- |
1114| 201     | Permission denied.                           |
1115| 401     | Parameter error.                             |
1116| 2100001 | Invalid parameter value.                |
1117| 2100002 | Operation failed. Cannot connect to service.|
1118| 2100003 | System internal error.                  |
1119
1120**Example**
1121
1122```js
1123policy.getUidsByPolicy(Number.parseInt(this.firstParam)).then(function (error, data) {
1124
1125})
1126this.firstParam = iccid
1127policy.resetPolicies(this.firstParam).then(function (error, data) {
1128  console.log(JSON.stringify(error))
1129  console.log(JSON.stringify(data))
1130})
1131
1132```
1133
1134## policy.updateRemindPolicy
1135
1136updateRemindPolicy(netType: NetBearType, iccid: string, remindType: RemindType, callback: AsyncCallback\<void>): void
1137
1138Updates a reminder policy. This API uses an asynchronous callback to return the result.
1139
1140**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1141
1142**System capability**: SystemCapability.Communication.NetManager.Core
1143
1144**Parameters**
1145
1146| Name  | Type                                   | Mandatory| Description      |
1147| -------- | --------------------------------------- | ---- | ---------- |
1148| netType | [NetBearType](js-apis-net-connection.md#netbeartype) | Yes| Network type.|
1149| iccid | string | Yes| SIM card ID.|
1150| remindType | [RemindType](#remindtype) | Yes| Reminder type.|
1151| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
1152
1153**Error codes**
1154
1155| ID| Error Message                                     |
1156| ------- | -------------------------------------------- |
1157| 201     | Permission denied.                           |
1158| 401     | Parameter error.                             |
1159| 2100001 | Invalid parameter value.                |
1160| 2100002 | Operation failed. Cannot connect to service.|
1161| 2100003 | System internal error.                  |
1162
1163**Example**
1164
1165```js
1166let param = {
1167  netType: Number.parseInt(this.netType), iccid: this.firstParam, remindType: this.currentRemindType
1168}
1169policy.updateRemindPolicy(Number.parseInt(this.netType), this.firstParam, Number.parseInt(this.currentRemindType), (error, data) => {
1170  this.callBack(error, data);
1171});
1172```
1173
1174## policy.updateRemindPolicy
1175
1176updateRemindPolicy(netType: NetBearType, iccid: string, remindType: RemindType): Promise\<void>;
1177
1178Updates a reminder policy. This API uses a promise to return the result.
1179
1180**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1181
1182**System capability**: SystemCapability.Communication.NetManager.Core
1183
1184**Parameters**
1185
1186| Name  | Type                                   | Mandatory| Description      |
1187| -------- | --------------------------------------- | ---- | ---------- |
1188| netType | [NetBearType](js-apis-net-connection.md#netbeartype) | Yes| Network type.|
1189| iccid | string | Yes| SIM card ID.|
1190| remindType | [RemindType](#remindtype) | Yes| Reminder type.|
1191
1192**Return value**
1193
1194| Type                             | Description                                 |
1195| --------------------------------- | ------------------------------------- |
1196| Promise\<void> | Promise used to return the result.|
1197
1198**Error codes**
1199
1200| ID| Error Message                                     |
1201| ------- | -------------------------------------------- |
1202| 201     | Permission denied.                           |
1203| 401     | Parameter error.                             |
1204| 2100001 | Invalid parameter value.                |
1205| 2100002 | Operation failed. Cannot connect to service.|
1206| 2100003 | System internal error.                  |
1207
1208**Example**
1209
1210```js
1211let param = {
1212  netType: Number.parseInt(this.netType), iccid: this.firstParam, remindType: this.currentRemindType
1213}
1214policy.updateRemindPolicy(Number.parseInt(this.netType), this.firstParam, Number.parseInt(this.currentRemindType)).then(function (error, data) {
1215  console.log(JSON.stringify(error))
1216  console.log(JSON.stringify(data))
1217})
1218
1219```
1220
1221## policy.setPowerSaveAllowList
1222
1223setPowerSaveAllowList(uid: number, isAllowed: boolean, callback: AsyncCallback\<void>): void
1224
1225Sets whether to add an application to the power-saving allowlist. This API uses an asynchronous callback to return the result.
1226
1227**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1228
1229**System capability**: SystemCapability.Communication.NetManager.Core
1230
1231**Parameters**
1232
1233| Name  | Type                                   | Mandatory| Description      |
1234| -------- | --------------------------------------- | ---- | ---------- |
1235| uid | number | Yes| Unique ID of the application.|
1236| isAllowed | boolean | Yes| Whether to add the application to the allowlist.|
1237| callback | callback: AsyncCallback\<void> | Yes  | Callback used to return the result.|
1238
1239**Error codes**
1240
1241| ID| Error Message                                     |
1242| ------- | -------------------------------------------- |
1243| 201     | Permission denied.                           |
1244| 401     | Parameter error.                             |
1245| 2100001 | Invalid parameter value.                |
1246| 2100002 | Operation failed. Cannot connect to service.|
1247| 2100003 | System internal error.                  |
1248
1249**Example**
1250
1251```js
1252let param = {
1253  uid: Number.parseInt(this.firstParam), isAllowed: Boolean(Number.parseInt(this.isBoolean))
1254}
1255policy.setPowerSaveAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean)), (error, data) => {
1256  this.callBack(error, data);
1257});
1258```
1259
1260## policy.setPowerSaveAllowList
1261
1262setPowerSaveAllowList(uid: number, isAllowed: boolean): Promise\<void>;
1263
1264Sets whether to add an application to the power-saving allowlist. This API uses a promise to return the result.
1265
1266**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1267
1268**System capability**: SystemCapability.Communication.NetManager.Core
1269
1270**Parameters**
1271
1272| Name  | Type                                   | Mandatory| Description      |
1273| -------- | --------------------------------------- | ---- | ---------- |
1274| uid | number | Yes| Unique ID of the application.|
1275| isAllowed | boolean | Yes| Whether to add the application to the allowlist.|
1276
1277**Return value**
1278
1279| Type                             | Description                                 |
1280| --------------------------------- | ------------------------------------- |
1281| Promise\<void> | Promise used to return the result.|
1282
1283**Error codes**
1284
1285| ID| Error Message                                     |
1286| ------- | -------------------------------------------- |
1287| 201     | Permission denied.                           |
1288| 401     | Parameter error.                             |
1289| 2100001 | Invalid parameter value.                |
1290| 2100002 | Operation failed. Cannot connect to service.|
1291| 2100003 | System internal error.                  |
1292
1293**Example**
1294
1295```js
1296let param = {
1297  uid: Number.parseInt(this.firstParam), isAllowed: Boolean(Number.parseInt(this.isBoolean))
1298}
1299policy.setPowerSaveAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then(function (error, data) {
1300  console.log(JSON.stringify(error))
1301  console.log(JSON.stringify(data))
1302})
1303
1304```
1305
1306## policy.getPowerSaveAllowList
1307
1308getPowerSaveAllowList(callback: AsyncCallback\<Array\<number>>): void
1309
1310Obtains the UID array of applications that are on the power-saving allowlist. This API uses an asynchronous callback to return the result.
1311
1312**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1313
1314**System capability**: SystemCapability.Communication.NetManager.Core
1315
1316**Parameters**
1317
1318| Name  | Type                                   | Mandatory| Description      |
1319| -------- | --------------------------------------- | ---- | ---------- |
1320| callback | AsyncCallback\<Array\<number>> | Yes  | Callback used to return the result.|
1321
1322**Error codes**
1323
1324| ID| Error Message                                     |
1325| ------- | -------------------------------------------- |
1326| 201     | Permission denied.                           |
1327| 2100002 | Operation failed. Cannot connect to service.|
1328| 2100003 | System internal error.                  |
1329
1330**Example**
1331
1332```js
1333policy.getPowerSaveAllowList((error, data) => {
1334  this.callBack(error, data);
1335});
1336```
1337
1338## policy.getPowerSaveAllowList
1339
1340getPowerSaveAllowList(): Promise\<Array\<number>>;
1341
1342Obtains the UID array of applications that are on the device idle allowlist. This API uses a promise to return the result.
1343
1344**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1345
1346**System capability**: SystemCapability.Communication.NetManager.Core
1347
1348**Return value**
1349
1350| Type                             | Description                                 |
1351| --------------------------------- | ------------------------------------- |
1352| Promise\<Array\<number>> | Promise used to return the result.|
1353
1354**Error codes**
1355
1356| ID| Error Message                                     |
1357| ------- | -------------------------------------------- |
1358| 201     | Permission denied.                           |
1359| 2100002 | Operation failed. Cannot connect to service.|
1360| 2100003 | System internal error.                  |
1361
1362**Example**
1363
1364```js
1365policy.getPowerSaveAllowList().then(function (error, data) {
1366  console.log(JSON.stringify(error))
1367  console.log(JSON.stringify(data))
1368})
1369```
1370
1371## policy.on
1372
1373Functions as the handle to a network policy.
1374
1375### on('netUidPolicyChange')
1376
1377on(type: "netUidPolicyChange", callback: Callback\<{ uid: number, policy: NetUidPolicy }>): void
1378
1379Subscribes to policy changes. This API uses an asynchronous callback to return the result.
1380
1381**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1382
1383**System capability**: SystemCapability.Communication.NetManager.Core
1384
1385**Parameters**
1386
1387| Name  | Type                              | Mandatory| Description                                                        |
1388| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
1389| type | netUidPolicyChange | Yes| Event type. The value **netUidPolicyChange** indicates a policy change event.|
1390| callback | Callback\<{ uid: number, policy: [NetUidPolicy](#netuidpolicy) }> | Yes  | Callback used to return the result. It is called when the registered network policy changes.|
1391
1392**Example**
1393
1394```js
1395policy.on('netUidPolicyChange', (data) => {
1396  this.log('on netUidPolicyChange: ' + JSON.stringify(data));
1397})
1398```
1399
1400### on('netUidRuleChange')
1401
1402on(type: "netUidRuleChange", callback: Callback\<{ uid: number, rule: NetUidRule }>): void
1403
1404Subscribes to rule changes. This API uses an asynchronous callback to return the result.
1405
1406**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1407
1408**System capability**: SystemCapability.Communication.NetManager.Core
1409
1410**Parameters**
1411
1412| Name  | Type                              | Mandatory| Description                                                        |
1413| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
1414| type | netUidRuleChange | Yes| Event type. The value **netUidRuleChange** indicates a rule change event.|
1415| callback | Callback\<{ uid: number, rule: [NetUidRule](#netuidrule) }> | Yes  | Callback used to return the result. It is called when the registered rule changes.|
1416
1417**Example**
1418
1419```js
1420policy.on('netUidRuleChange', (data) => {
1421  this.log('on netUidRuleChange: ' + JSON.stringify(data));
1422})
1423```
1424
1425### on('netMeteredIfacesChange')
1426
1427on(type: "netMeteredIfacesChange", callback: Callback\<Array\<string>>): void
1428
1429Subscribes to metered **iface** changes. This API uses an asynchronous callback to return the result.
1430
1431**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1432
1433**System capability**: SystemCapability.Communication.NetManager.Core
1434
1435**Parameters**
1436
1437| Name  | Type                              | Mandatory| Description                                                        |
1438| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
1439| type | netMeteredIfacesChange | Yes| Event type. The value **netMeteredIfacesChange** indicates a metered **iface** change event.|
1440| callback | Callback\<Array\<string>> | Yes  | Callback used to return the result. It is called when the registered metered **iface** changes.|
1441
1442**Example**
1443
1444```js
1445policy.on('netMeteredIfacesChange', (data) => {
1446  this.log('on netMeteredIfacesChange: ' + JSON.stringify(data));
1447})
1448```
1449
1450### on('netQuotaPolicyChange')
1451
1452on(type: "netQuotaPolicyChange", callback: Callback\<Array\<NetQuotaPolicy>>): void
1453
1454Subscribes to network quota policy changes. This API uses an asynchronous callback to return the result.
1455
1456**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1457
1458**System capability**: SystemCapability.Communication.NetManager.Core
1459
1460**Parameters**
1461
1462| Name  | Type                              | Mandatory| Description                                                        |
1463| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
1464| type | netQuotaPolicyChange | Yes| Event type. The value **netQuotaPolicyChange** indicates a network quota policy change event.|
1465| callback | Callback\<Array\<[NetQuotaPolicy](#netquotapolicy)>> | Yes  | Callback used to return the result. It is called when the registered network quota policy changes.|
1466
1467**Example**
1468
1469```js
1470policy.on('netQuotaPolicyChange', (data) => {
1471  this.log('on netQuotaPolicyChange: ' + JSON.stringify(data));
1472})
1473```
1474
1475### on('netBackgroundPolicyChange')
1476
1477on(type: "netBackgroundPolicyChange", callback: Callback\<boolean>): void
1478
1479Subscribes to background network policy changes. This API uses an asynchronous callback to return the result.
1480
1481**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
1482
1483**System capability**: SystemCapability.Communication.NetManager.Core
1484
1485**Parameters**
1486
1487| Name  | Type                              | Mandatory| Description                                                        |
1488| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
1489| type | netBackgroundPolicyChange | Yes| Event type. The value **netBackgroundPolicyChange** indicates a background network policy change event.|
1490| callback | Callback\<boolean> | Yes  | Callback used to return the result. It is called when the registered background network policy changes.|
1491
1492**Example**
1493
1494```js
1495policy.on('netBackgroundPolicyChange', (data) => {
1496  this.log('on netBackgroundPolicyChange: ' + JSON.stringify(data));
1497})
1498```
1499
1500## NetBackgroundPolicy
1501
1502Enumerates the background network policies.
1503
1504**System capability**: SystemCapability.Communication.NetManager.Core
1505
1506| Name                 | Value  | Description                  |
1507| ------------------------ | ---- | ---------------------- |
1508| NET_BACKGROUND_POLICY_NONE       | 0 | Default policy.|
1509| NET_BACKGROUND_POLICY_ENABLE     | 1 | Applications running in the background are allowed to access metered networks.|
1510| NET_BACKGROUND_POLICY_DISABLE    | 2 | Applications running in the background are not allowed to access metered networks.|
1511| NET_BACKGROUND_POLICY_ALLOW_LIST | 3 | Only applications on the allowlist are allowed to access metered networks when they are running in the background.|
1512
1513## NetQuotaPolicy
1514
1515Defines a network quota policy.
1516
1517**System capability**: SystemCapability.Communication.NetManager.Core
1518
1519| Name                 | Type                               | Description                                                        |
1520| ----------------------- | ----------------------------------- | ------------------------------------------------------------ |
1521| netType           | [NetBearType](js-apis-net-connection.md#netbeartype) | Network type.|
1522| iccid             | string                      | Identifier of the SIM card on the metered cellular network. It is not used for Wi-Fi networks.|
1523| ident             | string                      | Identifier of the SIM card on the metered cellular network. It is used for Wi-Fi networks. It is used together with **iccid**.|
1524| periodDuration    | string                      | Start time of metering.|
1525| warningBytes      | number                      | Data volume threshold for generating an alarm.|
1526| limitBytes        | number                      | Data volume quota.|
1527| lastWarningRemind | string                      | Last time when an alarm was generated.|
1528| lastLimitRemind   | string                      | Last time when the quota was exhausted.|
1529| metered           | string                      | Whether the network is a metered network.|
1530| limitAction       | [LimitAction](#limitaction) | Action to take when the data volume quota is reached.|
1531
1532## LimitAction
1533
1534Enumerates the actions that can be taken when the data volume quota is reached.
1535
1536**System capability**: SystemCapability.Communication.NetManager.Core
1537
1538| Name                 | Value| Description  |
1539| ---------------------- | ----- | ------------ |
1540| LIMIT_ACTION_NONE     | -1 | Default policy.|
1541| LIMIT_ACTION_DISABLE  | 0  | Internet access is disabled.|
1542| LIMIT_ACTION_AUTO_BILL| 1  | Users will be automatically charged for the data volume they use.|
1543
1544## NetUidRule
1545
1546Enumerates the metered network rules.
1547
1548**System capability**: SystemCapability.Communication.NetManager.Core
1549
1550| Name                 | Value| Description  |
1551| ---------------------- | ----- | ------------ |
1552| NET_RULE_NONE                     | 0 | Default rule.|
1553| NET_RULE_ALLOW_METERED_FOREGROUND | 1 | Applications running in the foreground are allowed to access metered networks.|
1554| NET_RULE_ALLOW_METERED            | 2 | Applications are allowed to access metered networks.|
1555| NET_RULE_REJECT_METERED           | 4 | Applications are not allowed to access metered networks.|
1556| NET_RULE_ALLOW_ALL                | 32 | Applications are allowed to access all networks (metered or non-metered).|
1557| NET_RULE_REJECT_ALL               | 64 | Applications are not allowed to access any networks (metered or non-metered).|
1558
1559## RemindType
1560
1561Enumerates the reminder types.
1562
1563**System capability**: SystemCapability.Communication.NetManager.Core
1564
1565| Name                 | Value| Description  |
1566| ---------------------- | - | ------- |
1567| REMIND_TYPE_WARNING    | 1 | Warning.|
1568| REMIND_TYPE_LIMIT      | 2 | Limit.|
1569
1570## NetUidPolicy
1571
1572Enumerates the application-specific network policies.
1573
1574**System capability**: SystemCapability.Communication.NetManager.Core
1575
1576| Name                 | Value| Description  |
1577| ---------------------- | ----- | ------------ |
1578| NET_POLICY_NONE                       | 0 | Default network policy.|
1579| NET_POLICY_ALLOW_METERED_BACKGROUND   | 1 | Applications running in the background are allowed to access metered networks.|
1580| NET_POLICY_REJECT_METERED_BACKGROUND  | 2 | Applications running in the background are not allowed to access metered networks.|
1581