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 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Modules to Import 10 11```ts 12import policy from '@ohos.net.policy'; 13``` 14 15## policy.setBackgroundAllowed<sup>10+</sup> 16 17setBackgroundAllowed(isAllowed: boolean, callback: AsyncCallback\<void>): void 18 19Specifies whether background applications are allowed to access the network. This API uses an asynchronous callback to return the result. 20 21**System API**: This is a system API. 22 23**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 24 25**System capability**: SystemCapability.Communication.NetManager.Core 26 27**Parameters** 28 29| Name | Type | Mandatory| Description | 30| --------- | -------------------- | ---- | ------------------------------------------------------------ | 31| isAllowed | boolean | Yes | Whether background applications are allowed to use mobile data. | 32| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 33 34**Error codes** 35 36| ID| Error Message | 37| --------- | -------------------------------------------- | 38| 201 | Permission denied. | 39| 202 | Non-system applications use system APIs. | 40| 401 | Parameter error. | 41| 2100001 | Invalid parameter value. | 42| 2100002 | Operation failed. Cannot connect to service. | 43| 2100003 | System internal error. | 44 45**Example** 46 47```ts 48import { BusinessError } from '@ohos.base'; 49policy.setBackgroundAllowed(true, (error: BusinessError) => { 50 console.log(JSON.stringify(error)); 51}); 52``` 53 54## policy.setBackgroundAllowed<sup>10+</sup> 55 56setBackgroundAllowed(isAllowed: boolean): Promise\<void> 57 58Specifies whether background applications are allowed to access the network. This API uses a promise to return the result. 59 60**System API**: This is a system API. 61 62**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 63 64**System capability**: SystemCapability.Communication.NetManager.Core 65 66**Parameters** 67 68| Name | Type | Mandatory| Description | 69| --------- | ------- | ---- | ------------------------ | 70| isAllowed | boolean | Yes | Whether background applications are allowed to use mobile data.| 71 72**Error codes** 73 74| ID| Error Message | 75| --------- | -------------------------------------------- | 76| 201 | Permission denied. | 77| 202 | Non-system applications use system APIs. | 78| 401 | Parameter error. | 79| 2100001 | Invalid parameter value. | 80| 2100002 | Operation failed. Cannot connect to service. | 81| 2100003 | System internal error. | 82 83**Return value** 84 85| Type | Description | 86| -------------- | ----------------------------------------------------------------- | 87| Promise\<void> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 88 89**Example** 90 91```ts 92import { BusinessError } from '@ohos.base'; 93policy.setBackgroundAllowed(true).then(() => { 94 console.log("setBackgroundAllowed success"); 95}).catch((error: BusinessError) => { 96 console.log(JSON.stringify(error)); 97}); 98``` 99 100## policy.isBackgroundAllowed<sup>10+</sup> 101 102isBackgroundAllowed(callback: AsyncCallback\<boolean>): void 103 104Checks whether the current application is allowed to access the network when running at the background. This API uses an asynchronous callback to return the result. 105 106**System API**: This is a system API. 107 108**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 109 110**System capability**: SystemCapability.Communication.NetManager.Core 111 112**Parameters** 113 114| Name | Type | Mandatory| Description | 115| -------- | ----------------------- | ---- | ---------------------------------------------------------------- | 116| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. If the operation is successful, the value **true** is returned, which means that background applications are allowed to use mobile data. If the operation fails, an error message is returned.| 117 118**Error codes** 119 120| ID| Error Message | 121| --------- | -------------------------------------------- | 122| 201 | Permission denied. | 123| 202 | Non-system applications use system APIs. | 124| 401 | Parameter error. | 125| 2100001 | Invalid parameter value. | 126| 2100002 | Operation failed. Cannot connect to service. | 127| 2100003 | System internal error. | 128 129**Example** 130 131```ts 132import { BusinessError } from '@ohos.base'; 133policy.isBackgroundAllowed((error: BusinessError, data: boolean) => { 134 console.log(JSON.stringify(error)); 135 console.log(JSON.stringify(data)); 136}); 137``` 138 139## policy.isBackgroundAllowed<sup>10+</sup> 140 141isBackgroundAllowed(): Promise\<boolean>; 142 143Checks whether the current application is allowed to access the network when running at the background. This API uses a promise to return the result. 144 145**System API**: This is a system API. 146 147**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 148 149**System capability**: SystemCapability.Communication.NetManager.Core 150 151**Return value** 152 153| Type | Description | 154| ----------------- | ------------------------------------------------------------------------------------ | 155| Promise\<boolean> | Promise used to return the result. If the operation is successful, the value **true** is returned, which means that background applications are allowed to use mobile data. If the operation fails, an error message is returned.| 156 157**Error codes** 158 159| ID| Error Message | 160| --------- | -------------------------------------------- | 161| 201 | Permission denied. | 162| 202 | Non-system applications use system APIs. | 163| 401 | Parameter error. | 164| 2100001 | Invalid parameter value. | 165| 2100002 | Operation failed. Cannot connect to service. | 166| 2100003 | System internal error. | 167 168**Example** 169 170```ts 171import { BusinessError } from '@ohos.base'; 172policy 173 .isBackgroundAllowed() 174 .then((data: boolean) => { 175 console.log(JSON.stringify(data)); 176 }) 177 .catch((error: BusinessError) => { 178 console.log(JSON.stringify(error)); 179 }); 180``` 181 182## policy.setPolicyByUid<sup>10+</sup> 183 184setPolicyByUid(uid: number, policy: NetUidPolicy, callback: AsyncCallback\<void>): void 185 186Sets the metered network access policy for the application specified by a given UID. This API uses an asynchronous callback to return the result. 187 188**System API**: This is a system API. 189 190**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 191 192**System capability**: SystemCapability.Communication.NetManager.Core 193 194**Parameters** 195 196| Name | Type | Mandatory| Description | 197| -------- | ------------------------------- | ---- | ---------------------------------------------- | 198| uid | number | Yes | Unique ID of the application. | 199| policy | [NetUidPolicy](#netuidpolicy10) | Yes | Network access policy for the application. | 200| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 201 202**Error codes** 203 204| ID| Error Message | 205| --------- | -------------------------------------------- | 206| 201 | Permission denied. | 207| 202 | Non-system applications use system APIs. | 208| 401 | Parameter error. | 209| 2100001 | Invalid parameter value. | 210| 2100002 | Operation failed. Cannot connect to service. | 211| 2100003 | System internal error. | 212 213**Example** 214 215```ts 216import { BusinessError } from '@ohos.base'; 217policy.setPolicyByUid(11111, policy.NetUidPolicy.NET_POLICY_NONE, (error: BusinessError) => { 218 console.log(JSON.stringify(error)); 219}); 220``` 221 222## policy.setPolicyByUid<sup>10+</sup> 223 224setPolicyByUid(uid: number, policy: NetUidPolicy): Promise\<void>; 225 226Sets the metered network access policy for the application specified by a given UID. This API uses a promise to return the result. 227 228**System API**: This is a system API. 229 230**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 231 232**System capability**: SystemCapability.Communication.NetManager.Core 233 234**Parameters** 235 236| Name| Type | Mandatory| Description | 237| ------ | ------------------------------- | ---- | -------------- | 238| uid | number | Yes | Unique ID of the application.| 239| policy | [NetUidPolicy](#netuidpolicy10) | Yes | Network access policy for the application.| 240 241**Return value** 242 243| Type | Description | 244| -------------- | ----------------------------------------------------------------- | 245| Promise\<void> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 246 247**Error codes** 248 249| ID| Error Message | 250| --------- | -------------------------------------------- | 251| 201 | Permission denied. | 252| 202 | Non-system applications use system APIs. | 253| 401 | Parameter error. | 254| 2100001 | Invalid parameter value. | 255| 2100002 | Operation failed. Cannot connect to service. | 256| 2100003 | System internal error. | 257 258**Example** 259 260```ts 261import { BusinessError } from '@ohos.base'; 262policy 263 .setPolicyByUid(11111, policy.NetUidPolicy.NET_POLICY_NONE) 264 .then(() => { 265 console.log('setPolicyByUid success'); 266 }) 267 .catch((error: BusinessError) => { 268 console.log(JSON.stringify(error)); 269 }); 270``` 271 272## policy.getPolicyByUid<sup>10+</sup> 273 274getPolicyByUid(uid: number, callback: AsyncCallback\<NetUidPolicy>): void 275 276Obtains the network access policy for the application specified by a given UID. This API uses an asynchronous callback to return the result. 277 278**System API**: This is a system API. 279 280**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 281 282**System capability**: SystemCapability.Communication.NetManager.Core 283 284**Parameters** 285 286| Name | Type | Mandatory| Description | 287| -------- | ----------------------------------------------- | ---- | -------------------------------------------------------- | 288| uid | number | Yes | Unique ID of the application. | 289| callback | AsyncCallback\<[NetUidPolicy](#netuidpolicy10)> | 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.| 290 291**Error codes** 292 293| ID| Error Message | 294| --------- | -------------------------------------------- | 295| 201 | Permission denied. | 296| 202 | Non-system applications use system APIs. | 297| 401 | Parameter error. | 298| 2100001 | Invalid parameter value. | 299| 2100002 | Operation failed. Cannot connect to service. | 300| 2100003 | System internal error. | 301 302**Example** 303 304```ts 305import { BusinessError } from '@ohos.base'; 306policy.getPolicyByUid(11111, (error: BusinessError, data: object) => { 307 console.log(JSON.stringify(error)); 308 console.log(JSON.stringify(data)); 309}); 310``` 311 312## policy.getPolicyByUid<sup>10+</sup> 313 314getPolicyByUid(uid: number): Promise\<NetUidPolicy>; 315 316Obtains the network access policy for the application specified by a given UID. This API uses a promise to return the result. 317 318**System API**: This is a system API. 319 320**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 321 322**System capability**: SystemCapability.Communication.NetManager.Core 323 324**Parameters** 325 326| Name| Type | Mandatory| Description | 327| ------ | ------ | ---- | -------------- | 328| uid | number | Yes | Unique ID of the application.| 329 330**Return value** 331 332| Type | Description | 333| ----------------------------------------- | --------------------------------------------------------- | 334| Promise\<[NetUidPolicy](#netuidpolicy10)> | Promise used to return the result. If the operation fails, an error message is returned.| 335 336**Error codes** 337 338| ID| Error Message | 339| --------- | -------------------------------------------- | 340| 201 | Permission denied. | 341| 202 | Non-system applications use system APIs. | 342| 401 | Parameter error. | 343| 2100001 | Invalid parameter value. | 344| 2100002 | Operation failed. Cannot connect to service. | 345| 2100003 | System internal error. | 346 347**Example** 348 349```ts 350import { BusinessError } from '@ohos.base'; 351policy 352 .getPolicyByUid(11111) 353 .then((data: policy.NetUidPolicy) => { 354 console.log(JSON.stringify(data)); 355 }) 356 .catch((error: BusinessError) => { 357 console.log(JSON.stringify(error)); 358 }); 359``` 360 361## policy.getUidsByPolicy<sup>10+</sup> 362 363getUidsByPolicy(policy: NetUidPolicy, callback: AsyncCallback\<Array\<number>>): void 364 365Obtains all UIDs that match the specified network policy. This API uses an asynchronous callback to return the result. 366 367**System API**: This is a system API. 368 369**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 370 371**System capability**: SystemCapability.Communication.NetManager.Core 372 373**Parameters** 374 375| Name | Type | Mandatory| Description | 376| -------- | ------------------------------- | ---- | ----------------------------------------------------------- | 377| policy | [NetUidPolicy](#netuidpolicy10) | Yes | Network policy for the application. | 378| 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.| 379 380**Error codes** 381 382| ID| Error Message | 383| --------- | -------------------------------------------- | 384| 201 | Permission denied. | 385| 202 | Non-system applications use system APIs. | 386| 401 | Parameter error. | 387| 2100001 | Invalid parameter value. | 388| 2100002 | Operation failed. Cannot connect to service. | 389| 2100003 | System internal error. | 390 391**Example** 392 393```ts 394import { BusinessError } from '@ohos.base'; 395policy.getUidsByPolicy(11111, (error: BusinessError, data: object) => { 396 console.log(JSON.stringify(error)); 397 console.log(JSON.stringify(data)); 398}); 399``` 400 401## policy.getUidsByPolicy<sup>10+</sup> 402 403getUidsByPolicy(policy: NetUidPolicy): Promise\<Array\<number>>; 404 405Obtains all UIDs that match the specified network policy. This API uses a promise to return the result. 406 407**System API**: This is a system API. 408 409**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 410 411**System capability**: SystemCapability.Communication.NetManager.Core 412 413**Parameters** 414 415| Name| Type | Mandatory| Description | 416| ------ | ------------------------------- | ---- | -------------------------- | 417| policy | [NetUidPolicy](#netuidpolicy10) | Yes | Network policy for the application.| 418 419**Return value** 420 421| Type | Description | 422| ------------------------ | ------------------------------------------------------------ | 423| 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.| 424 425**Error codes** 426 427| ID| Error Message | 428| --------- | -------------------------------------------- | 429| 201 | Permission denied. | 430| 202 | Non-system applications use system APIs. | 431| 401 | Parameter error. | 432| 2100001 | Invalid parameter value. | 433| 2100002 | Operation failed. Cannot connect to service. | 434| 2100003 | System internal error. | 435 436**Example** 437 438```ts 439import { BusinessError } from '@ohos.base'; 440policy 441 .getUidsByPolicy(11111) 442 .then((data: object) => { 443 console.log(JSON.stringify(data)); 444 }) 445 .catch((error: BusinessError) => { 446 console.log(JSON.stringify(error)); 447 }); 448``` 449 450## policy.getNetQuotaPolicies<sup>10+</sup> 451 452getNetQuotaPolicies(callback: AsyncCallback\<Array\<NetQuotaPolicy>>): void 453 454Obtains the network quota policies. This API uses an asynchronous callback to return the result. 455 456**System API**: This is a system API. 457 458**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 459 460**System capability**: SystemCapability.Communication.NetManager.Core 461 462**Parameters** 463 464| Name | Type | Mandatory| Description | 465| -------- | ----------------------------------------------------------- | ---- | ------------------------ | 466| callback | AsyncCallback\<Array\<[NetQuotaPolicy](#netquotapolicy10)>> | Yes | Callback used to return the result.| 467 468**Error codes** 469 470| ID| Error Message | 471| --------- | -------------------------------------------- | 472| 201 | Permission denied. | 473| 202 | Non-system applications use system APIs. | 474| 401 | Parameter error. | 475| 2100001 | Invalid parameter value. | 476| 2100002 | Operation failed. Cannot connect to service. | 477| 2100003 | System internal error. | 478 479**Example** 480 481```ts 482import { BusinessError } from '@ohos.base'; 483policy.getNetQuotaPolicies((error: BusinessError, data: policy.NetQuotaPolicy[]) => { 484 console.log(JSON.stringify(error)); 485 console.log(JSON.stringify(data)); 486}); 487``` 488 489## policy.getNetQuotaPolicies<sup>10+</sup> 490 491getNetQuotaPolicies(): Promise\<Array\<NetQuotaPolicy>>; 492 493Obtains the network quota policies. This API uses a promise to return the result. 494 495**System API**: This is a system API. 496 497**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 498 499**System capability**: SystemCapability.Communication.NetManager.Core 500 501**Return value** 502 503| Type | Description | 504| ----------------------------------------------------- | ----------------------------- | 505| Promise\<Array\<[NetQuotaPolicy](#netquotapolicy10)>> | Promise used to return the result.| 506 507**Error codes** 508 509| ID| Error Message | 510| --------- | -------------------------------------------- | 511| 201 | Permission denied. | 512| 202 | Non-system applications use system APIs. | 513| 401 | Parameter error. | 514| 2100002 | Operation failed. Cannot connect to service. | 515| 2100003 | System internal error. | 516 517**Example** 518 519```ts 520import { BusinessError } from '@ohos.base'; 521policy 522 .getNetQuotaPolicies() 523 .then((data: policy.NetQuotaPolicy[]) => { 524 console.log(JSON.stringify(data)); 525 }) 526 .catch((error: BusinessError) => { 527 console.log(JSON.stringify(error)); 528 }); 529``` 530 531## policy.setNetQuotaPolicies<sup>10+</sup> 532 533setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>, callback: AsyncCallback\<void>): void 534 535Sets network quota policies. This API uses an asynchronous callback to return the result. 536 537**System API**: This is a system API. 538 539**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 540 541**System capability**: SystemCapability.Communication.NetManager.Core 542 543**Parameters** 544 545| Name | Type | Mandatory| Description | 546| ------------- | ------------------------------------------- | ---- | ---------------------------------------------- | 547| quotaPolicies | Array\<[NetQuotaPolicy](#netquotapolicy10)> | Yes | Network quota policies. | 548| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 549 550**Error codes** 551 552| ID| Error Message | 553| --------- | -------------------------------------------- | 554| 201 | Permission denied. | 555| 202 | Non-system applications use system APIs. | 556| 401 | Parameter error. | 557| 2100001 | Invalid parameter value. | 558| 2100002 | Operation failed. Cannot connect to service. | 559| 2100003 | System internal error. | 560 561**Example** 562 563```ts 564import connection from '@ohos.net.connection'; 565import { BusinessError } from '@ohos.base'; 566 567let netQuotaPolicyList: Array<policy.NetQuotaPolicy>|null = null; 568class Netquotapolicy { 569 networkMatchRule: NetworkMatchRule = new NetworkMatchRule(); 570 quotaPolicy: QuotaPolicy = new QuotaPolicy(); 571} 572class NetworkMatchRule { 573 netType: policy.NetBearType = connection.NetBearType.BEARER_CELLULAR; 574 identity: string = ''; 575 simId: string = '1'; 576} 577class QuotaPolicy { 578 periodDuration: string = 'M1'; 579 warningBytes: number = 40000; 580 limitBytes: number = 50000; 581 metered: boolean = true; 582 limitAction: policy.LimitAction.LIMIT_ACTION_NONE; 583} 584 585let netquotapolicy = new Netquotapolicy(); 586 587netQuotaPolicyList.push(netquotapolicy); 588 589policy.setNetQuotaPolicies(netQuotaPolicyList, (error: BusinessError) => { 590 console.log(JSON.stringify(error)); 591}); 592``` 593 594## policy.setNetQuotaPolicies<sup>10+</sup> 595 596setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>): Promise\<void>; 597 598Sets network quota policies. This API uses a promise to return the result. 599 600**System API**: This is a system API. 601 602**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 603 604**System capability**: SystemCapability.Communication.NetManager.Core 605 606**Parameters** 607 608| Name | Type | Mandatory| Description | 609| ------------- | ------------------------------------------- | ---- | ------------ | 610| quotaPolicies | Array\<[NetQuotaPolicy](#netquotapolicy10)> | Yes | Network quota policies.| 611 612**Error codes** 613 614| ID| Error Message | 615| --------- | -------------------------------------------- | 616| 201 | Permission denied. | 617| 202 | Non-system applications use system APIs. | 618| 401 | Parameter error. | 619| 2100001 | Invalid parameter value. | 620| 2100002 | Operation failed. Cannot connect to service. | 621| 2100003 | System internal error. | 622 623**Return value** 624 625| Type | Description | 626| -------------- | ----------------------------------------------------------------- | 627| Promise\<void> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 628 629**Example** 630 631```ts 632import connection from '@ohos.net.connection'; 633import { BusinessError } from '@ohos.base'; 634 635let netQuotaPolicyList: Array<policy.NetQuotaPolicy>|null = null; 636class Netquotapolicy { 637 networkMatchRule: NetworkMatchRule = new NetworkMatchRule(); 638 quotaPolicy: QuotaPolicy = new QuotaPolicy(); 639} 640class NetworkMatchRule { 641 netType: policy.NetBearType = connection.NetBearType.BEARER_CELLULAR; 642 identity: string = ''; 643 simId: string = '1'; 644} 645class QuotaPolicy { 646 periodDuration: string = 'M1'; 647 warningBytes: number = 40000; 648 limitBytes: number = 50000; 649 metered: boolean = true; 650 limitAction: policy.LimitAction.LIMIT_ACTION_NONE; 651} 652 653let netquotapolicy = new Netquotapolicy(); 654 655netQuotaPolicyList.push(netquotapolicy); 656 657policy 658 .setNetQuotaPolicies(netQuotaPolicyList) 659 .then(() => { 660 console.log('setNetQuotaPolicies success'); 661 }) 662 .catch((error: BusinessError) => { 663 console.log(JSON.stringify(error)); 664 }); 665``` 666 667## policy.isUidNetAllowed<sup>10+</sup> 668 669isUidNetAllowed(uid: number, isMetered: boolean, callback: AsyncCallback\<boolean>): void 670 671Checks whether the application specified by a given UID is allowed to access a metered network. This API uses an asynchronous callback to return the result. 672 673**System API**: This is a system API. 674 675**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 676 677**System capability**: SystemCapability.Communication.NetManager.Core 678 679**Parameters** 680 681| Name | Type | Mandatory| Description | 682| --------- | ----------------------- | ---- | --------------------------------------------------------- | 683| uid | number | Yes | Unique ID of the application. | 684| isMetered | boolean | Yes | Whether the network is a metered network. | 685| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** means that the application is allowed to access metered networks, and the value **false** means the opposite.| 686 687**Error codes** 688 689| ID| Error Message | 690| --------- | -------------------------------------------- | 691| 201 | Permission denied. | 692| 202 | Non-system applications use system APIs. | 693| 401 | Parameter error. | 694| 2100001 | Invalid parameter value. | 695| 2100002 | Operation failed. Cannot connect to service. | 696| 2100003 | System internal error. | 697 698**Example** 699 700```ts 701import { BusinessError } from '@ohos.base'; 702policy.isUidNetAllowed(11111, true, (error: BusinessError, data: boolean) => { 703 console.log(JSON.stringify(error)); 704 console.log(JSON.stringify(data)); 705}); 706``` 707 708## policy.isUidNetAllowed<sup>10+</sup> 709 710isUidNetAllowed(uid: number, isMetered: boolean): Promise\<boolean>; 711 712Checks whether the application specified by a given UID is allowed to access a metered network. This API uses a promise to return the result. 713 714**System API**: This is a system API. 715 716**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 717 718**System capability**: SystemCapability.Communication.NetManager.Core 719 720**Parameters** 721 722| Name | Type | Mandatory| Description | 723| --------- | ------- | ---- | -------------- | 724| uid | number | Yes | Unique ID of the application.| 725| isMetered | boolean | Yes | Whether the network is a metered network.| 726 727**Return value** 728 729| Type | Description | 730| ----------------- | ----------------------------- | 731| Promise\<boolean> | Promise used to return the result.| 732 733**Error codes** 734 735| ID| Error Message | 736| --------- | -------------------------------------------- | 737| 201 | Permission denied. | 738| 202 | Non-system applications use system APIs. | 739| 401 | Parameter error. | 740| 2100001 | Invalid parameter value. | 741| 2100002 | Operation failed. Cannot connect to service. | 742| 2100003 | System internal error. | 743 744**Example** 745 746```ts 747import { BusinessError } from '@ohos.base'; 748policy 749 .isUidNetAllowed(11111, true) 750 .then((data: boolean) => { 751 console.log(JSON.stringify(data)); 752 }) 753 .catch((error: BusinessError) => { 754 console.log(JSON.stringify(error)); 755 }); 756``` 757 758## policy.isUidNetAllowed<sup>10+</sup> 759 760isUidNetAllowed(uid: number, iface: string, callback: AsyncCallback\<boolean>): void 761 762Checks whether an application is allowed to access the specified network. This API uses an asynchronous callback to return the result. 763 764**System API**: This is a system API. 765 766**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 767 768**System capability**: SystemCapability.Communication.NetManager.Core 769 770**Parameters** 771 772| Name | Type | Mandatory| Description | 773| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 774| uid | number | Yes | Unique ID of the application. | 775| iface | string | Yes | Name of the target network. | 776| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** means that the application is allowed to access the specified network, and the value **false** means the opposite.| 777 778**Error codes** 779 780| ID| Error Message | 781| --------- | -------------------------------------------- | 782| 201 | Permission denied. | 783| 202 | Non-system applications use system APIs. | 784| 401 | Parameter error. | 785| 2100001 | Invalid parameter value. | 786| 2100002 | Operation failed. Cannot connect to service. | 787| 2100003 | System internal error. | 788 789**Example** 790 791```ts 792import { BusinessError } from '@ohos.base'; 793policy.isUidNetAllowed(11111, 'wlan0', (error: BusinessError, data: boolean) => { 794 console.log(JSON.stringify(error)); 795 console.log(JSON.stringify(data)); 796}); 797``` 798 799## policy.isUidNetAllowed<sup>10+</sup> 800 801isUidNetAllowed(uid: number, iface: string): Promise\<boolean>; 802 803Checks whether an application is allowed to access the specified network. This API uses a promise to return the result. 804 805**System API**: This is a system API. 806 807**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 808 809**System capability**: SystemCapability.Communication.NetManager.Core 810 811**Parameters** 812 813| Name| Type | Mandatory| Description | 814| ------ | ------ | ---- | -------------- | 815| uid | number | Yes | Unique ID of the application.| 816| iface | string | Yes | Name of the target network.| 817 818**Return value** 819 820| Type | Description | 821| ----------------- | ------------------------------------------------------- | 822| Promise\<boolean> | Promise used to return the result. | 823 824**Error codes** 825 826| ID| Error Message | 827| --------- | -------------------------------------------- | 828| 201 | Permission denied. | 829| 202 | Non-system applications use system APIs. | 830| 401 | Parameter error. | 831| 2100001 | Invalid parameter value. | 832| 2100002 | Operation failed. Cannot connect to service. | 833| 2100003 | System internal error. | 834 835**Example** 836 837```ts 838import { BusinessError } from '@ohos.base'; 839policy 840 .isUidNetAllowed(11111, 'wlan0') 841 .then((data: boolean) => { 842 console.log(JSON.stringify(data)); 843 }) 844 .catch((error: BusinessError) => { 845 console.log(JSON.stringify(error)); 846 }); 847``` 848 849## policy.setDeviceIdleTrustlist<sup>10+</sup> 850 851setDeviceIdleTrustlist(uids: Array\<number>, isAllowed: boolean, callback: AsyncCallback\<void>): void 852 853Adds applications specified by given UIDs to the device idle allowlist. This API uses an asynchronous callback to return the result. 854 855**System API**: This is a system API. 856 857**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 858 859**System capability**: SystemCapability.Communication.NetManager.Core 860 861**Parameters** 862 863| Name | Type | Mandatory| Description | 864| --------- | ------------------------------ | ---- | ---------------------------------------------- | 865| uids | Array\<number> | Yes | Unique ID of the application. | 866| isAllowed | boolean | Yes | Whether to add the application to the allowlist. | 867| callback | callback: AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 868 869**Error codes** 870 871| ID| Error Message | 872| --------- | -------------------------------------------- | 873| 201 | Permission denied. | 874| 202 | Non-system applications use system APIs. | 875| 401 | Parameter error. | 876| 2100001 | Invalid parameter value. | 877| 2100002 | Operation failed. Cannot connect to service. | 878| 2100003 | System internal error. | 879 880**Example** 881 882```ts 883import { BusinessError } from '@ohos.base'; 884policy.setDeviceIdleTrustlist([11111, 22222], true, (error: BusinessError) => { 885 console.log(JSON.stringify(error)); 886}); 887``` 888 889## policy.setDeviceIdleTrustlist<sup>10+</sup> 890 891setDeviceIdleTrustlist(uids: Array\<number>, isAllowed: boolean): Promise\<void>; 892 893Adds applications specified by given UIDs to the device idle allowlist. This API uses a promise to return the result. 894 895**System API**: This is a system API. 896 897**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 898 899**System capability**: SystemCapability.Communication.NetManager.Core 900 901**Parameters** 902 903| Name | Type | Mandatory| Description | 904| --------- | -------------- | ---- | -------------- | 905| uids | Array\<number> | Yes | Unique ID of the application.| 906| isAllowed | boolean | Yes | Whether to add the application to the allowlist.| 907 908**Return value** 909 910| Type | Description | 911| -------------- | ----------------------------------------------------------------- | 912| Promise\<void> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 913 914**Error codes** 915 916| ID| Error Message | 917| --------- | -------------------------------------------- | 918| 201 | Permission denied. | 919| 202 | Non-system applications use system APIs. | 920| 401 | Parameter error. | 921| 2100001 | Invalid parameter value. | 922| 2100002 | Operation failed. Cannot connect to service. | 923| 2100003 | System internal error. | 924 925**Example** 926 927```ts 928import { BusinessError } from '@ohos.base'; 929policy 930 .setDeviceIdleTrustlist([11111, 22222], true) 931 .then(() => { 932 console.log('setDeviceIdleTrustlist success'); 933 }) 934 .catch((error: BusinessError) => { 935 console.log(JSON.stringify(error)); 936 }); 937``` 938 939## policy.getDeviceIdleTrustlist<sup>10+</sup> 940 941getDeviceIdleTrustlist(callback: AsyncCallback\<Array\<number>>): void 942 943Obtains the UIDs of applications that are on the device idle allowlist. This API uses an asynchronous callback to return the result. 944 945**System API**: This is a system API. 946 947**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 948 949**System capability**: SystemCapability.Communication.NetManager.Core 950 951**Parameters** 952 953| Name | Type | Mandatory| Description | 954| -------- | ------------------------------ | ---- | ------------------------ | 955| callback | AsyncCallback\<Array\<number>> | Yes | Callback used to return the result.| 956 957**Error codes** 958 959| ID| Error Message | 960| --------- | -------------------------------------------- | 961| 201 | Permission denied. | 962| 202 | Non-system applications use system APIs. | 963| 401 | Parameter error. | 964| 2100001 | Invalid parameter value. | 965| 2100002 | Operation failed. Cannot connect to service. | 966| 2100003 | System internal error. | 967 968**Example** 969 970```ts 971import { BusinessError } from '@ohos.base'; 972policy.getDeviceIdleTrustlist((error: BusinessError, data: number[]) => { 973 console.log(JSON.stringify(error)); 974 console.log(JSON.stringify(data)); 975}); 976``` 977 978## policy.getDeviceIdleTrustlist<sup>10+</sup> 979 980getDeviceIdleTrustlist(): Promise\<Array\<number>>; 981 982Obtains the UIDs of applications that are on the device idle allowlist. This API uses a promise to return the result. 983 984**System API**: This is a system API. 985 986**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 987 988**System capability**: SystemCapability.Communication.NetManager.Core 989 990**Return value** 991 992| Type | Description | 993| ------------------------ | ----------------------------- | 994| Promise\<Array\<number>> | Promise used to return the result.| 995 996**Error codes** 997 998| ID| Error Message | 999| --------- | -------------------------------------------- | 1000| 201 | Permission denied. | 1001| 202 | Non-system applications use system APIs. | 1002| 401 | Parameter error. | 1003| 2100002 | Operation failed. Cannot connect to service. | 1004| 2100003 | System internal error. | 1005 1006**Example** 1007 1008```ts 1009import { BusinessError } from '@ohos.base'; 1010policy 1011 .getDeviceIdleTrustlist() 1012 .then((data: number[]) => { 1013 console.log(JSON.stringify(data)); 1014 }) 1015 .catch((error: BusinessError) => { 1016 console.log(JSON.stringify(error)); 1017 }); 1018``` 1019 1020## policy.getBackgroundPolicyByUid<sup>10+</sup> 1021 1022getBackgroundPolicyByUid(uid: number, callback: AsyncCallback\<NetBackgroundPolicy>): void 1023 1024Obtains the background network policy for the application specified by a given UID. This API uses an asynchronous callback to return the result. 1025 1026**System API**: This is a system API. 1027 1028**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1029 1030**System capability**: SystemCapability.Communication.NetManager.Core 1031 1032**Parameters** 1033 1034| Name | Type | Mandatory| Description | 1035| -------- | ------------------------------------------------------------- | ---- | ------------------------ | 1036| uid | number | Yes | Unique ID of the application. | 1037| callback | AsyncCallback\<[NetBackgroundPolicy](#netbackgroundpolicy10)> | Yes | Callback used to return the result.| 1038 1039**Error codes** 1040 1041| ID| Error Message | 1042| --------- | -------------------------------------------- | 1043| 201 | Permission denied. | 1044| 202 | Non-system applications use system APIs. | 1045| 401 | Parameter error. | 1046| 2100001 | Invalid parameter value. | 1047| 2100002 | Operation failed. Cannot connect to service. | 1048| 2100003 | System internal error. | 1049 1050**Example** 1051 1052```ts 1053import { BusinessError } from '@ohos.base'; 1054policy.getBackgroundPolicyByUid(11111, (error: BusinessError, data: number) => { 1055 console.log(JSON.stringify(error)); 1056 console.log(JSON.stringify(data)); 1057}); 1058``` 1059 1060## policy.getBackgroundPolicyByUid<sup>10+</sup> 1061 1062getBackgroundPolicyByUid(uid: number): Promise\<NetBackgroundPolicy>; 1063 1064Obtains the background network policies configured for the specified application. This API uses a promise to return the result. 1065 1066**System API**: This is a system API. 1067 1068**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1069 1070**System capability**: SystemCapability.Communication.NetManager.Core 1071 1072**Parameters** 1073 1074| Name| Type | Mandatory| Description | 1075| ------ | ------ | ---- | -------------- | 1076| uid | number | Yes | Unique ID of the application.| 1077 1078**Return value** 1079 1080| Type | Description | 1081| ------------------------------------------------------- | ----------------------------- | 1082| Promise\<[NetBackgroundPolicy](#netbackgroundpolicy10)> | Promise used to return the result.| 1083 1084**Error codes** 1085 1086| ID| Error Message | 1087| --------- | -------------------------------------------- | 1088| 201 | Permission denied. | 1089| 202 | Non-system applications use system APIs. | 1090| 401 | Parameter error. | 1091| 2100001 | Invalid parameter value. | 1092| 2100002 | Operation failed. Cannot connect to service. | 1093| 2100003 | System internal error. | 1094 1095**Example** 1096 1097```ts 1098import { BusinessError } from '@ohos.base'; 1099policy 1100 .getBackgroundPolicyByUid(11111) 1101 .then((data: number) => { 1102 console.log(JSON.stringify(data)); 1103 }) 1104 .catch((error: BusinessError) => { 1105 console.log(JSON.stringify(error)); 1106 }); 1107``` 1108 1109## policy.resetPolicies<sup>10+</sup> 1110 1111resetPolicies(simId: string, callback: AsyncCallback\<void>): void 1112 1113Restores all the policies (cellular network, background network, firewall, and application-specific network policies) for the specified SIM card. This API uses an asynchronous callback to return the result. 1114 1115**System API**: This is a system API. 1116 1117**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1118 1119**System capability**: SystemCapability.Communication.NetManager.Core 1120 1121**Parameters** 1122 1123| Name | Type | Mandatory| Description | 1124| -------- | -------------------- | ---- | ---------------------------------------------- | 1125| simId | string | Yes | SIM card ID. | 1126| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 1127 1128**Error codes** 1129 1130| ID| Error Message | 1131| --------- | -------------------------------------------- | 1132| 201 | Permission denied. | 1133| 202 | Non-system applications use system APIs. | 1134| 401 | Parameter error. | 1135| 2100001 | Invalid parameter value. | 1136| 2100002 | Operation failed. Cannot connect to service. | 1137| 2100003 | System internal error. | 1138 1139**Example** 1140 1141```ts 1142import { BusinessError } from '@ohos.base'; 1143policy.resetPolicies('1', (error: BusinessError) => { 1144 console.log(JSON.stringify(error)); 1145}); 1146``` 1147 1148## policy.resetPolicies<sup>10+</sup> 1149 1150resetPolicies(simId: string): Promise\<void>; 1151 1152Restores all the policies (cellular network, background network, firewall, and application-specific network policies) for the specified SIM card. This API uses a promise to return the result. 1153 1154**System API**: This is a system API. 1155 1156**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1157 1158**System capability**: SystemCapability.Communication.NetManager.Core 1159 1160**Parameters** 1161 1162| Name| Type | Mandatory| Description | 1163| ------ | ------ | ---- | --------- | 1164| simId | string | Yes | SIM card ID.| 1165 1166**Return value** 1167 1168| Type | Description | 1169| -------------- | ----------------------------------------------------------------- | 1170| Promise\<void> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 1171 1172**Error codes** 1173 1174| ID| Error Message | 1175| --------- | -------------------------------------------- | 1176| 201 | Permission denied. | 1177| 202 | Non-system applications use system APIs. | 1178| 401 | Parameter error. | 1179| 2100001 | Invalid parameter value. | 1180| 2100002 | Operation failed. Cannot connect to service. | 1181| 2100003 | System internal error. | 1182 1183**Example** 1184 1185```ts 1186import { BusinessError } from '@ohos.base'; 1187policy 1188 .resetPolicies('1') 1189 .then(() => { 1190 console.log('resetPolicies success'); 1191 }) 1192 .catch((error: BusinessError) => { 1193 console.log(JSON.stringify(error)); 1194 }); 1195``` 1196 1197## policy.updateRemindPolicy<sup>10+</sup> 1198 1199updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType, callback: AsyncCallback\<void>): void 1200 1201Updates a reminder policy. This API uses an asynchronous callback to return the result. 1202 1203**System API**: This is a system API. 1204 1205**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1206 1207**System capability**: SystemCapability.Communication.NetManager.Core 1208 1209**Parameters** 1210 1211| Name | Type | Mandatory| Description | 1212| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- | 1213| netType | [NetBearType](js-apis-net-connection.md#netbeartype) | Yes | Network type. | 1214| simId | string | Yes | SIM card ID. | 1215| remindType | [RemindType](#remindtype10) | Yes | Reminder type. | 1216| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 1217 1218**Error codes** 1219 1220| ID| Error Message | 1221| --------- | -------------------------------------------- | 1222| 201 | Permission denied. | 1223| 202 | Non-system applications use system APIs. | 1224| 401 | Parameter error. | 1225| 2100001 | Invalid parameter value. | 1226| 2100002 | Operation failed. Cannot connect to service. | 1227| 2100003 | System internal error. | 1228 1229**Example** 1230 1231```ts 1232import connection from '@ohos.net.connection'; 1233import { BusinessError } from '@ohos.base'; 1234policy.updateRemindPolicy( 1235 connection.NetBearType.BEARER_CELLULAR, 1236 '1', 1237 policy.RemindType.REMIND_TYPE_WARNING, 1238 (error: BusinessError) => { 1239 console.log(JSON.stringify(error)); 1240 } 1241); 1242``` 1243 1244## policy.updateRemindPolicy<sup>10+</sup> 1245 1246updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType): Promise\<void>; 1247 1248Updates a reminder policy. This API uses a promise to return the result. 1249 1250**System API**: This is a system API. 1251 1252**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1253 1254**System capability**: SystemCapability.Communication.NetManager.Core 1255 1256**Parameters** 1257 1258| Name | Type | Mandatory| Description | 1259| ---------- | ---------------------------------------------------- | ---- | --------- | 1260| netType | [NetBearType](js-apis-net-connection.md#netbeartype) | Yes | Network type. | 1261| simId | string | Yes | SIM card ID.| 1262| remindType | [RemindType](#remindtype10) | Yes | Reminder type. | 1263 1264**Return value** 1265 1266| Type | Description | 1267| -------------- | ----------------------------------------------------------------- | 1268| Promise\<void> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 1269 1270**Error codes** 1271 1272| ID| Error Message | 1273| --------- | -------------------------------------------- | 1274| 201 | Permission denied. | 1275| 202 | Non-system applications use system APIs. | 1276| 401 | Parameter error. | 1277| 2100001 | Invalid parameter value. | 1278| 2100002 | Operation failed. Cannot connect to service. | 1279| 2100003 | System internal error. | 1280 1281**Example** 1282 1283```ts 1284import connection from '@ohos.net.connection'; 1285import { BusinessError } from '@ohos.base'; 1286policy 1287 .updateRemindPolicy( 1288 connection.NetBearType.BEARER_CELLULAR, 1289 '1', 1290 policy.RemindType.REMIND_TYPE_WARNING 1291 ) 1292 .then(() => { 1293 console.log('updateRemindPolicy success'); 1294 }) 1295 .catch((error: BusinessError) => { 1296 console.log(JSON.stringify(error)); 1297 }); 1298``` 1299 1300## policy.setPowerSaveTrustlist<sup>10+</sup> 1301 1302setPowerSaveTrustlist(uids: Array\<number>, isAllowed: boolean, callback: AsyncCallback\<void>): void 1303 1304Sets whether to add the application specified by a given UID to the power-saving allowlist. This API uses an asynchronous callback to return the result. 1305 1306**System API**: This is a system API. 1307 1308**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1309 1310**System capability**: SystemCapability.Communication.NetManager.Core 1311 1312**Parameters** 1313 1314| Name | Type | Mandatory| Description | 1315| --------- | ------------------------------ | ---- | ---------------------------------------------- | 1316| uids | Array\<number> | Yes | Unique ID of the application. | 1317| isAllowed | boolean | Yes | Whether to add the application to the allowlist. | 1318| callback | callback: AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 1319 1320**Error codes** 1321 1322| ID| Error Message | 1323| --------- | -------------------------------------------- | 1324| 201 | Permission denied. | 1325| 202 | Non-system applications use system APIs. | 1326| 401 | Parameter error. | 1327| 2100001 | Invalid parameter value. | 1328| 2100002 | Operation failed. Cannot connect to service. | 1329| 2100003 | System internal error. | 1330 1331**Example** 1332 1333```ts 1334import { BusinessError } from '@ohos.base'; 1335policy.setPowerSaveTrustlist([11111, 22222], true, (error: BusinessError) => { 1336 console.log(JSON.stringify(error)); 1337}); 1338``` 1339 1340## policy.setPowerSaveTrustlist<sup>10+</sup> 1341 1342setPowerSaveTrustlist(uids: Array\<number>, isAllowed: boolean): Promise\<void>; 1343 1344Sets whether to add the application specified by a given UID to the power-saving allowlist. This API uses a promise to return the result. 1345 1346**System API**: This is a system API. 1347 1348**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1349 1350**System capability**: SystemCapability.Communication.NetManager.Core 1351 1352**Parameters** 1353 1354| Name | Type | Mandatory| Description | 1355| --------- | -------------- | ---- | -------------- | 1356| uids | Array\<number> | Yes | Unique ID of the application.| 1357| isAllowed | boolean | Yes | Whether to add the application to the allowlist.| 1358 1359**Return value** 1360 1361| Type | Description | 1362| -------------- | ----------------------------------------------------------------- | 1363| Promise\<void> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.| 1364 1365**Error codes** 1366 1367| ID| Error Message | 1368| --------- | -------------------------------------------- | 1369| 201 | Permission denied. | 1370| 202 | Non-system applications use system APIs. | 1371| 401 | Parameter error. | 1372| 2100001 | Invalid parameter value. | 1373| 2100002 | Operation failed. Cannot connect to service. | 1374| 2100003 | System internal error. | 1375 1376**Example** 1377 1378```ts 1379import { BusinessError } from '@ohos.base'; 1380policy 1381 .setPowerSaveTrustlist([11111, 22222], true) 1382 .then(() => { 1383 console.log('setPowerSaveTrustlist success'); 1384 }) 1385 .catch((error: BusinessError) => { 1386 console.log(JSON.stringify(error)); 1387 }); 1388``` 1389 1390## policy.getPowerSaveTrustlist<sup>10+</sup> 1391 1392getPowerSaveTrustlist(callback: AsyncCallback\<Array\<number>>): void 1393 1394Obtains the UID array of applications that are on the power-saving allowlist. This API uses an asynchronous callback to return the result. 1395 1396**System API**: This is a system API. 1397 1398**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1399 1400**System capability**: SystemCapability.Communication.NetManager.Core 1401 1402**Parameters** 1403 1404| Name | Type | Mandatory| Description | 1405| -------- | ------------------------------ | ---- | ------------------------ | 1406| callback | AsyncCallback\<Array\<number>> | Yes | Callback used to return the result.| 1407 1408**Error codes** 1409 1410| ID| Error Message | 1411| --------- | -------------------------------------------- | 1412| 201 | Permission denied. | 1413| 202 | Non-system applications use system APIs. | 1414| 401 | Parameter error. | 1415| 2100001 | Invalid parameter value. | 1416| 2100002 | Operation failed. Cannot connect to service. | 1417| 2100003 | System internal error. | 1418 1419**Example** 1420 1421```ts 1422import { BusinessError } from '@ohos.base'; 1423policy.getPowerSaveTrustlist((error: BusinessError, data: number[]) => { 1424 console.log(JSON.stringify(error)); 1425 console.log(JSON.stringify(data)); 1426}); 1427``` 1428 1429## policy.getPowerSaveTrustlist<sup>10+</sup> 1430 1431getPowerSaveTrustlist(): Promise\<Array\<number>>; 1432 1433Obtains the UID array of applications that are on the device idle allowlist. This API uses a promise to return the result. 1434 1435**System API**: This is a system API. 1436 1437**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1438 1439**System capability**: SystemCapability.Communication.NetManager.Core 1440 1441**Return value** 1442 1443| Type | Description | 1444| ------------------------ | ----------------------------- | 1445| Promise\<Array\<number>> | Promise used to return the result.| 1446 1447**Error codes** 1448 1449| ID| Error Message | 1450| --------- | -------------------------------------------- | 1451| 201 | Permission denied. | 1452| 202 | Non-system applications use system APIs. | 1453| 401 | Parameter error. | 1454| 2100002 | Operation failed. Cannot connect to service. | 1455| 2100003 | System internal error. | 1456 1457**Example** 1458 1459```ts 1460import { BusinessError } from '@ohos.base'; 1461policy 1462 .getPowerSaveTrustlist() 1463 .then((data: number[]) => { 1464 console.log(JSON.stringify(data)); 1465 }) 1466 .catch((error: BusinessError) => { 1467 console.log(JSON.stringify(error)); 1468 }); 1469``` 1470 1471## policy.on 1472 1473Represents the handle to a network policy. 1474 1475### on('netUidPolicyChange')<sup>10+</sup> 1476 1477on(type: "netUidPolicyChange", callback: Callback\<{ uid: number, policy: NetUidPolicy }>): void 1478 1479Subscribes to policy changes. This API uses an asynchronous callback to return the result. 1480 1481**System API**: This is a system API. 1482 1483**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1484 1485**System capability**: SystemCapability.Communication.NetManager.Core 1486 1487**Parameters** 1488 1489| Name | Type | Mandatory| Description | 1490| -------- | ------------------------------------------------------------------- | ---- | -------------------------------------- | 1491| type | string | Yes | Event type. The value **netUidPolicyChange** indicates a policy change event. | 1492| callback | Callback\<{ uid: number, policy: [NetUidPolicy](#netuidpolicy10) }> | Yes | Callback used to return the result. It is called when the registered network policy changes.| 1493 1494**Error codes** 1495 1496| ID| Error Message | 1497| --------- | -------------------------------------------- | 1498| 201 | Permission denied. | 1499| 202 | Non-system applications use system APIs. | 1500| 401 | Parameter error. | 1501| 2100001 | Invalid parameter value. | 1502| 2100002 | Operation failed. Cannot connect to service. | 1503| 2100003 | System internal error. | 1504 1505**Example** 1506 1507```ts 1508import policy from '@ohos.net.policy'; 1509interface Data { 1510 uid: number, 1511 policy: policy.NetUidPolicy 1512} 1513policy.on('netUidPolicyChange', (data: Data) => { 1514 console.log('on netUidPolicyChange: ' + JSON.stringify(data)); 1515}); 1516``` 1517 1518### off('netUidPolicyChange')<sup>10+</sup> 1519 1520off(type: "netUidPolicyChange", callback?: Callback<{ uid: number, policy: NetUidPolicy }>): void 1521 1522Unsubscribes from policy changes. This API uses an asynchronous callback to return the result. 1523 1524**System API**: This is a system API. 1525 1526**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1527 1528**System capability**: SystemCapability.Communication.NetManager.Core 1529 1530**Parameters** 1531 1532| Name | Type | Mandatory| Description | 1533| -------- | ------------------------------------------------------------------- | ---- | -------------------------------------- | 1534| type | string | Yes | Event type. The value **netUidPolicyChange** indicates a policy change event. | 1535| callback | Callback\<{ uid: number, policy: [NetUidPolicy](#netuidpolicy10) }> | No | Callback used to return the result. It is called when the registered network policy changes.| 1536 1537**Error codes** 1538 1539| ID| Error Message | 1540| --------- | -------------------------------------------- | 1541| 201 | Permission denied. | 1542| 202 | Non-system applications use system APIs. | 1543| 401 | Parameter error. | 1544| 2100001 | Invalid parameter value. | 1545| 2100002 | Operation failed. Cannot connect to service. | 1546| 2100003 | System internal error. | 1547 1548**Example** 1549 1550```ts 1551let callback = (data: object) => { 1552 console.log('on netUidPolicyChange, data:' + JSON.stringify(data)); 1553}; 1554policy.on('netUidPolicyChange', callback); 1555policy.off('netUidPolicyChange', callback); 1556``` 1557 1558### on('netUidRuleChange')<sup>10+</sup> 1559 1560on(type: "netUidRuleChange", callback: Callback\<{ uid: number, rule: NetUidRule }>): void 1561 1562Subscribes to rule changes. This API uses an asynchronous callback to return the result. 1563 1564**System API**: This is a system API. 1565 1566**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1567 1568**System capability**: SystemCapability.Communication.NetManager.Core 1569 1570**Parameters** 1571 1572| Name | Type | Mandatory| Description | 1573| -------- | ------------------------------------------------------------- | ---- | -------------------------------------- | 1574| type | string | Yes | Event type. The value **netUidRuleChange** indicates a rule change event. | 1575| callback | Callback\<{ uid: number, rule: [NetUidRule](#netuidrule10) }> | Yes | Callback used to return the result. It is called when the registered rule changes.| 1576 1577**Error codes** 1578 1579| ID| Error Message | 1580| --------- | -------------------------------------------- | 1581| 201 | Permission denied. | 1582| 202 | Non-system applications use system APIs. | 1583| 401 | Parameter error. | 1584| 2100001 | Invalid parameter value. | 1585| 2100002 | Operation failed. Cannot connect to service. | 1586| 2100003 | System internal error. | 1587 1588**Example** 1589 1590```ts 1591policy.on('netUidRuleChange', (data: object) => { 1592 console.log('on netUidRuleChange: ' + JSON.stringify(data)); 1593}); 1594``` 1595 1596### off('netUidRuleChange')<sup>10+</sup> 1597 1598off(type: "netUidRuleChange", callback?: Callback<{ uid: number, rule: NetUidRule }>): void 1599 1600Unsubscribes from rule changes. This API uses an asynchronous callback to return the result. 1601 1602**System API**: This is a system API. 1603 1604**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1605 1606**System capability**: SystemCapability.Communication.NetManager.Core 1607 1608**Parameters** 1609 1610| Name | Type | Mandatory| Description | 1611| -------- | ------------------------------------------------------------- | ---- | -------------------------------------- | 1612| type | string | Yes | Event type. The value **netUidRuleChange** indicates a rule change event. | 1613| callback | Callback\<{ uid: number, rule: [NetUidRule](#netuidrule10) }> | No | Callback used to return the result. It is called when the registered rule changes.| 1614 1615**Error codes** 1616 1617| ID| Error Message | 1618| --------- | -------------------------------------------- | 1619| 201 | Permission denied. | 1620| 202 | Non-system applications use system APIs. | 1621| 401 | Parameter error. | 1622| 2100001 | Invalid parameter value. | 1623| 2100002 | Operation failed. Cannot connect to service. | 1624| 2100003 | System internal error. | 1625 1626**Example** 1627 1628```ts 1629let callback = (data: object) => { 1630 console.log('on netUidRuleChange, data:' + JSON.stringify(data)); 1631}; 1632policy.on('netUidRuleChange', callback); 1633policy.off('netUidRuleChange', callback); 1634``` 1635 1636### on('netMeteredIfacesChange')<sup>10+</sup> 1637 1638on(type: "netMeteredIfacesChange", callback: Callback\<Array\<string>>): void 1639 1640Subscribes to metered **iface** changes. This API uses an asynchronous callback to return the result. 1641 1642**System API**: This is a system API. 1643 1644**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1645 1646**System capability**: SystemCapability.Communication.NetManager.Core 1647 1648**Parameters** 1649 1650| Name | Type | Mandatory| Description | 1651| -------- | ------------------------- | ---- | ----------------------------------------- | 1652| type | string | Yes | Event type. The value **netMeteredIfacesChange** indicates a metered **iface** change event. | 1653| callback | Callback\<Array\<string>> | Yes | Callback used to return the result. It is called when the registered metered **iface** changes.| 1654 1655**Error codes** 1656 1657| ID| Error Message | 1658| --------- | -------------------------------------------- | 1659| 201 | Permission denied. | 1660| 202 | Non-system applications use system APIs. | 1661| 401 | Parameter error. | 1662| 2100001 | Invalid parameter value. | 1663| 2100002 | Operation failed. Cannot connect to service. | 1664| 2100003 | System internal error. | 1665 1666**Example** 1667 1668```ts 1669policy.on('netMeteredIfacesChange', (data: string[]) => { 1670 console.log('on netMeteredIfacesChange: ' + JSON.stringify(data)); 1671}); 1672``` 1673 1674### off('netMeteredIfacesChange')<sup>10+</sup> 1675 1676off(type: "netMeteredIfacesChange", callback?: Callback\<Array\<string>>): void 1677 1678Unsubscribes from metered **iface** changes. This API uses an asynchronous callback to return the result. 1679 1680**System API**: This is a system API. 1681 1682**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1683 1684**System capability**: SystemCapability.Communication.NetManager.Core 1685 1686**Parameters** 1687 1688| Name | Type | Mandatory| Description | 1689| -------- | ------------------------- | ---- | ----------------------------------------- | 1690| type | string | Yes | Event type. The value **netMeteredIfacesChange** indicates a metered **iface** change event. | 1691| callback | Callback\<Array\<string>> | No | Callback used to return the result. It is called when the registered metered **iface** changes.| 1692 1693**Error codes** 1694 1695| ID| Error Message | 1696| --------- | -------------------------------------------- | 1697| 201 | Permission denied. | 1698| 202 | Non-system applications use system APIs. | 1699| 401 | Parameter error. | 1700| 2100001 | Invalid parameter value. | 1701| 2100002 | Operation failed. Cannot connect to service. | 1702| 2100003 | System internal error. | 1703 1704**Example** 1705 1706```ts 1707let callback = (data: string[]) => { 1708 console.log('on netMeteredIfacesChange, data:' + JSON.stringify(data)); 1709}; 1710policy.on('netMeteredIfacesChange', callback); 1711policy.off('netMeteredIfacesChange', callback); 1712``` 1713 1714### on('netQuotaPolicyChange')<sup>10+</sup> 1715 1716on(type: "netQuotaPolicyChange", callback: Callback\<Array\<NetQuotaPolicy>>): void 1717 1718Subscribes to network quota policy changes. This API uses an asynchronous callback to return the result. 1719 1720**System API**: This is a system API. 1721 1722**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1723 1724**System capability**: SystemCapability.Communication.NetManager.Core 1725 1726**Parameters** 1727 1728| Name | Type | Mandatory| Description | 1729| -------- | ------------------------------------------------------ | ---- | ------------------------------------------ | 1730| type | string | Yes | Event type. The value **netQuotaPolicyChange** indicates a network quota policy change event. | 1731| callback | Callback\<Array\<[NetQuotaPolicy](#netquotapolicy10)>> | Yes | Callback used to return the result. It is called when the registered network quota policy changes.| 1732 1733**Error codes** 1734 1735| ID| Error Message | 1736| --------- | -------------------------------------------- | 1737| 201 | Permission denied. | 1738| 202 | Non-system applications use system APIs. | 1739| 401 | Parameter error. | 1740| 2100001 | Invalid parameter value. | 1741| 2100002 | Operation failed. Cannot connect to service. | 1742| 2100003 | System internal error. | 1743 1744**Example** 1745 1746```ts 1747policy.on('netQuotaPolicyChange', (data: policy.NetQuotaPolicy[]) => { 1748 console.log('on netQuotaPolicyChange: ' + JSON.stringify(data)); 1749}); 1750``` 1751 1752### off('netQuotaPolicyChange')<sup>10+</sup> 1753 1754off(type: "netQuotaPolicyChange", callback?: Callback\<Array\<NetQuotaPolicy>>): void 1755 1756Unsubscribes from network quota policy changes. This API uses an asynchronous callback to return the result. 1757 1758**System API**: This is a system API. 1759 1760**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1761 1762**System capability**: SystemCapability.Communication.NetManager.Core 1763 1764**Parameters** 1765 1766| Name | Type | Mandatory| Description | 1767| -------- | ------------------------------------------------------ | ---- | ------------------------------------------ | 1768| type | string | Yes | Event type. The value **netQuotaPolicyChange** indicates a network quota policy change event. | 1769| callback | Callback\<Array\<[NetQuotaPolicy](#netquotapolicy10)>> | No | Callback used to return the result. It is called when the registered network quota policy changes.| 1770 1771**Error codes** 1772 1773| ID| Error Message | 1774| --------- | -------------------------------------------- | 1775| 201 | Permission denied. | 1776| 202 | Non-system applications use system APIs. | 1777| 401 | Parameter error. | 1778| 2100001 | Invalid parameter value. | 1779| 2100002 | Operation failed. Cannot connect to service. | 1780| 2100003 | System internal error. | 1781 1782**Example** 1783 1784```ts 1785policy.on('netQuotaPolicyChange', (data: Array<policy.NetQuotaPolicy>) => { 1786 console.log('on netQuotaPolicyChange, data:' + JSON.stringify(data)); 1787}); 1788policy.off('netQuotaPolicyChange', (data: Array<policy.NetQuotaPolicy>) => { 1789 console.log('on netQuotaPolicyChange, data:' + JSON.stringify(data)); 1790}); 1791``` 1792 1793### on('netBackgroundPolicyChange')<sup>10+</sup> 1794 1795on(type: "netBackgroundPolicyChange", callback: Callback\<boolean>): void 1796 1797Subscribes to background network policy changes. This API uses an asynchronous callback to return the result. 1798 1799**System API**: This is a system API. 1800 1801**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1802 1803**System capability**: SystemCapability.Communication.NetManager.Core 1804 1805**Parameters** 1806 1807| Name | Type | Mandatory| Description | 1808| -------- | ------------------ | ---- | ------------------------------------------ | 1809| type | string | Yes | Event type. The value **netBackgroundPolicyChange** indicates a background network policy change event. | 1810| callback | Callback\<boolean> | Yes | Callback used to return the result. It is called when the registered background network policy changes.| 1811 1812**Error codes** 1813 1814| ID| Error Message | 1815| --------- | -------------------------------------------- | 1816| 201 | Permission denied. | 1817| 202 | Non-system applications use system APIs. | 1818| 401 | Parameter error. | 1819| 2100001 | Invalid parameter value. | 1820| 2100002 | Operation failed. Cannot connect to service. | 1821| 2100003 | System internal error. | 1822 1823**Example** 1824 1825```ts 1826policy.on('netBackgroundPolicyChange', (data: boolean) => { 1827 console.log('on netBackgroundPolicyChange: ' + JSON.stringify(data)); 1828}); 1829``` 1830 1831### off('netBackgroundPolicyChange')<sup>10+</sup> 1832 1833off(type: "netBackgroundPolicyChange", callback?: Callback\<boolean>): void 1834 1835Unsubscribes from background network policy changes. This API uses an asynchronous callback to return the result. 1836 1837**System API**: This is a system API. 1838 1839**Required permission**: ohos.permission.MANAGE_NET_STRATEGY 1840 1841**System capability**: SystemCapability.Communication.NetManager.Core 1842 1843**Parameters** 1844 1845| Name | Type | Mandatory| Description | 1846| -------- | ------------------ | ---- | ------------------------------------------ | 1847| type | string | Yes | Event type. The value **netBackgroundPolicyChange** indicates a background network policy change event. | 1848| callback | Callback\<boolean> | No | Callback used to return the result. It is called when the registered background network policy changes.| 1849 1850**Error codes** 1851 1852| ID| Error Message | 1853| --------- | -------------------------------------------- | 1854| 201 | Permission denied. | 1855| 202 | Non-system applications use system APIs. | 1856| 401 | Parameter error. | 1857| 2100001 | Invalid parameter value. | 1858| 2100002 | Operation failed. Cannot connect to service. | 1859| 2100003 | System internal error. | 1860 1861**Example** 1862 1863```ts 1864let callback = (data: boolean) => { 1865 console.log('on netBackgroundPolicyChange, data:' + JSON.stringify(data)); 1866}; 1867policy.on('netBackgroundPolicyChange', callback); 1868policy.off('netBackgroundPolicyChange', callback); 1869``` 1870 1871## NetBackgroundPolicy<sup>10+</sup> 1872 1873Enumerates the background network policies. 1874 1875**System API**: This is a system API. 1876 1877**System capability**: SystemCapability.Communication.NetManager.Core 1878 1879| Name | Value | Description | 1880| ------------------------------- | --- | ------------------------------------------ | 1881| NET_BACKGROUND_POLICY_NONE | 0 | Default policy. | 1882| NET_BACKGROUND_POLICY_ENABLE | 1 | Background applications are allowed to access a metered network. | 1883| NET_BACKGROUND_POLICY_DISABLE | 2 | Applications running in the background are not allowed to access a metered network. | 1884| NET_BACKGROUND_POLICY_TRUSTLIST | 3 | Only applications on the allowlist are allowed to access metered networks when they are running in the background.| 1885 1886## NetQuotaPolicy<sup>10+</sup> 1887 1888Defines the quota policy for the specified network. 1889 1890**System API**: This is a system API. 1891 1892**System capability**: SystemCapability.Communication.NetManager.Core 1893 1894| Name | Type | Mandatory| Description | 1895| ---------------- | --------------------------------------- | ---- | -------------------------------- | 1896| networkMatchRule | [NetworkMatchRule](#networkmatchrule10) | Yes | Network for which the quota policy is set.| 1897| quotaPolicy | [QuotaPolicy](#quotapolicy10) | Yes | Network quota policy. | 1898 1899## NetworkMatchRule<sup>10+</sup> 1900 1901Defines the network for which the quota policy is set. 1902 1903**System API**: This is a system API. 1904 1905**System capability**: SystemCapability.Communication.NetManager.Core 1906 1907| Name | Type | Mandatory| Description | 1908| -------- | ---------------------------------------------------- | ---- | ---------------------------------------------------------------------------- | 1909| netType | [NetBearType](js-apis-net-connection.md#netbeartype) | Yes | Network type. | 1910| simId | string | Yes | Identifier of the SIM card on the metered cellular network. It is not used for Wi-Fi networks. | 1911| identity | string | Yes | ID of the SIM card on the metered cellular network. It is used for Wi-Fi networks. It is used together with **iccid**.| 1912 1913## QuotaPolicy<sup>10+</sup> 1914 1915Defines the network quota policy. 1916 1917**System API**: This is a system API. 1918 1919**System capability**: SystemCapability.Communication.NetManager.Core 1920 1921| Name | Type | Mandatory| Description | 1922| ----------------- | ----------------------------- | ---- | ---------------------------------------------------------------------------------------- | 1923| periodDuration | string | Yes | Metering period for the quota limit. **D1**, **M1**, and **Y1** indicate one day, one month, and one year, respectively. If the specified metering period is exceeded, the quota is not limited.| 1924| warningBytes | number | Yes | Data volume threshold for generating an alarm. | 1925| limitBytes | number | Yes | Data volume quota. | 1926| metered | string | Yes | Whether the network is a metered network. | 1927| limitAction | [LimitAction](#limitaction10) | Yes | Action to take when the data volume quota is reached. | 1928| lastWarningRemind | string | No | Last time when an alarm was generated. | 1929| lastLimitRemind | string | No | Last time when the quota was exhausted. | 1930 1931## LimitAction<sup>10+</sup> 1932 1933Enumerates the actions that can be taken when the data volume quota is reached. 1934 1935**System API**: This is a system API. 1936 1937**System capability**: SystemCapability.Communication.NetManager.Core 1938 1939| Name | Value | Description | 1940| ---------------------------- | --- | ---------------------------------- | 1941| LIMIT_ACTION_NONE | -1 | No action is taken. This is the default value. | 1942| LIMIT_ACTION_ACCESS_DISABLED | 0 | Internet access is disabled.| 1943| LIMIT_ACTION_ALERT_ONLY | 1 | An alarm is generated when the quota limit is reached.| 1944 1945## NetUidRule<sup>10+</sup> 1946 1947Enumerates the metered network rules. 1948 1949**System API**: This is a system API. 1950 1951**System capability**: SystemCapability.Communication.NetManager.Core 1952 1953| Name | Value | Description | 1954| --------------------------------- | ------ | -------------------- | 1955| NET_RULE_NONE | 0 | Default rule. | 1956| NET_RULE_ALLOW_METERED_FOREGROUND | 1 << 0 | Applications running in the foreground are allowed to access a metered network.| 1957| NET_RULE_ALLOW_METERED | 1 << 1 | Applications are allowed to access a metered network. | 1958| NET_RULE_REJECT_METERED | 1 << 2 | Applications are not allowed to access a metered network. | 1959| NET_RULE_ALLOW_ALL | 1 << 5 | Applications are allowed to access all networks (metered or non-metered). | 1960| NET_RULE_REJECT_ALL | 1 << 6 | Applications are not allowed to access any networks (metered or non-metered). | 1961 1962## RemindType<sup>10+</sup> 1963 1964Enumerates the reminder types. 1965 1966**System API**: This is a system API. 1967 1968**System capability**: SystemCapability.Communication.NetManager.Core 1969 1970| Name | Value | Description | 1971| ------------------- | --- | -------- | 1972| REMIND_TYPE_WARNING | 1 | Warning.| 1973| REMIND_TYPE_LIMIT | 2 | Limit.| 1974 1975## NetUidPolicy<sup>10+</sup> 1976 1977Enumerates network access policies for the application. 1978 1979**System API**: This is a system API. 1980 1981**System capability**: SystemCapability.Communication.NetManager.Core 1982 1983| Name | Value | Description | 1984| ------------------------------------ | ------ | -------------------------- | 1985| NET_POLICY_NONE | 0 | Default network policy. | 1986| NET_POLICY_ALLOW_METERED_BACKGROUND | 1 << 0 | Background applications are allowed to access a metered network.| 1987| NET_POLICY_REJECT_METERED_BACKGROUND | 1 << 1 | Applications running in the background are not allowed to access a metered network.| 1988