1# @ohos.net.policy (网络策略管理)(系统接口) 2 3网络策略管理通过对用户使用数据流量进行控制管理,采用防火墙技术实现网络策略的管理。 4 5> **说明:** 6> 7> 本模块首批接口从 API version 10 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 本模块为系统接口。 9 10## 导入模块 11 12```ts 13import { policy } from '@kit.NetworkKit'; 14``` 15 16## policy.setBackgroundAllowed<sup>10+</sup> 17 18setBackgroundAllowed(isAllowed: boolean, callback: AsyncCallback\<void>): void 19 20设置是否允许后台应用访问网络,使用 callback 方式作为异步方法。 21 22**系统接口**:此接口为系统接口。 23 24**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 25 26**系统能力**:SystemCapability.Communication.NetManager.Core 27 28**参数:** 29 30| 参数名 | 类型 | 必填 | 说明 | 31| --------- | -------------------- | ---- | ------------------------------------------------------------ | 32| isAllowed | boolean | 是 | 是否允许应用后台使用数据 | 33| callback | AsyncCallback\<void> | 是 | 回调函数,成功时,err 为 undefined,失败返回错误码错误信息。 | 34 35**错误码:** 36 37| 错误码 ID | 错误信息 | 38| --------- | -------------------------------------------- | 39| 201 | Permission denied. | 40| 202 | Non-system applications use system APIs. | 41| 401 | Parameter error. | 42| 2100001 | Invalid parameter value. | 43| 2100002 | Failed to connect to the service. | 44| 2100003 | System internal error. | 45 46**示例:** 47 48```ts 49import { BusinessError } from '@kit.BasicServicesKit'; 50 51policy.setBackgroundAllowed(true, (error: BusinessError) => { 52 console.log(JSON.stringify(error)); 53}); 54``` 55 56## policy.setBackgroundAllowed<sup>10+</sup> 57 58setBackgroundAllowed(isAllowed: boolean): Promise\<void> 59 60设置是否允许后台应用访问网络,使用 Promise 方式作为异步方法。 61 62**系统接口**:此接口为系统接口。 63 64**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 65 66**系统能力**:SystemCapability.Communication.NetManager.Core 67 68**参数:** 69 70| 参数名 | 类型 | 必填 | 说明 | 71| --------- | ------- | ---- | ------------------------ | 72| isAllowed | boolean | 是 | 是否允许应用后台使用数据 | 73 74**错误码:** 75 76| 错误码 ID | 错误信息 | 77| --------- | -------------------------------------------- | 78| 201 | Permission denied. | 79| 202 | Non-system applications use system APIs. | 80| 401 | Parameter error. | 81| 2100001 | Invalid parameter value. | 82| 2100002 | Failed to connect to the service. | 83| 2100003 | System internal error. | 84 85**返回值:** 86 87| 类型 | 说明 | 88| -------------- | ----------------------------------------------------------------- | 89| Promise\<void> | 以 Promise 形式返回设定结果,成功返回空,失败返回错误码错误信息。 | 90 91**示例:** 92 93```ts 94import { BusinessError } from '@kit.BasicServicesKit'; 95 96policy.setBackgroundAllowed(true).then(() => { 97 console.log("setBackgroundAllowed success"); 98}).catch((error: BusinessError) => { 99 console.log(JSON.stringify(error)); 100}); 101``` 102 103## policy.isBackgroundAllowed<sup>10+</sup> 104 105isBackgroundAllowed(callback: AsyncCallback\<boolean>): void 106 107获取当前应用是否允许后台访问网络,使用 callback 方式作为异步方法。 108 109**系统接口**:此接口为系统接口。 110 111**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 112 113**系统能力**:SystemCapability.Communication.NetManager.Core 114 115**参数:** 116 117| 参数名 | 类型 | 必填 | 说明 | 118| -------- | ----------------------- | ---- | ---------------------------------------------------------------- | 119| callback | AsyncCallback\<boolean> | 是 | 回调函数,返回 true 代表后台策略为允许。失败返回错误码错误信息。 | 120 121**错误码:** 122 123| 错误码 ID | 错误信息 | 124| --------- | -------------------------------------------- | 125| 201 | Permission denied. | 126| 202 | Non-system applications use system APIs. | 127| 401 | Parameter error. | 128| 2100001 | Invalid parameter value. | 129| 2100002 | Failed to connect to the service. | 130| 2100003 | System internal error. | 131 132**示例:** 133 134```ts 135import { BusinessError } from '@kit.BasicServicesKit'; 136 137policy.isBackgroundAllowed((error: BusinessError, data: boolean) => { 138 console.log(JSON.stringify(error)); 139 console.log(JSON.stringify(data)); 140}); 141``` 142 143## policy.isBackgroundAllowed<sup>10+</sup> 144 145isBackgroundAllowed(): Promise\<boolean>; 146 147获取当前应用是否允许后台访问网络,使用 Promise 方式作为异步方法。 148 149**系统接口**:此接口为系统接口。 150 151**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 152 153**系统能力**:SystemCapability.Communication.NetManager.Core 154 155**返回值:** 156 157| 类型 | 说明 | 158| ----------------- | ------------------------------------------------------------------------------------ | 159| Promise\<boolean> | 以 Promise 形式返回设定结果。 返回 true 代表后台策略为允许。失败返回错误码错误信息。 | 160 161**错误码:** 162 163| 错误码 ID | 错误信息 | 164| --------- | -------------------------------------------- | 165| 201 | Permission denied. | 166| 202 | Non-system applications use system APIs. | 167| 401 | Parameter error. | 168| 2100001 | Invalid parameter value. | 169| 2100002 | Failed to connect to the service. | 170| 2100003 | System internal error. | 171 172**示例:** 173 174```ts 175import { BusinessError } from '@kit.BasicServicesKit'; 176 177policy 178 .isBackgroundAllowed() 179 .then((data: boolean) => { 180 console.log(JSON.stringify(data)); 181 }) 182 .catch((error: BusinessError) => { 183 console.log(JSON.stringify(error)); 184 }); 185``` 186 187## policy.setPolicyByUid<sup>10+</sup> 188 189setPolicyByUid(uid: number, policy: NetUidPolicy, callback: AsyncCallback\<void>): void 190 191设置对应 uid 应用是否能够访问计量网络的策略,使用 callback 方式作为异步方法。 192 193**系统接口**:此接口为系统接口。 194 195**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 196 197**系统能力**:SystemCapability.Communication.NetManager.Core 198 199**参数:** 200 201| 参数名 | 类型 | 必填 | 说明 | 202| -------- | ------------------------------- | ---- | ---------------------------------------------- | 203| uid | number | 是 | app 唯一标识符 | 204| policy | [NetUidPolicy](#netuidpolicy10) | 是 | 应用对应的策略 | 205| callback | AsyncCallback\<void> | 是 | 回调函数,成功返回空,失败返回错误码错误信息。 | 206 207**错误码:** 208 209| 错误码 ID | 错误信息 | 210| --------- | -------------------------------------------- | 211| 201 | Permission denied. | 212| 202 | Non-system applications use system APIs. | 213| 401 | Parameter error. | 214| 2100001 | Invalid parameter value. | 215| 2100002 | Failed to connect to the service. | 216| 2100003 | System internal error. | 217 218**示例:** 219 220```ts 221import { BusinessError } from '@kit.BasicServicesKit'; 222 223policy.setPolicyByUid(11111, policy.NetUidPolicy.NET_POLICY_NONE, (error: BusinessError) => { 224 console.log(JSON.stringify(error)); 225}); 226``` 227 228## policy.setPolicyByUid<sup>10+</sup> 229 230setPolicyByUid(uid: number, policy: NetUidPolicy): Promise\<void>; 231 232设置对应 uid 应用是否能够访问计量网络的策略,使用 Promise 方式作为异步方法。 233 234**系统接口**:此接口为系统接口。 235 236**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 237 238**系统能力**:SystemCapability.Communication.NetManager.Core 239 240**参数:** 241 242| 参数名 | 类型 | 必填 | 说明 | 243| ------ | ------------------------------- | ---- | -------------- | 244| uid | number | 是 | app 唯一标识符 | 245| policy | [NetUidPolicy](#netuidpolicy10) | 是 | 应用对应的策略 | 246 247**返回值:** 248 249| 类型 | 说明 | 250| -------------- | ----------------------------------------------------------------- | 251| Promise\<void> | 以 Promise 形式返回设定结果。成功返回空,失败返回错误码错误信息。 | 252 253**错误码:** 254 255| 错误码 ID | 错误信息 | 256| --------- | -------------------------------------------- | 257| 201 | Permission denied. | 258| 202 | Non-system applications use system APIs. | 259| 401 | Parameter error. | 260| 2100001 | Invalid parameter value. | 261| 2100002 | Failed to connect to the service. | 262| 2100003 | System internal error. | 263 264**示例:** 265 266```ts 267import { BusinessError } from '@kit.BasicServicesKit'; 268 269policy 270 .setPolicyByUid(11111, policy.NetUidPolicy.NET_POLICY_NONE) 271 .then(() => { 272 console.log('setPolicyByUid success'); 273 }) 274 .catch((error: BusinessError) => { 275 console.log(JSON.stringify(error)); 276 }); 277``` 278 279## policy.getPolicyByUid<sup>10+</sup> 280 281getPolicyByUid(uid: number, callback: AsyncCallback\<NetUidPolicy>): void 282 283通过应用 uid 获取对应访问网络策略,使用 callback 方式作为异步方法。 284 285**系统接口**:此接口为系统接口。 286 287**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 288 289**系统能力**:SystemCapability.Communication.NetManager.Core 290 291**参数:** 292 293| 参数名 | 类型 | 必填 | 说明 | 294| -------- | ----------------------------------------------- | ---- | -------------------------------------------------------- | 295| uid | number | 是 | app 唯一标识符 | 296| callback | AsyncCallback\<[NetUidPolicy](#netuidpolicy10)> | 是 | 回调函数,成功返回获取策略结果,失败返回错误码错误信息。 | 297 298**错误码:** 299 300| 错误码 ID | 错误信息 | 301| --------- | -------------------------------------------- | 302| 201 | Permission denied. | 303| 202 | Non-system applications use system APIs. | 304| 401 | Parameter error. | 305| 2100001 | Invalid parameter value. | 306| 2100002 | Failed to connect to the service. | 307| 2100003 | System internal error. | 308 309**示例:** 310 311```ts 312import { BusinessError } from '@kit.BasicServicesKit'; 313 314policy.getPolicyByUid(11111, (error: BusinessError, data: policy.NetUidPolicy) => { 315 console.log(JSON.stringify(error)); 316 console.log(JSON.stringify(data)); 317}); 318``` 319 320## policy.getPolicyByUid<sup>10+</sup> 321 322getPolicyByUid(uid: number): Promise\<NetUidPolicy>; 323 324通过应用 uid 获取对应访问网络策略,使用 Promise 方式作为异步方法。 325 326**系统接口**:此接口为系统接口。 327 328**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 329 330**系统能力**:SystemCapability.Communication.NetManager.Core 331 332**参数:** 333 334| 参数名 | 类型 | 必填 | 说明 | 335| ------ | ------ | ---- | -------------- | 336| uid | number | 是 | app 唯一标识符 | 337 338**返回值:** 339 340| 类型 | 说明 | 341| ----------------------------------------- | --------------------------------------------------------- | 342| Promise\<[NetUidPolicy](#netuidpolicy10)> | 以 Promise 形式返回获取策略结果。失败返回错误码错误信息。 | 343 344**错误码:** 345 346| 错误码 ID | 错误信息 | 347| --------- | -------------------------------------------- | 348| 201 | Permission denied. | 349| 202 | Non-system applications use system APIs. | 350| 401 | Parameter error. | 351| 2100001 | Invalid parameter value. | 352| 2100002 | Failed to connect to the service. | 353| 2100003 | System internal error. | 354 355**示例:** 356 357```ts 358import { BusinessError } from '@kit.BasicServicesKit'; 359 360policy 361 .getPolicyByUid(11111) 362 .then((data: policy.NetUidPolicy) => { 363 console.log(JSON.stringify(data)); 364 }) 365 .catch((error: BusinessError) => { 366 console.log(JSON.stringify(error)); 367 }); 368``` 369 370## policy.getUidsByPolicy<sup>10+</sup> 371 372getUidsByPolicy(policy: NetUidPolicy, callback: AsyncCallback\<Array\<number>>): void 373 374通过策略获取跟策略匹配的所有 uid,使用 callback 方式作为异步方法。 375 376**系统接口**:此接口为系统接口。 377 378**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 379 380**系统能力**:SystemCapability.Communication.NetManager.Core 381 382**参数:** 383 384| 参数名 | 类型 | 必填 | 说明 | 385| -------- | ------------------------------- | ---- | ----------------------------------------------------------- | 386| policy | [NetUidPolicy](#netuidpolicy10) | 是 | 应用对应的计量网络下的策略 | 387| callback | AsyncCallback\<Array\<number>> | 是 | 回调函数,成功返回应用的 uid 数组,失败返回错误码错误信息。 | 388 389**错误码:** 390 391| 错误码 ID | 错误信息 | 392| --------- | -------------------------------------------- | 393| 201 | Permission denied. | 394| 202 | Non-system applications use system APIs. | 395| 401 | Parameter error. | 396| 2100001 | Invalid parameter value. | 397| 2100002 | Failed to connect to the service. | 398| 2100003 | System internal error. | 399 400**示例:** 401 402```ts 403import { BusinessError } from '@kit.BasicServicesKit'; 404 405policy.getUidsByPolicy(11111, (error: BusinessError, data: number[]) => { 406 console.log(JSON.stringify(error)); 407 console.log(JSON.stringify(data)); 408}); 409``` 410 411## policy.getUidsByPolicy<sup>10+</sup> 412 413getUidsByPolicy(policy: NetUidPolicy): Promise\<Array\<number>>; 414 415通过策略获取跟策略匹配的所有 uid,使用 Promise 方式作为异步方法。 416 417**系统接口**:此接口为系统接口。 418 419**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 420 421**系统能力**:SystemCapability.Communication.NetManager.Core 422 423**参数:** 424 425| 参数名 | 类型 | 必填 | 说明 | 426| ------ | ------------------------------- | ---- | -------------------------- | 427| policy | [NetUidPolicy](#netuidpolicy10) | 是 | app 对应的计量网络下的策略 | 428 429**返回值:** 430 431| 类型 | 说明 | 432| ------------------------ | ------------------------------------------------------------ | 433| Promise\<Array\<number>> | 以 Promise 形式返回应用的 uid 数组,失败返回错误码错误信息。 | 434 435**错误码:** 436 437| 错误码 ID | 错误信息 | 438| --------- | -------------------------------------------- | 439| 201 | Permission denied. | 440| 202 | Non-system applications use system APIs. | 441| 401 | Parameter error. | 442| 2100001 | Invalid parameter value. | 443| 2100002 | Failed to connect to the service. | 444| 2100003 | System internal error. | 445 446**示例:** 447 448```ts 449import { BusinessError } from '@kit.BasicServicesKit'; 450 451policy 452 .getUidsByPolicy(11111) 453 .then((data: object) => { 454 console.log(JSON.stringify(data)); 455 }) 456 .catch((error: BusinessError) => { 457 console.log(JSON.stringify(error)); 458 }); 459``` 460 461## policy.getNetQuotaPolicies<sup>10+</sup> 462 463getNetQuotaPolicies(callback: AsyncCallback\<Array\<NetQuotaPolicy>>): void 464 465获取计量网络策略,使用 callback 方式作为异步方法。 466 467**系统接口**:此接口为系统接口。 468 469**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 470 471**系统能力**:SystemCapability.Communication.NetManager.Core 472 473**参数:** 474 475| 参数名 | 类型 | 必填 | 说明 | 476| -------- | ----------------------------------------------------------- | ---- | ------------------------ | 477| callback | AsyncCallback\<Array\<[NetQuotaPolicy](#netquotapolicy10)>> | 是 | 回调函数,返回获取结果。 | 478 479**错误码:** 480 481| 错误码 ID | 错误信息 | 482| --------- | -------------------------------------------- | 483| 201 | Permission denied. | 484| 202 | Non-system applications use system APIs. | 485| 401 | Parameter error. | 486| 2100001 | Invalid parameter value. | 487| 2100002 | Failed to connect to the service. | 488| 2100003 | System internal error. | 489 490**示例:** 491 492```ts 493import { BusinessError } from '@kit.BasicServicesKit'; 494 495policy.getNetQuotaPolicies((error: BusinessError, data: policy.NetQuotaPolicy[]) => { 496 console.log(JSON.stringify(error)); 497 console.log(JSON.stringify(data)); 498}); 499``` 500 501## policy.getNetQuotaPolicies<sup>10+</sup> 502 503getNetQuotaPolicies(): Promise\<Array\<NetQuotaPolicy>>; 504 505获取计量网络策略,使用 Promise 方式作为异步方法。 506 507**系统接口**:此接口为系统接口。 508 509**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 510 511**系统能力**:SystemCapability.Communication.NetManager.Core 512 513**返回值:** 514 515| 类型 | 说明 | 516| ----------------------------------------------------- | ----------------------------- | 517| Promise\<Array\<[NetQuotaPolicy](#netquotapolicy10)>> | 以 Promise 形式返回设定结果。 | 518 519**错误码:** 520 521| 错误码 ID | 错误信息 | 522| --------- | -------------------------------------------- | 523| 201 | Permission denied. | 524| 202 | Non-system applications use system APIs. | 525| 401 | Parameter error. | 526| 2100002 | Failed to connect to the service. | 527| 2100003 | System internal error. | 528 529**示例:** 530 531```ts 532import { BusinessError } from '@kit.BasicServicesKit'; 533 534policy 535 .getNetQuotaPolicies() 536 .then((data: policy.NetQuotaPolicy[]) => { 537 console.log(JSON.stringify(data)); 538 }) 539 .catch((error: BusinessError) => { 540 console.log(JSON.stringify(error)); 541 }); 542``` 543 544## policy.setNetQuotaPolicies<sup>10+</sup> 545 546setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>, callback: AsyncCallback\<void>): void 547 548设置计量网络策略,使用 callback 方式作为异步方法。 549 550**系统接口**:此接口为系统接口。 551 552**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 553 554**系统能力**:SystemCapability.Communication.NetManager.Core 555 556**参数:** 557 558| 参数名 | 类型 | 必填 | 说明 | 559| ------------- | ------------------------------------------- | ---- | ---------------------------------------------- | 560| quotaPolicies | Array\<[NetQuotaPolicy](#netquotapolicy10)> | 是 | 计量网络策略 | 561| callback | AsyncCallback\<void> | 是 | 回调函数,成功返回空,失败返回错误码错误信息。 | 562 563**错误码:** 564 565| 错误码 ID | 错误信息 | 566| --------- | -------------------------------------------- | 567| 201 | Permission denied. | 568| 202 | Non-system applications use system APIs. | 569| 401 | Parameter error. | 570| 2100001 | Invalid parameter value. | 571| 2100002 | Failed to connect to the service. | 572| 2100003 | System internal error. | 573 574**示例:** 575 576```ts 577import { connection } from '@kit.NetworkKit'; 578import { BusinessError } from '@kit.BasicServicesKit'; 579 580let netQuotaPolicyList: Array<policy.NetQuotaPolicy> = []; 581let netquotapolicy: policy.NetQuotaPolicy = { 582 networkMatchRule: { 583 netType: connection.NetBearType.BEARER_CELLULAR, 584 identity: '', 585 simId: '1' 586 }, 587 quotaPolicy: { 588 periodDuration: 'M1', 589 warningBytes: 40000, 590 limitBytes: 50000, 591 metered: true, 592 limitAction: policy.LimitAction.LIMIT_ACTION_NONE 593 } 594} 595netQuotaPolicyList.push(netquotapolicy); 596 597policy.setNetQuotaPolicies(netQuotaPolicyList, (error: BusinessError) => { 598 console.log(JSON.stringify(error)); 599}); 600``` 601 602## policy.setNetQuotaPolicies<sup>10+</sup> 603 604setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>): Promise\<void>; 605 606设置计量网络策略,使用 Promise 方式作为异步方法。 607 608**系统接口**:此接口为系统接口。 609 610**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 611 612**系统能力**:SystemCapability.Communication.NetManager.Core 613 614**参数:** 615 616| 参数名 | 类型 | 必填 | 说明 | 617| ------------- | ------------------------------------------- | ---- | ------------ | 618| quotaPolicies | Array\<[NetQuotaPolicy](#netquotapolicy10)> | 是 | 计量网络策略 | 619 620**错误码:** 621 622| 错误码 ID | 错误信息 | 623| --------- | -------------------------------------------- | 624| 201 | Permission denied. | 625| 202 | Non-system applications use system APIs. | 626| 401 | Parameter error. | 627| 2100001 | Invalid parameter value. | 628| 2100002 | Failed to connect to the service. | 629| 2100003 | System internal error. | 630 631**返回值:** 632 633| 类型 | 说明 | 634| -------------- | ----------------------------------------------------------------- | 635| Promise\<void> | 以 Promise 形式返回设定结果。成功返回空,失败返回错误码错误信息。 | 636 637**示例:** 638 639```ts 640import { connection } from '@kit.NetworkKit'; 641import { BusinessError } from '@kit.BasicServicesKit'; 642 643let netQuotaPolicyList: Array<policy.NetQuotaPolicy> = []; 644let netquotapolicy: policy.NetQuotaPolicy = { 645 networkMatchRule: { 646 netType: connection.NetBearType.BEARER_CELLULAR, 647 identity: '', 648 simId: '1' 649 }, 650 quotaPolicy: { 651 periodDuration: 'M1', 652 warningBytes: 40000, 653 limitBytes: 50000, 654 metered: true, 655 limitAction: policy.LimitAction.LIMIT_ACTION_NONE 656 } 657} 658netQuotaPolicyList.push(netquotapolicy); 659 660policy 661 .setNetQuotaPolicies(netQuotaPolicyList) 662 .then(() => { 663 console.log('setNetQuotaPolicies success'); 664 }) 665 .catch((error: BusinessError) => { 666 console.log(JSON.stringify(error)); 667 }); 668``` 669 670## policy.isUidNetAllowed<sup>10+</sup> 671 672isUidNetAllowed(uid: number, isMetered: boolean, callback: AsyncCallback\<boolean>): void 673 674判断对应 uid 能否访问计量或非计量网络,使用 callback 方式作为异步方法。 675 676**系统接口**:此接口为系统接口。 677 678**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 679 680**系统能力**:SystemCapability.Communication.NetManager.Core 681 682**参数:** 683 684| 参数名 | 类型 | 必填 | 说明 | 685| --------- | ----------------------- | ---- | --------------------------------------------------------- | 686| uid | number | 是 | app 唯一标识符 | 687| isMetered | boolean | 是 | 是否为计量网络 | 688| callback | AsyncCallback\<boolean> | 是 | 回调函数,返回 true 表示这个 uid 可以访问对应的计量网络。 | 689 690**错误码:** 691 692| 错误码 ID | 错误信息 | 693| --------- | -------------------------------------------- | 694| 201 | Permission denied. | 695| 202 | Non-system applications use system APIs. | 696| 401 | Parameter error. | 697| 2100001 | Invalid parameter value. | 698| 2100002 | Failed to connect to the service. | 699| 2100003 | System internal error. | 700 701**示例:** 702 703```ts 704import { BusinessError } from '@kit.BasicServicesKit'; 705 706policy.isUidNetAllowed(11111, true, (error: BusinessError, data: boolean) => { 707 console.log(JSON.stringify(error)); 708 console.log(JSON.stringify(data)); 709}); 710``` 711 712## policy.isUidNetAllowed<sup>10+</sup> 713 714isUidNetAllowed(uid: number, isMetered: boolean): Promise\<boolean>; 715 716判断对应 uid 能否访问计量或非计量网络,使用 Promise 方式作为异步方法。 717 718**系统接口**:此接口为系统接口。 719 720**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 721 722**系统能力**:SystemCapability.Communication.NetManager.Core 723 724**参数:** 725 726| 参数名 | 类型 | 必填 | 说明 | 727| --------- | ------- | ---- | -------------- | 728| uid | number | 是 | app 唯一标识符 | 729| isMetered | boolean | 是 | 是否为计量网络 | 730 731**返回值:** 732 733| 类型 | 说明 | 734| ----------------- | ----------------------------- | 735| Promise\<boolean> | 以 Promise 形式返回设定结果。 | 736 737**错误码:** 738 739| 错误码 ID | 错误信息 | 740| --------- | -------------------------------------------- | 741| 201 | Permission denied. | 742| 202 | Non-system applications use system APIs. | 743| 401 | Parameter error. | 744| 2100001 | Invalid parameter value. | 745| 2100002 | Failed to connect to the service. | 746| 2100003 | System internal error. | 747 748**示例:** 749 750```ts 751import { BusinessError } from '@kit.BasicServicesKit'; 752 753policy 754 .isUidNetAllowed(11111, true) 755 .then((data: boolean) => { 756 console.log(JSON.stringify(data)); 757 }) 758 .catch((error: BusinessError) => { 759 console.log(JSON.stringify(error)); 760 }); 761``` 762 763## policy.isUidNetAllowed<sup>10+</sup> 764 765isUidNetAllowed(uid: number, iface: string, callback: AsyncCallback\<boolean>): void 766 767获取对应 uid 能否访问指定的 iface 的网络,使用 callback 方式作为异步方法。 768 769**系统接口**:此接口为系统接口。 770 771**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 772 773**系统能力**:SystemCapability.Communication.NetManager.Core 774 775**参数:** 776 777| 参数名 | 类型 | 必填 | 说明 | 778| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 779| uid | number | 是 | app 唯一标识符 | 780| iface | string | 是 | 网络对应的名称 | 781| callback | AsyncCallback\<boolean> | 是 | 回调函数,返回 true 表示这个 uid 可以访问对应 iface 的网络。 | 782 783**错误码:** 784 785| 错误码 ID | 错误信息 | 786| --------- | -------------------------------------------- | 787| 201 | Permission denied. | 788| 202 | Non-system applications use system APIs. | 789| 401 | Parameter error. | 790| 2100001 | Invalid parameter value. | 791| 2100002 | Failed to connect to the service. | 792| 2100003 | System internal error. | 793 794**示例:** 795 796```ts 797import { BusinessError } from '@kit.BasicServicesKit'; 798 799policy.isUidNetAllowed(11111, 'wlan0', (error: BusinessError, data: boolean) => { 800 console.log(JSON.stringify(error)); 801 console.log(JSON.stringify(data)); 802}); 803``` 804 805## policy.isUidNetAllowed<sup>10+</sup> 806 807isUidNetAllowed(uid: number, iface: string): Promise\<boolean>; 808 809获取对应 uid 能否访问指定的 iface 的网络,使用 Promise 方式作为异步方法。 810 811**系统接口**:此接口为系统接口。 812 813**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 814 815**系统能力**:SystemCapability.Communication.NetManager.Core 816 817**参数:** 818 819| 参数名 | 类型 | 必填 | 说明 | 820| ------ | ------ | ---- | -------------- | 821| uid | number | 是 | app 唯一标识符 | 822| iface | string | 是 | 网络对应的名称 | 823 824**返回值:** 825 826| 类型 | 说明 | 827| ----------------- | ------------------------------------------------------- | 828| Promise\<boolean> | 以 Promise 形式返回当前 uid 能否访问对应 iface 的网络。 | 829 830**错误码:** 831 832| 错误码 ID | 错误信息 | 833| --------- | -------------------------------------------- | 834| 201 | Permission denied. | 835| 202 | Non-system applications use system APIs. | 836| 401 | Parameter error. | 837| 2100001 | Invalid parameter value. | 838| 2100002 | Failed to connect to the service. | 839| 2100003 | System internal error. | 840 841**示例:** 842 843```ts 844import { BusinessError } from '@kit.BasicServicesKit'; 845 846policy 847 .isUidNetAllowed(11111, 'wlan0') 848 .then((data: boolean) => { 849 console.log(JSON.stringify(data)); 850 }) 851 .catch((error: BusinessError) => { 852 console.log(JSON.stringify(error)); 853 }); 854``` 855 856## policy.setDeviceIdleTrustlist<sup>10+</sup> 857 858setDeviceIdleTrustlist(uids: Array\<number>, isAllowed: boolean, callback: AsyncCallback\<void>): void 859 860设置多个 uid 是否在休眠防火墙的白名单,使用 callback 方式作为异步方法。 861 862**系统接口**:此接口为系统接口。 863 864**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 865 866**系统能力**:SystemCapability.Communication.NetManager.Core 867 868**参数:** 869 870| 参数名 | 类型 | 必填 | 说明 | 871| --------- | ------------------------------ | ---- | ---------------------------------------------- | 872| uids | Array\<number> | 是 | app 唯一标识符 | 873| isAllowed | boolean | 是 | 是否加入白名单 | 874| callback | callback: AsyncCallback\<void> | 是 | 回调函数,成功返回空,失败返回错误码错误信息。 | 875 876**错误码:** 877 878| 错误码 ID | 错误信息 | 879| --------- | -------------------------------------------- | 880| 201 | Permission denied. | 881| 202 | Non-system applications use system APIs. | 882| 401 | Parameter error. | 883| 2100001 | Invalid parameter value. | 884| 2100002 | Failed to connect to the service. | 885| 2100003 | System internal error. | 886 887**示例:** 888 889```ts 890import { BusinessError } from '@kit.BasicServicesKit'; 891 892policy.setDeviceIdleTrustlist([11111, 22222], true, (error: BusinessError) => { 893 console.log(JSON.stringify(error)); 894}); 895``` 896 897## policy.setDeviceIdleTrustlist<sup>10+</sup> 898 899setDeviceIdleTrustlist(uids: Array\<number>, isAllowed: boolean): Promise\<void>; 900 901设置多个 uid 是否在休眠防火墙的白名单,使用 Promise 方式作为异步方法。 902 903**系统接口**:此接口为系统接口。 904 905**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 906 907**系统能力**:SystemCapability.Communication.NetManager.Core 908 909**参数:** 910 911| 参数名 | 类型 | 必填 | 说明 | 912| --------- | -------------- | ---- | -------------- | 913| uids | Array\<number> | 是 | app 唯一标识符 | 914| isAllowed | boolean | 是 | 是否加入白名单 | 915 916**返回值:** 917 918| 类型 | 说明 | 919| -------------- | ----------------------------------------------------------------- | 920| Promise\<void> | 以 Promise 形式返回设定结果。成功返回空,失败返回错误码错误信息。 | 921 922**错误码:** 923 924| 错误码 ID | 错误信息 | 925| --------- | -------------------------------------------- | 926| 201 | Permission denied. | 927| 202 | Non-system applications use system APIs. | 928| 401 | Parameter error. | 929| 2100001 | Invalid parameter value. | 930| 2100002 | Failed to connect to the service. | 931| 2100003 | System internal error. | 932 933**示例:** 934 935```ts 936import { BusinessError } from '@kit.BasicServicesKit'; 937 938policy 939 .setDeviceIdleTrustlist([11111, 22222], true) 940 .then(() => { 941 console.log('setDeviceIdleTrustlist success'); 942 }) 943 .catch((error: BusinessError) => { 944 console.log(JSON.stringify(error)); 945 }); 946``` 947 948## policy.getDeviceIdleTrustlist<sup>10+</sup> 949 950getDeviceIdleTrustlist(callback: AsyncCallback\<Array\<number>>): void 951 952获取休眠模式白名单所包含的 uid,使用 callback 方式作为异步方法。 953 954**系统接口**:此接口为系统接口。 955 956**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 957 958**系统能力**:SystemCapability.Communication.NetManager.Core 959 960**参数:** 961 962| 参数名 | 类型 | 必填 | 说明 | 963| -------- | ------------------------------ | ---- | ------------------------ | 964| callback | AsyncCallback\<Array\<number>> | 是 | 回调函数,返回获取结果。 | 965 966**错误码:** 967 968| 错误码 ID | 错误信息 | 969| --------- | -------------------------------------------- | 970| 201 | Permission denied. | 971| 202 | Non-system applications use system APIs. | 972| 401 | Parameter error. | 973| 2100001 | Invalid parameter value. | 974| 2100002 | Failed to connect to the service. | 975| 2100003 | System internal error. | 976 977**示例:** 978 979```ts 980import { BusinessError } from '@kit.BasicServicesKit'; 981 982policy.getDeviceIdleTrustlist((error: BusinessError, data: number[]) => { 983 console.log(JSON.stringify(error)); 984 console.log(JSON.stringify(data)); 985}); 986``` 987 988## policy.getDeviceIdleTrustlist<sup>10+</sup> 989 990getDeviceIdleTrustlist(): Promise\<Array\<number>>; 991 992获取休眠模式白名单所包含的 uid,使用 Promise 方式作为异步方法。 993 994**系统接口**:此接口为系统接口。 995 996**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 997 998**系统能力**:SystemCapability.Communication.NetManager.Core 999 1000**返回值:** 1001 1002| 类型 | 说明 | 1003| ------------------------ | ----------------------------- | 1004| Promise\<Array\<number>> | 以 Promise 形式返回设定结果。 | 1005 1006**错误码:** 1007 1008| 错误码 ID | 错误信息 | 1009| --------- | -------------------------------------------- | 1010| 201 | Permission denied. | 1011| 202 | Non-system applications use system APIs. | 1012| 401 | Parameter error. | 1013| 2100002 | Failed to connect to the service. | 1014| 2100003 | System internal error. | 1015 1016**示例:** 1017 1018```ts 1019import { BusinessError } from '@kit.BasicServicesKit'; 1020 1021policy 1022 .getDeviceIdleTrustlist() 1023 .then((data: number[]) => { 1024 console.log(JSON.stringify(data)); 1025 }) 1026 .catch((error: BusinessError) => { 1027 console.log(JSON.stringify(error)); 1028 }); 1029``` 1030 1031## policy.getBackgroundPolicyByUid<sup>10+</sup> 1032 1033getBackgroundPolicyByUid(uid: number, callback: AsyncCallback\<NetBackgroundPolicy>): void 1034 1035获取指定 uid 是否能访问后台网络,使用 callback 方式作为异步方法。 1036 1037**系统接口**:此接口为系统接口。 1038 1039**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1040 1041**系统能力**:SystemCapability.Communication.NetManager.Core 1042 1043**参数:** 1044 1045| 参数名 | 类型 | 必填 | 说明 | 1046| -------- | ------------------------------------------------------------- | ---- | ------------------------ | 1047| uid | number | 是 | app 唯一标识符 | 1048| callback | AsyncCallback\<[NetBackgroundPolicy](#netbackgroundpolicy10)> | 是 | 回调函数,返回获取结果。 | 1049 1050**错误码:** 1051 1052| 错误码 ID | 错误信息 | 1053| --------- | -------------------------------------------- | 1054| 201 | Permission denied. | 1055| 202 | Non-system applications use system APIs. | 1056| 401 | Parameter error. | 1057| 2100001 | Invalid parameter value. | 1058| 2100002 | Failed to connect to the service. | 1059| 2100003 | System internal error. | 1060 1061**示例:** 1062 1063```ts 1064import { BusinessError } from '@kit.BasicServicesKit'; 1065 1066policy.getBackgroundPolicyByUid(11111, (error: BusinessError, data: policy.NetBackgroundPolicy) => { 1067 console.log(JSON.stringify(error)); 1068 console.log(JSON.stringify(data)); 1069}); 1070``` 1071 1072## policy.getBackgroundPolicyByUid<sup>10+</sup> 1073 1074getBackgroundPolicyByUid(uid: number): Promise\<NetBackgroundPolicy>; 1075 1076获取指定 uid 能否访问后台网络,使用 Promise 方式作为异步方法。 1077 1078**系统接口**:此接口为系统接口。 1079 1080**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1081 1082**系统能力**:SystemCapability.Communication.NetManager.Core 1083 1084**参数:** 1085 1086| 参数名 | 类型 | 必填 | 说明 | 1087| ------ | ------ | ---- | -------------- | 1088| uid | number | 是 | app 唯一标识符 | 1089 1090**返回值:** 1091 1092| 类型 | 说明 | 1093| ------------------------------------------------------- | ----------------------------- | 1094| Promise\<[NetBackgroundPolicy](#netbackgroundpolicy10)> | 以 Promise 形式返回设定结果。 | 1095 1096**错误码:** 1097 1098| 错误码 ID | 错误信息 | 1099| --------- | -------------------------------------------- | 1100| 201 | Permission denied. | 1101| 202 | Non-system applications use system APIs. | 1102| 401 | Parameter error. | 1103| 2100001 | Invalid parameter value. | 1104| 2100002 | Failed to connect to the service. | 1105| 2100003 | System internal error. | 1106 1107**示例:** 1108 1109```ts 1110import { BusinessError } from '@kit.BasicServicesKit'; 1111 1112policy 1113 .getBackgroundPolicyByUid(11111) 1114 .then((data: policy.NetBackgroundPolicy) => { 1115 console.log(JSON.stringify(data)); 1116 }) 1117 .catch((error: BusinessError) => { 1118 console.log(JSON.stringify(error)); 1119 }); 1120``` 1121 1122## policy.resetPolicies<sup>10+</sup> 1123 1124resetPolicies(simId: string, callback: AsyncCallback\<void>): void 1125 1126重置对应 sim 卡 id 的蜂窝网络、后台网络策略、防火墙策略、应用对应的策略,使用 callback 方式作为异步方法。 1127 1128**系统接口**:此接口为系统接口。 1129 1130**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1131 1132**系统能力**:SystemCapability.Communication.NetManager.Core 1133 1134**参数:** 1135 1136| 参数名 | 类型 | 必填 | 说明 | 1137| -------- | -------------------- | ---- | ---------------------------------------------- | 1138| simId | string | 是 | SIM 卡 ID | 1139| callback | AsyncCallback\<void> | 是 | 回调函数,成功返回空,失败返回错误码错误信息。 | 1140 1141**错误码:** 1142 1143| 错误码 ID | 错误信息 | 1144| --------- | -------------------------------------------- | 1145| 201 | Permission denied. | 1146| 202 | Non-system applications use system APIs. | 1147| 401 | Parameter error. | 1148| 2100001 | Invalid parameter value. | 1149| 2100002 | Failed to connect to the service. | 1150| 2100003 | System internal error. | 1151 1152**示例:** 1153 1154```ts 1155import { BusinessError } from '@kit.BasicServicesKit'; 1156 1157policy.resetPolicies('1', (error: BusinessError) => { 1158 console.log(JSON.stringify(error)); 1159}); 1160``` 1161 1162## policy.resetPolicies<sup>10+</sup> 1163 1164resetPolicies(simId: string): Promise\<void>; 1165 1166重置对应 sim 卡 id 的蜂窝网络、后台网络策略、防火墙策略、应用对应的策略,使用 Promise 方式作为异步方法。 1167 1168**系统接口**:此接口为系统接口。 1169 1170**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1171 1172**系统能力**:SystemCapability.Communication.NetManager.Core 1173 1174**参数:** 1175 1176| 参数名 | 类型 | 必填 | 说明 | 1177| ------ | ------ | ---- | --------- | 1178| simId | string | 是 | SIM 卡 ID | 1179 1180**返回值:** 1181 1182| 类型 | 说明 | 1183| -------------- | ----------------------------------------------------------------- | 1184| Promise\<void> | 以 Promise 形式返回设定结果。成功返回空,失败返回错误码错误信息。 | 1185 1186**错误码:** 1187 1188| 错误码 ID | 错误信息 | 1189| --------- | -------------------------------------------- | 1190| 201 | Permission denied. | 1191| 202 | Non-system applications use system APIs. | 1192| 401 | Parameter error. | 1193| 2100001 | Invalid parameter value. | 1194| 2100002 | Failed to connect to the service. | 1195| 2100003 | System internal error. | 1196 1197**示例:** 1198 1199```ts 1200import { BusinessError } from '@kit.BasicServicesKit'; 1201 1202policy 1203 .resetPolicies('1') 1204 .then(() => { 1205 console.log('resetPolicies success'); 1206 }) 1207 .catch((error: BusinessError) => { 1208 console.log(JSON.stringify(error)); 1209 }); 1210``` 1211 1212## policy.updateRemindPolicy<sup>10+</sup> 1213 1214updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType, callback: AsyncCallback\<void>): void 1215 1216更新提醒策略,使用 callback 方式作为异步方法。 1217 1218**系统接口**:此接口为系统接口。 1219 1220**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1221 1222**系统能力**:SystemCapability.Communication.NetManager.Core 1223 1224**参数:** 1225 1226| 参数名 | 类型 | 必填 | 说明 | 1227| ---------- | ---------------------------------------------------- | ---- | ---------------------------------------------- | 1228| netType | [NetBearType](js-apis-net-connection.md#netbeartype) | 是 | 网络类型 | 1229| simId | string | 是 | SIM 卡 ID | 1230| remindType | [RemindType](#remindtype10) | 是 | 提醒类型 | 1231| callback | AsyncCallback\<void> | 是 | 回调函数,成功返回空,失败返回错误码错误信息。 | 1232 1233**错误码:** 1234 1235| 错误码 ID | 错误信息 | 1236| --------- | -------------------------------------------- | 1237| 201 | Permission denied. | 1238| 202 | Non-system applications use system APIs. | 1239| 401 | Parameter error. | 1240| 2100001 | Invalid parameter value. | 1241| 2100002 | Failed to connect to the service. | 1242| 2100003 | System internal error. | 1243 1244**示例:** 1245 1246```ts 1247import { connection } from '@kit.NetworkKit'; 1248import { BusinessError } from '@kit.BasicServicesKit'; 1249 1250policy.updateRemindPolicy( 1251 connection.NetBearType.BEARER_CELLULAR, 1252 '1', 1253 policy.RemindType.REMIND_TYPE_WARNING, 1254 (error: BusinessError) => { 1255 console.log(JSON.stringify(error)); 1256 } 1257); 1258``` 1259 1260## policy.updateRemindPolicy<sup>10+</sup> 1261 1262updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType): Promise\<void>; 1263 1264更新提醒策略,使用 Promise 方式作为异步方法。 1265 1266**系统接口**:此接口为系统接口。 1267 1268**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1269 1270**系统能力**:SystemCapability.Communication.NetManager.Core 1271 1272**参数:** 1273 1274| 参数名 | 类型 | 必填 | 说明 | 1275| ---------- | ---------------------------------------------------- | ---- | --------- | 1276| netType | [NetBearType](js-apis-net-connection.md#netbeartype) | 是 | 网络类型 | 1277| simId | string | 是 | SIM 卡 ID | 1278| remindType | [RemindType](#remindtype10) | 是 | 提醒类型 | 1279 1280**返回值:** 1281 1282| 类型 | 说明 | 1283| -------------- | ----------------------------------------------------------------- | 1284| Promise\<void> | 以 Promise 形式返回设定结果。成功返回空,失败返回错误码错误信息。 | 1285 1286**错误码:** 1287 1288| 错误码 ID | 错误信息 | 1289| --------- | -------------------------------------------- | 1290| 201 | Permission denied. | 1291| 202 | Non-system applications use system APIs. | 1292| 401 | Parameter error. | 1293| 2100001 | Invalid parameter value. | 1294| 2100002 | Failed to connect to the service. | 1295| 2100003 | System internal error. | 1296 1297**示例:** 1298 1299```ts 1300import { connection } from '@kit.NetworkKit'; 1301import { BusinessError } from '@kit.BasicServicesKit'; 1302 1303policy 1304 .updateRemindPolicy( 1305 connection.NetBearType.BEARER_CELLULAR, 1306 '1', 1307 policy.RemindType.REMIND_TYPE_WARNING 1308 ) 1309 .then(() => { 1310 console.log('updateRemindPolicy success'); 1311 }) 1312 .catch((error: BusinessError) => { 1313 console.log(JSON.stringify(error)); 1314 }); 1315``` 1316 1317## policy.setPowerSaveTrustlist<sup>10+</sup> 1318 1319setPowerSaveTrustlist(uids: Array\<number>, isAllowed: boolean, callback: AsyncCallback\<void>): void 1320 1321设置指定 uid 应用是否在省电防火墙的白名单,使用 callback 方式作为异步方法。 1322 1323**系统接口**:此接口为系统接口。 1324 1325**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1326 1327**系统能力**:SystemCapability.Communication.NetManager.Core 1328 1329**参数:** 1330 1331| 参数名 | 类型 | 必填 | 说明 | 1332| --------- | ------------------------------ | ---- | ---------------------------------------------- | 1333| uids | Array\<number> | 是 | app 唯一标识符 | 1334| isAllowed | boolean | 是 | 是否加入白名单 | 1335| callback | callback: AsyncCallback\<void> | 是 | 回调函数,成功返回空,失败返回错误码错误信息。 | 1336 1337**错误码:** 1338 1339| 错误码 ID | 错误信息 | 1340| --------- | -------------------------------------------- | 1341| 201 | Permission denied. | 1342| 202 | Non-system applications use system APIs. | 1343| 401 | Parameter error. | 1344| 2100001 | Invalid parameter value. | 1345| 2100002 | Failed to connect to the service. | 1346| 2100003 | System internal error. | 1347 1348**示例:** 1349 1350```ts 1351import { BusinessError } from '@kit.BasicServicesKit'; 1352 1353policy.setPowerSaveTrustlist([11111, 22222], true, (error: BusinessError) => { 1354 console.log(JSON.stringify(error)); 1355}); 1356``` 1357 1358## policy.setPowerSaveTrustlist<sup>10+</sup> 1359 1360setPowerSaveTrustlist(uids: Array\<number>, isAllowed: boolean): Promise\<void>; 1361 1362设置指定 uid 应用是否在省电防火墙的白名单,使用 Promise 方式作为异步方法。 1363 1364**系统接口**:此接口为系统接口。 1365 1366**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1367 1368**系统能力**:SystemCapability.Communication.NetManager.Core 1369 1370**参数:** 1371 1372| 参数名 | 类型 | 必填 | 说明 | 1373| --------- | -------------- | ---- | -------------- | 1374| uids | Array\<number> | 是 | app 唯一标识符 | 1375| isAllowed | boolean | 是 | 是否加入白名单 | 1376 1377**返回值:** 1378 1379| 类型 | 说明 | 1380| -------------- | ----------------------------------------------------------------- | 1381| Promise\<void> | 以 Promise 形式返回设定结果。成功返回空,失败返回错误码错误信息。 | 1382 1383**错误码:** 1384 1385| 错误码 ID | 错误信息 | 1386| --------- | -------------------------------------------- | 1387| 201 | Permission denied. | 1388| 202 | Non-system applications use system APIs. | 1389| 401 | Parameter error. | 1390| 2100001 | Invalid parameter value. | 1391| 2100002 | Failed to connect to the service. | 1392| 2100003 | System internal error. | 1393 1394**示例:** 1395 1396```ts 1397import { BusinessError } from '@kit.BasicServicesKit'; 1398 1399policy 1400 .setPowerSaveTrustlist([11111, 22222], true) 1401 .then(() => { 1402 console.log('setPowerSaveTrustlist success'); 1403 }) 1404 .catch((error: BusinessError) => { 1405 console.log(JSON.stringify(error)); 1406 }); 1407``` 1408 1409## policy.getPowerSaveTrustlist<sup>10+</sup> 1410 1411getPowerSaveTrustlist(callback: AsyncCallback\<Array\<number>>): void 1412 1413获取省电模式白名单所包含的 uid 数组,使用 callback 方式作为异步方法。 1414 1415**系统接口**:此接口为系统接口。 1416 1417**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1418 1419**系统能力**:SystemCapability.Communication.NetManager.Core 1420 1421**参数:** 1422 1423| 参数名 | 类型 | 必填 | 说明 | 1424| -------- | ------------------------------ | ---- | ------------------------ | 1425| callback | AsyncCallback\<Array\<number>> | 是 | 回调函数,返回获取结果。 | 1426 1427**错误码:** 1428 1429| 错误码 ID | 错误信息 | 1430| --------- | -------------------------------------------- | 1431| 201 | Permission denied. | 1432| 202 | Non-system applications use system APIs. | 1433| 401 | Parameter error. | 1434| 2100001 | Invalid parameter value. | 1435| 2100002 | Failed to connect to the service. | 1436| 2100003 | System internal error. | 1437 1438**示例:** 1439 1440```ts 1441import { BusinessError } from '@kit.BasicServicesKit'; 1442 1443policy.getPowerSaveTrustlist((error: BusinessError, data: number[]) => { 1444 console.log(JSON.stringify(error)); 1445 console.log(JSON.stringify(data)); 1446}); 1447``` 1448 1449## policy.getPowerSaveTrustlist<sup>10+</sup> 1450 1451getPowerSaveTrustlist(): Promise\<Array\<number>>; 1452 1453获取休眠模式白名单所包含的 uid 数组,使用 Promise 方式作为异步方法。 1454 1455**系统接口**:此接口为系统接口。 1456 1457**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1458 1459**系统能力**:SystemCapability.Communication.NetManager.Core 1460 1461**返回值:** 1462 1463| 类型 | 说明 | 1464| ------------------------ | ----------------------------- | 1465| Promise\<Array\<number>> | 以 Promise 形式返回设定结果。 | 1466 1467**错误码:** 1468 1469| 错误码 ID | 错误信息 | 1470| --------- | -------------------------------------------- | 1471| 201 | Permission denied. | 1472| 202 | Non-system applications use system APIs. | 1473| 401 | Parameter error. | 1474| 2100002 | Failed to connect to the service. | 1475| 2100003 | System internal error. | 1476 1477**示例:** 1478 1479```ts 1480import { BusinessError } from '@kit.BasicServicesKit'; 1481 1482policy 1483 .getPowerSaveTrustlist() 1484 .then((data: number[]) => { 1485 console.log(JSON.stringify(data)); 1486 }) 1487 .catch((error: BusinessError) => { 1488 console.log(JSON.stringify(error)); 1489 }); 1490``` 1491 1492## policy.setNetworkAccessPolicy<sup>12+</sup> 1493 1494setNetworkAccessPolicy(uid: number, policy: NetworkAccessPolicy, isReconfirmed?: boolean): Promise<void> 1495 1496设置指定 uid 应用能否能访问网络的策略,使用 Promise 方式作为异步方法。 1497 1498**系统接口**:此接口为系统接口。 1499 1500**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1501 1502**系统能力**:SystemCapability.Communication.NetManager.Core 1503 1504**参数:** 1505 1506| 参数名 | 类型 | 必填 | 说明 | 1507| ------------- | ---------------------------------------------- | ---- | ---------------------------------------------------------------------------- | 1508| uid | number | 是 | app 唯一标识符 | 1509| policy | [NetworkAccessPolicy](#networkaccesspolicy12) | 是 | 网络策略 | 1510| isReconfirmed | boolean | 否 | 默认false;false 表示需要重确认,应用访问网络会弹框; true 表示不需要重确认,无弹框 | 1511 1512**返回值:** 1513 1514| 类型 | 说明 | 1515| -------------- | ------------------------------------------------------------ | 1516| Promise\<void> | 以 Promise 形式返回设定结果。成功返回空,失败返回错误码错误信息。 | 1517 1518**错误码:** 1519 1520| 错误码 ID | 错误信息 | 1521| --------- | -------------------------------------------- | 1522| 201 | Permission denied. | 1523| 202 | Non-system applications use system APIs. | 1524| 401 | Parameter error. | 1525| 2100001 | Invalid parameter value. | 1526| 2100002 | Failed to connect to the service. | 1527| 2100003 | System internal error. | 1528 1529**示例:** 1530 1531```ts 1532import { BusinessError } from '@kit.BasicServicesKit'; 1533 1534let accessPolicy: policy.NetworkAccessPolicy = { 1535 allowWiFi: false, 1536 allowCellular: true, 1537} 1538policy 1539 .setNetworkAccessPolicy(11111, accessPolicy) 1540 .then(() => { 1541 console.log('setNetworkAccessPolicy success'); 1542 }) 1543 .catch((error: BusinessError) => { 1544 console.error(JSON.stringify(error)); 1545 }); 1546``` 1547 1548## policy.getNetworkAccessPolicy<sup>12+</sup> 1549 1550getNetworkAccessPolicy(uid: number): Promise<NetworkAccessPolicy> 1551 1552获取指定 uid 能否访问网络策略,使用 Promise 方式作为异步方法。 1553 1554**系统接口**:此接口为系统接口。 1555 1556**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1557 1558**系统能力**:SystemCapability.Communication.NetManager.Core 1559 1560**参数:** 1561 1562| 参数名 | 类型 | 必填 | 说明 | 1563| ------ | ------ | ---- | -------------- | 1564| uid | number | 是 | app 唯一标识符 | 1565 1566**返回值:** 1567 1568| 类型 | 说明 | 1569| ------------------------------------------------------- | ----------------------------- | 1570| Promise\<[NetworkAccessPolicy](#networkaccesspolicy12)> | 以 Promise 形式返回设定结果。 | 1571 1572**错误码:** 1573 1574| 错误码 ID | 错误信息 | 1575| --------- | -------------------------------------------- | 1576| 201 | Permission denied. | 1577| 202 | Non-system applications use system APIs. | 1578| 401 | Parameter error. | 1579| 2100001 | Invalid parameter value. | 1580| 2100002 | Failed to connect to the service. | 1581| 2100003 | System internal error. | 1582 1583**示例:** 1584 1585```ts 1586import { BusinessError } from '@kit.BasicServicesKit'; 1587 1588policy 1589 .getNetworkAccessPolicy(11111) 1590 .then((data: policy.NetworkAccessPolicy) => { 1591 console.log(JSON.stringify(data)); 1592 }) 1593 .catch((error: BusinessError) => { 1594 console.error(JSON.stringify(error)); 1595 }); 1596``` 1597 1598## policy.getNetworkAccessPolicy<sup>12+</sup> 1599 1600getNetworkAccessPolicy(): Promise<UidNetworkAccessPolicy> 1601 1602获取当前用户下所有应用 app 能否访问网络策略信息,使用 Promise 方式作为异步方法。 1603 1604**系统接口**:此接口为系统接口。 1605 1606**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1607 1608**系统能力**:SystemCapability.Communication.NetManager.Core 1609 1610**返回值:** 1611 1612| 类型 | 说明 | 1613| ------------------------------------------------------------- | --------------------------- | 1614| Promise\<[UidNetworkAccessPolicy](#uidnetworkaccesspolicy12)> | 以 Promise 形式返回设定结果。 | 1615 1616**错误码:** 1617 1618| 错误码 ID | 错误信息 | 1619| --------- | -------------------------------------------- | 1620| 201 | Permission denied. | 1621| 202 | Non-system applications use system APIs. | 1622| 2100002 | Failed to connect to the service. | 1623| 2100003 | System internal error. | 1624 1625**示例:** 1626 1627```ts 1628import { BusinessError } from '@kit.BasicServicesKit'; 1629 1630policy 1631 .getNetworkAccessPolicy() 1632 .then((data: policy.UidNetworkAccessPolicy) => { 1633 let keyMap: Map<string, object> = new Map<string, object>(Object.entries(data)); 1634 let uid:number = 0; 1635 let allowWiFi: string = ""; 1636 let allowCellular: string = ""; 1637 1638 keyMap.forEach((value:object, key:string) => { 1639 let valueMap: Map<string, string> = new Map<string, string>(Object.entries(value)); 1640 uid = Number.parseInt(key); 1641 valueMap.forEach((value:string, key:string)=>{ 1642 if (key == "allowWiFi") { 1643 allowWiFi = value; 1644 } 1645 if (key == "allowCellular") { 1646 allowCellular = value; 1647 } 1648 }) 1649 }) 1650 console.log(JSON.stringify(data)); 1651 }) 1652 .catch((error: BusinessError) => { 1653 console.error(JSON.stringify(error)); 1654 }); 1655``` 1656 1657## policy.on 1658 1659网络策略的句柄。 1660 1661### on('netUidPolicyChange')<sup>10+</sup> 1662 1663on(type: "netUidPolicyChange", callback: Callback\<NetUidPolicyInfo\>): void 1664 1665注册 policy 发生改变时的回调,使用 callback 方式作为异步方法。 1666 1667**系统接口**:此接口为系统接口。 1668 1669**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1670 1671**系统能力**:SystemCapability.Communication.NetManager.Core 1672 1673**参数:** 1674 1675| 参数名 | 类型 | 必填 | 说明 | 1676| -------- | ------------------------------------------------------------------- | ---- | -------------------------------------- | 1677| type | string | 是 | policy 发生改变的类型 | 1678| callback | Callback\<[NetUidPolicyInfo](#netuidpolicyinfo11)> | 是 | 回调函数。注册 policy 发生改变时调用。 | 1679 1680**错误码:** 1681 1682| 错误码 ID | 错误信息 | 1683| --------- | -------------------------------------------- | 1684| 201 | Permission denied. | 1685| 202 | Non-system applications use system APIs. | 1686| 401 | Parameter error. | 1687| 2100001 | Invalid parameter value. | 1688| 2100002 | Failed to connect to the service. | 1689| 2100003 | System internal error. | 1690 1691**示例:** 1692 1693```ts 1694import { policy } from '@kit.NetworkKit'; 1695 1696interface Data { 1697 uid: number, 1698 policy: policy.NetUidPolicy 1699} 1700 1701try { 1702 policy.on('netUidPolicyChange', (data: Data) => { 1703 console.log('on netUidPolicyChange data: ' + JSON.stringify(data)); 1704 }); 1705} catch(err) { 1706 console.error('on netUidPolicyChange error: ' + JSON.stringify(err)); 1707} 1708``` 1709 1710### off('netUidPolicyChange')<sup>10+</sup> 1711 1712off(type: "netUidPolicyChange", callback?: Callback\<NetUidPolicyInfo\>): void 1713 1714注销 policy 发生改变时的回调,使用 callback 方式作为异步方法。 1715 1716**系统接口**:此接口为系统接口。 1717 1718**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1719 1720**系统能力**:SystemCapability.Communication.NetManager.Core 1721 1722**参数:** 1723 1724| 参数名 | 类型 | 必填 | 说明 | 1725| -------- | ------------------------------------------------------------------- | ---- | -------------------------------------- | 1726| type | string | 是 | policy 发生改变的类型 | 1727| callback | Callback\<[NetUidPolicyInfo](#netuidpolicyinfo11)> | 否 | 回调函数。注销 policy 发生改变时调用。 | 1728 1729**错误码:** 1730 1731| 错误码 ID | 错误信息 | 1732| --------- | -------------------------------------------- | 1733| 201 | Permission denied. | 1734| 202 | Non-system applications use system APIs. | 1735| 401 | Parameter error. | 1736| 2100001 | Invalid parameter value. | 1737| 2100002 | Failed to connect to the service. | 1738| 2100003 | System internal error. | 1739 1740**示例:** 1741 1742```ts 1743import { policy } from '@kit.NetworkKit'; 1744 1745interface Data { 1746 uid: number, 1747 policy: policy.NetUidPolicy 1748} 1749 1750try { 1751 policy.on('netUidPolicyChange', (data: Data) => { 1752 console.log('on netUidPolicyChange data: ' + JSON.stringify(data)); 1753 }); 1754} catch(err) { 1755 console.error('on netUidPolicyChange error: ' + JSON.stringify(err)); 1756} 1757 1758try { 1759 policy.off('netUidPolicyChange', (data: Data) => { 1760 console.log('off netUidPolicyChange data: ' + JSON.stringify(data)); 1761 }); 1762} catch(err) { 1763 console.error('off netUidPolicyChange error: ' + JSON.stringify(err)); 1764} 1765``` 1766 1767### on('netUidRuleChange')<sup>10+</sup> 1768 1769on(type: "netUidRuleChange", callback: Callback\<NetUidRuleInfo\>): void 1770 1771注册 rule 发生改变时的回调,使用 callback 方式作为异步方法。 1772 1773**系统接口**:此接口为系统接口。 1774 1775**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1776 1777**系统能力**:SystemCapability.Communication.NetManager.Core 1778 1779**参数:** 1780 1781| 参数名 | 类型 | 必填 | 说明 | 1782| -------- | ------------------------------------------------------------- | ---- | -------------------------------------- | 1783| type | string | 是 | rule 发生改变的类型 | 1784| callback | Callback\<[NetUidRuleInfo](#netuidruleinfo11)> | 是 | 回调函数。注册 rule 发生改变时的调用。 | 1785 1786**错误码:** 1787 1788| 错误码 ID | 错误信息 | 1789| --------- | -------------------------------------------- | 1790| 201 | Permission denied. | 1791| 202 | Non-system applications use system APIs. | 1792| 401 | Parameter error. | 1793| 2100001 | Invalid parameter value. | 1794| 2100002 | Failed to connect to the service. | 1795| 2100003 | System internal error. | 1796 1797**示例:** 1798 1799```ts 1800import { policy } from '@kit.NetworkKit'; 1801 1802interface Data { 1803 uid: number, 1804 rule: policy.NetUidRule 1805} 1806 1807try { 1808 policy.on('netUidRuleChange', (data: Data) => { 1809 console.log('on netUidRuleChange data: ' + JSON.stringify(data)); 1810 }); 1811} catch(err) { 1812 console.error('on netUidRuleChange error: ' + JSON.stringify(err)); 1813} 1814``` 1815 1816### off('netUidRuleChange')<sup>10+</sup> 1817 1818off(type: "netUidRuleChange", callback?: Callback\<NetUidRuleInfo\>): void 1819 1820注销 rule 发生改变时的回调,使用 callback 方式作为异步方法。 1821 1822**系统接口**:此接口为系统接口。 1823 1824**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1825 1826**系统能力**:SystemCapability.Communication.NetManager.Core 1827 1828**参数:** 1829 1830| 参数名 | 类型 | 必填 | 说明 | 1831| -------- | ------------------------------------------------------------- | ---- | -------------------------------------- | 1832| type | string | 是 | rule 发生改变的类型 | 1833| callback | Callback\<[NetUidRuleInfo](#netuidruleinfo11)> | 否 | 回调函数。注销 rule 发生改变时的调用。 | 1834 1835**错误码:** 1836 1837| 错误码 ID | 错误信息 | 1838| --------- | -------------------------------------------- | 1839| 201 | Permission denied. | 1840| 202 | Non-system applications use system APIs. | 1841| 401 | Parameter error. | 1842| 2100001 | Invalid parameter value. | 1843| 2100002 | Failed to connect to the service. | 1844| 2100003 | System internal error. | 1845 1846**示例:** 1847 1848```ts 1849import { policy } from '@kit.NetworkKit'; 1850 1851interface Data { 1852 uid: number, 1853 rule: policy.NetUidRule 1854} 1855 1856try { 1857 policy.on('netUidRuleChange', (data: Data) => { 1858 console.log('on netUidRuleChange data: ' + JSON.stringify(data)); 1859 }); 1860} catch(err) { 1861 console.error('on netUidRuleChange error: ' + JSON.stringify(err)); 1862} 1863 1864try { 1865 policy.off('netUidRuleChange', (data: Data) => { 1866 console.log('off netUidRuleChange data: ' + JSON.stringify(data)); 1867 }); 1868} catch(err) { 1869 console.error('off netUidRuleChange error: ' + JSON.stringify(err)); 1870} 1871``` 1872 1873### on('netMeteredIfacesChange')<sup>10+</sup> 1874 1875on(type: "netMeteredIfacesChange", callback: Callback\<Array\<string>>): void 1876 1877注册计量 iface 发生改变时的回调,使用 callback 方式作为异步方法。 1878 1879**系统接口**:此接口为系统接口。 1880 1881**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1882 1883**系统能力**:SystemCapability.Communication.NetManager.Core 1884 1885**参数:** 1886 1887| 参数名 | 类型 | 必填 | 说明 | 1888| -------- | ------------------------- | ---- | ----------------------------------------- | 1889| type | string | 是 | 计量 iface 发生改变的类型 | 1890| callback | Callback\<Array\<string>> | 是 | 回调函数。注册计量 iface 发生改变时调用。 | 1891 1892**错误码:** 1893 1894| 错误码 ID | 错误信息 | 1895| --------- | -------------------------------------------- | 1896| 201 | Permission denied. | 1897| 202 | Non-system applications use system APIs. | 1898| 401 | Parameter error. | 1899| 2100001 | Invalid parameter value. | 1900| 2100002 | Failed to connect to the service. | 1901| 2100003 | System internal error. | 1902 1903**示例:** 1904 1905```ts 1906import { policy } from '@kit.NetworkKit'; 1907 1908try { 1909 policy.on('netMeteredIfacesChange', (data: string[]) => { 1910 console.log('on netMeteredIfacesChange data: ' + JSON.stringify(data)); 1911 }); 1912} catch(err) { 1913 console.error('on netMeteredIfacesChange error: ' + JSON.stringify(err)); 1914} 1915``` 1916 1917### off('netMeteredIfacesChange')<sup>10+</sup> 1918 1919off(type: "netMeteredIfacesChange", callback?: Callback\<Array\<string>>): void 1920 1921注销计量 iface 发生改变时的回调,使用 callback 方式作为异步方法。 1922 1923**系统接口**:此接口为系统接口。 1924 1925**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1926 1927**系统能力**:SystemCapability.Communication.NetManager.Core 1928 1929**参数:** 1930 1931| 参数名 | 类型 | 必填 | 说明 | 1932| -------- | ------------------------- | ---- | ----------------------------------------- | 1933| type | string | 是 | 计量 iface 发生改变的类型 | 1934| callback | Callback\<Array\<string>> | 否 | 回调函数。注册计量 iface 发生改变时调用。 | 1935 1936**错误码:** 1937 1938| 错误码 ID | 错误信息 | 1939| --------- | -------------------------------------------- | 1940| 201 | Permission denied. | 1941| 202 | Non-system applications use system APIs. | 1942| 401 | Parameter error. | 1943| 2100001 | Invalid parameter value. | 1944| 2100002 | Failed to connect to the service. | 1945| 2100003 | System internal error. | 1946 1947**示例:** 1948 1949```ts 1950import { policy } from '@kit.NetworkKit'; 1951 1952try { 1953 policy.on('netMeteredIfacesChange', (data: string[]) => { 1954 console.log('on netMeteredIfacesChange data: ' + JSON.stringify(data)); 1955 }); 1956} catch(err) { 1957 console.error('on netMeteredIfacesChange error: ' + JSON.stringify(err)); 1958} 1959 1960try { 1961 policy.off('netMeteredIfacesChange', (data: string[]) => { 1962 console.log('off netMeteredIfacesChange data: ' + JSON.stringify(data)); 1963 }); 1964} catch(err) { 1965 console.error('off netMeteredIfacesChange error: ' + JSON.stringify(err)); 1966} 1967``` 1968 1969### on('netQuotaPolicyChange')<sup>10+</sup> 1970 1971on(type: "netQuotaPolicyChange", callback: Callback\<Array\<NetQuotaPolicy>>): void 1972 1973注册计量网络策略发生改变时的回调,使用 callback 方式作为异步方法。 1974 1975**系统接口**:此接口为系统接口。 1976 1977**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 1978 1979**系统能力**:SystemCapability.Communication.NetManager.Core 1980 1981**参数:** 1982 1983| 参数名 | 类型 | 必填 | 说明 | 1984| -------- | ------------------------------------------------------ | ---- | ------------------------------------------ | 1985| type | string | 是 | 计量网络策略发生改变的类型 | 1986| callback | Callback\<Array\<[NetQuotaPolicy](#netquotapolicy10)>> | 是 | 回调函数。注册计量网络策略发生改变时调用。 | 1987 1988**错误码:** 1989 1990| 错误码 ID | 错误信息 | 1991| --------- | -------------------------------------------- | 1992| 201 | Permission denied. | 1993| 202 | Non-system applications use system APIs. | 1994| 401 | Parameter error. | 1995| 2100001 | Invalid parameter value. | 1996| 2100002 | Failed to connect to the service. | 1997| 2100003 | System internal error. | 1998 1999**示例:** 2000 2001```ts 2002import { policy } from '@kit.NetworkKit'; 2003 2004interface Data { 2005 uid: number, 2006 policy: policy.NetUidPolicy 2007} 2008 2009try { 2010 policy.on('netQuotaPolicyChange', (data: policy.NetQuotaPolicy[]) => { 2011 console.log('on netQuotaPolicyChange data: ' + JSON.stringify(data)); 2012 }); 2013} catch(err) { 2014 console.error('on netQuotaPolicyChange error: ' + JSON.stringify(err)); 2015} 2016``` 2017 2018### off('netQuotaPolicyChange')<sup>10+</sup> 2019 2020off(type: "netQuotaPolicyChange", callback?: Callback\<Array\<NetQuotaPolicy>>): void 2021 2022注销计量网络策略发生改变时的回调,使用 callback 方式作为异步方法。 2023 2024**系统接口**:此接口为系统接口。 2025 2026**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 2027 2028**系统能力**:SystemCapability.Communication.NetManager.Core 2029 2030**参数:** 2031 2032| 参数名 | 类型 | 必填 | 说明 | 2033| -------- | ------------------------------------------------------ | ---- | ------------------------------------------ | 2034| type | string | 是 | 计量网络策略发生改变的类型 | 2035| callback | Callback\<Array\<[NetQuotaPolicy](#netquotapolicy10)>> | 否 | 回调函数。注册计量网络策略发生改变时调用。 | 2036 2037**错误码:** 2038 2039| 错误码 ID | 错误信息 | 2040| --------- | -------------------------------------------- | 2041| 201 | Permission denied. | 2042| 202 | Non-system applications use system APIs. | 2043| 401 | Parameter error. | 2044| 2100001 | Invalid parameter value. | 2045| 2100002 | Failed to connect to the service. | 2046| 2100003 | System internal error. | 2047 2048**示例:** 2049 2050```ts 2051import { policy } from '@kit.NetworkKit'; 2052 2053try { 2054 policy.on('netQuotaPolicyChange', (data: Array<policy.NetQuotaPolicy>) => { 2055 console.log('on netQuotaPolicyChange data: ' + JSON.stringify(data)); 2056 }); 2057} catch(err) { 2058 console.error('on netQuotaPolicyChange error: ' + JSON.stringify(err)); 2059} 2060 2061try { 2062 policy.off('netQuotaPolicyChange', (data: Array<policy.NetQuotaPolicy>) => { 2063 console.log('off netQuotaPolicyChange data: ' + JSON.stringify(data)); 2064 }); 2065} catch(err) { 2066 console.error('off netQuotaPolicyChange error: ' + JSON.stringify(err)); 2067} 2068``` 2069 2070### on('netBackgroundPolicyChange')<sup>10+</sup> 2071 2072on(type: "netBackgroundPolicyChange", callback: Callback\<boolean>): void 2073 2074注册后台网络策略发生改变时的回调,使用 callback 方式作为异步方法。 2075 2076**系统接口**:此接口为系统接口。 2077 2078**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 2079 2080**系统能力**:SystemCapability.Communication.NetManager.Core 2081 2082**参数:** 2083 2084| 参数名 | 类型 | 必填 | 说明 | 2085| -------- | ------------------ | ---- | ------------------------------------------ | 2086| type | string | 是 | 后台网络策略发生改变的类型 | 2087| callback | Callback\<boolean> | 是 | 回调函数。注册后台网络策略发生改变时调用。 | 2088 2089**错误码:** 2090 2091| 错误码 ID | 错误信息 | 2092| --------- | -------------------------------------------- | 2093| 201 | Permission denied. | 2094| 202 | Non-system applications use system APIs. | 2095| 401 | Parameter error. | 2096| 2100001 | Invalid parameter value. | 2097| 2100002 | Failed to connect to the service. | 2098| 2100003 | System internal error. | 2099 2100**示例:** 2101 2102```ts 2103import { policy } from '@kit.NetworkKit'; 2104 2105try { 2106 policy.on('netBackgroundPolicyChange', (data: boolean) => { 2107 console.log('on netBackgroundPolicyChange data: ' + JSON.stringify(data)); 2108 }); 2109} catch(err) { 2110 console.error('on netBackgroundPolicyChange error: ' + JSON.stringify(err)); 2111} 2112``` 2113 2114### off('netBackgroundPolicyChange')<sup>10+</sup> 2115 2116off(type: "netBackgroundPolicyChange", callback?: Callback\<boolean>): void 2117 2118注销后台网络策略发生改变时的回调,使用 callback 方式作为异步方法。 2119 2120**系统接口**:此接口为系统接口。 2121 2122**需要权限**:ohos.permission.MANAGE_NET_STRATEGY 2123 2124**系统能力**:SystemCapability.Communication.NetManager.Core 2125 2126**参数:** 2127 2128| 参数名 | 类型 | 必填 | 说明 | 2129| -------- | ------------------ | ---- | ------------------------------------------ | 2130| type | string | 是 | 后台网络策略发生改变的类型 | 2131| callback | Callback\<boolean> | 否 | 回调函数。注册后台网络策略发生改变时调用。 | 2132 2133**错误码:** 2134 2135| 错误码 ID | 错误信息 | 2136| --------- | -------------------------------------------- | 2137| 201 | Permission denied. | 2138| 202 | Non-system applications use system APIs. | 2139| 401 | Parameter error. | 2140| 2100001 | Invalid parameter value. | 2141| 2100002 | Failed to connect to the service. | 2142| 2100003 | System internal error. | 2143 2144**示例:** 2145 2146```ts 2147import { policy } from '@kit.NetworkKit'; 2148 2149try { 2150 policy.on('netBackgroundPolicyChange', (data: boolean) => { 2151 console.log('on netBackgroundPolicyChange data: ' + JSON.stringify(data)); 2152 }); 2153} catch(err) { 2154 console.error('on netBackgroundPolicyChange error: ' + JSON.stringify(err)); 2155} 2156 2157try { 2158 policy.off('netBackgroundPolicyChange', (data: boolean) => { 2159 console.log('off netBackgroundPolicyChange data: ' + JSON.stringify(data)); 2160 }); 2161} catch(err) { 2162 console.error('off netBackgroundPolicyChange error: ' + JSON.stringify(err)); 2163} 2164``` 2165 2166## NetBackgroundPolicy<sup>10+</sup> 2167 2168后台网络策略。 2169 2170**系统接口**:此接口为系统接口。 2171 2172**系统能力**:SystemCapability.Communication.NetManager.Core 2173 2174| 名称 | 值 | 说明 | 2175| ------------------------------- | --- | ------------------------------------------ | 2176| NET_BACKGROUND_POLICY_NONE | 0 | 默认值。 | 2177| NET_BACKGROUND_POLICY_ENABLE | 1 | 应用在后台可以使用计量网路。 | 2178| NET_BACKGROUND_POLICY_DISABLE | 2 | 应用在后台不可以使用计量网路。 | 2179| NET_BACKGROUND_POLICY_TRUSTLIST | 3 | 只有应用指定的列表在后台可以使用计量网络。 | 2180 2181## NetQuotaPolicy<sup>10+</sup> 2182 2183计量网络策略。 2184 2185**系统接口**:此接口为系统接口。 2186 2187**系统能力**:SystemCapability.Communication.NetManager.Core 2188 2189| 名称 | 类型 | 必填 | 说明 | 2190| ---------------- | --------------------------------------- | ---- | -------------------------------- | 2191| networkMatchRule | [NetworkMatchRule](#networkmatchrule10) | 是 | 网络标识,用来确定设置哪一个网络 | 2192| quotaPolicy | [QuotaPolicy](#quotapolicy10) | 是 | 具体的计量网络策略 | 2193 2194## NetworkMatchRule<sup>10+</sup> 2195 2196网络标识,用来确定设置哪一个网络 2197 2198**系统接口**:此接口为系统接口。 2199 2200**系统能力**:SystemCapability.Communication.NetManager.Core 2201 2202| 名称 | 类型 | 必填 | 说明 | 2203| -------- | ---------------------------------------------------- | ---- | ---------------------------------------------------------------------------- | 2204| netType | [NetBearType](js-apis-net-connection.md#netbeartype) | 是 | 网络类型。 | 2205| simId | string | 是 | 计量蜂窝网络的 SIM 卡的标识值。以太网,wifi 网络不会用到 | 2206| identity | string | 是 | 计量蜂窝网络中配合 simId 联合使用。以太网,wifi 网络单独使用。用于标记类型。 | 2207 2208## QuotaPolicy<sup>10+</sup> 2209 2210计量网络策略 2211 2212**系统接口**:此接口为系统接口。 2213 2214**系统能力**:SystemCapability.Communication.NetManager.Core 2215 2216| 名称 | 类型 | 必填 | 说明 | 2217| ----------------- |-------------------------------| ---- |-----------------------------------------------------| 2218| periodDuration | string | 是 | 流量限制计量周期。D1,M1,Y1 分别代表 1 天,1 个月,1 年内流量限制,超出时间则不受限制。 | 2219| warningBytes | number | 是 | 发出警告的流量阈值。 | 2220| limitBytes | number | 是 | 流量设置的配额。 | 2221| metered | boolean | 是 | 是否为计量网络。 | 2222| limitAction | [LimitAction](#limitaction10) | 是 | 到达流量限制后的动作。 | 2223| lastWarningRemind | number | 否 | 最新一次发出警告的时间。默认值:-1 | 2224| lastLimitRemind | number | 否 | 最新一次配额耗尽的时间。默认值:-1 | 2225 2226## LimitAction<sup>10+</sup> 2227 2228限制动作。 2229 2230**系统接口**:此接口为系统接口。 2231 2232**系统能力**:SystemCapability.Communication.NetManager.Core 2233 2234| 名称 | 值 | 说明 | 2235| ---------------------------- | --- | ---------------------------------- | 2236| LIMIT_ACTION_NONE | -1 | 默认值。 | 2237| LIMIT_ACTION_ACCESS_DISABLED | 0 | 当配额策略达到限制时,访问被禁用。 | 2238| LIMIT_ACTION_ALERT_ONLY | 1 | 当配额策略达到限制时,将警告用户。 | 2239 2240## NetUidRule<sup>10+</sup> 2241 2242计量网络规则。 2243 2244**系统接口**:此接口为系统接口。 2245 2246**系统能力**:SystemCapability.Communication.NetManager.Core 2247 2248| 名称 | 值 | 说明 | 2249| --------------------------------- | ------ | -------------------- | 2250| NET_RULE_NONE | 0 | 默认规则 | 2251| NET_RULE_ALLOW_METERED_FOREGROUND | 1 << 0 | 允许前台访问计量网络 | 2252| NET_RULE_ALLOW_METERED | 1 << 1 | 允许访问计量网络 | 2253| NET_RULE_REJECT_METERED | 1 << 2 | 拒绝访问计量网络 | 2254| NET_RULE_ALLOW_ALL | 1 << 5 | 允许访问所有网络 | 2255| NET_RULE_REJECT_ALL | 1 << 6 | 拒绝访问所有网络 | 2256 2257## NetUidRuleInfo<sup>11+</sup> 2258 2259生成网络唯一标识 2260 2261**系统接口**:此接口为系统接口。 2262 2263**系统能力**:SystemCapability.Communication.NetManager.Core 2264 2265| 名称 | 类型 | 必填 | 说明 | 2266| ----------------- | ----------------------------- | ---- | ----------------------------------------- | 2267| uid | number | 是 | 流量警告的阈值,默认:DATA_USAGE_UNKNOWN。 | 2268| rule | [NetUidRule](#netuidrule10) | 是 | 规定一个UID访问计量网络还是非计量网络。 | 2269 2270## NetUidPolicyInfo<sup>11+</sup> 2271 2272注册网络UID策略变化的回调函数 2273 2274**系统接口**:此接口为系统接口。 2275 2276**系统能力**:SystemCapability.Communication.NetManager.Core 2277 2278| 名称 | 类型 | 必填 | 说明 | 2279| ----------------- | ------------------------------- | ---- | -------------------------------------- | 2280| uid | number | 是 | 流量警告的阈值,默认:DATA_USAGE_UNKNOWN | 2281| policy | [NetUidPolicy](#netuidpolicy10) | 是 | UID指定了在后台模式下网络访问的策略。 | 2282 2283## RemindType<sup>10+</sup> 2284 2285提醒类型。 2286 2287**系统接口**:此接口为系统接口。 2288 2289**系统能力**:SystemCapability.Communication.NetManager.Core 2290 2291| 名称 | 值 | 说明 | 2292| ------------------- | --- | -------- | 2293| REMIND_TYPE_WARNING | 1 | 警告提醒 | 2294| REMIND_TYPE_LIMIT | 2 | 限制提醒 | 2295 2296## NetUidPolicy<sup>10+</sup> 2297 2298应用对应的网络策略。 2299 2300**系统接口**:此接口为系统接口。 2301 2302**系统能力**:SystemCapability.Communication.NetManager.Core 2303 2304| 名称 | 值 | 说明 | 2305| ------------------------------------ | ------ | -------------------------- | 2306| NET_POLICY_NONE | 0 | 默认网络策略 | 2307| NET_POLICY_ALLOW_METERED_BACKGROUND | 1 << 0 | 允许应用在后台访问计量网络 | 2308| NET_POLICY_REJECT_METERED_BACKGROUND | 1 << 1 | 拒绝应用在后台访问计量网络 | 2309 2310## NetworkAccessPolicy<sup>12+</sup> 2311 2312应用对应的连接网络的策略。 2313 2314**系统接口**:此接口为系统接口。 2315 2316**系统能力**:SystemCapability.Communication.NetManager.Core 2317 2318| 名称 | 类型 | 必填 | 说明 | 2319| ----------------- | --------- | ---- | ----------------------------- | 2320| allowWiFi | boolean | 是 | 能否允许访问wifi网络 | 2321| allowCellular | boolean | 是 | 能否允许访问蜂窝网络 | 2322 2323## UidNetworkAccessPolicy<sup>12+</sup> 2324 2325应用标识以及对应应用连接网络的策略。 2326 2327**系统接口**:此接口为系统接口。 2328 2329**系统能力**:SystemCapability.Communication.NetManager.Core 2330 2331| 名称 | 类型 | 必填 | 说明 | 2332| --------- | ----------------------------------------------------------- | ---- | ------------------- | 2333| undefined | [uid: string]: [NetworkAccessPolicy](#networkaccesspolicy12) | 否 | 数据类型为键值对 |