1# @ohos.abilityAccessCtrl (程序访问控制管理)(系统接口) 2 3程序访问控制提供程序的权限管理能力,包括鉴权、授权和取消授权等。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.abilityAccessCtrl (程序访问控制管理)](js-apis-abilityAccessCtrl.md)。 9 10## 导入模块 11 12```ts 13import { abilityAccessCtrl } from '@kit.AbilityKit' 14``` 15 16## AtManager 17 18管理访问控制模块的实例。 19 20### grantUserGrantedPermission 21 22grantUserGrantedPermission(tokenID: number, permissionName: Permissions, permissionFlags: number): Promise<void> 23 24授予应用user_grant权限。使用Promise异步回调。 25 26**系统接口:** 此接口为系统接口。 27 28**需要权限:** ohos.permission.GRANT_SENSITIVE_PERMISSIONS,仅系统应用可用。 29 30**系统能力:** SystemCapability.Security.AccessToken 31 32**参数:** 33 34| 参数名 | 类型 | 必填 | 说明 | 35| --------- | ------------------- | ---- | ------------------------------------------------------------ | 36| tokenID | number | 是 | 目标应用的身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。| 37| permissionName | Permissions | 是 | 被授予的权限名称,合法的权限名取值可在[应用权限列表](../../security/AccessToken/app-permissions.md)中查询。 | 38| permissionFlags | number | 是 | 授权选项<br>- 1表示当次用户若选择禁止该权限,下次权限弹窗仍可以弹出申请用户授权。<br>- 2表示当次用户若选择禁止该权限,下次不会再弹出权限弹窗,需要用户在setting的权限管理中进行授权。<br>- 64表示当次用户若选择仅本次允许,权限仅本次授权,应用切换后台状态或退出后取消授权。 | 39 40**返回值:** 41 42| 类型 | 说明 | 43| :------------ | :---------------------------------- | 44| Promise<void> | Promise对象。无返回结果的Promise对象。 | 45 46**错误码:** 47 48以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。 49 50| 错误码ID | 错误信息 | 51| -------- | -------- | 52| 201 | Permission denied. Interface caller does not have permission. | 53| 202 | Not System App. Interface caller is not a system app. | 54| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 55| 12100001 | Invalid parameter. The tokenID is 0, or the permissionName exceeds 256 characters, or the flags value is invalid. | 56| 12100002 | The specified tokenID does not exist. | 57| 12100003 | The specified permission does not exist. | 58| 12100006 | The application specified by the tokenID is not allowed to be granted with the specified permission. Either the application is a sandbox or the tokenID is from a remote device. | 59| 12100007 | The service is abnormal. | 60 61**示例:** 62 63```ts 64import { abilityAccessCtrl } from '@kit.AbilityKit'; 65import { BusinessError } from '@kit.BasicServicesKit'; 66 67let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 68let tokenID: number = 0; // 系统应用可以通过bundleManager.getApplicationInfo获取,普通应用可以通过bundleManager.getBundleInfoForSelf获取 69let permissionFlags: number = 1; 70atManager.grantUserGrantedPermission(tokenID, 'ohos.permission.READ_AUDIO', permissionFlags).then(() => { 71 console.log('grantUserGrantedPermission success'); 72}).catch((err: BusinessError) => { 73 console.error(`grantUserGrantedPermission fail, err->${JSON.stringify(err)}`); 74}); 75``` 76 77### grantUserGrantedPermission 78 79grantUserGrantedPermission(tokenID: number, permissionName: Permissions, permissionFlags: number, callback: AsyncCallback<void>): void 80 81授予应用user_grant权限。使用callback异步回调。 82 83**系统接口:** 此接口为系统接口。 84 85**需要权限:** ohos.permission.GRANT_SENSITIVE_PERMISSIONS,仅系统应用可用。 86 87**系统能力:** SystemCapability.Security.AccessToken 88 89**参数:** 90 91| 参数名 | 类型 | 必填 | 说明 | 92| --------- | ------------------- | ---- | ------------------------------------------------------------ | 93| tokenID | number | 是 | 目标应用的身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。| 94| permissionName | Permissions | 是 | 被授予的权限名称,合法的权限名取值可在[应用权限列表](../../security/AccessToken/app-permissions.md)中查询。 | 95| permissionFlags | number | 是 | 授权选项<br>- 1表示当次用户若选择禁止该权限,下次权限弹窗仍可以弹出申请用户授权。<br>- 2表示当次用户若选择禁止该权限,下次不会再弹出权限弹窗,需要用户在setting的权限管理中进行授权。<br>- 64表示当次用户若选择仅本次允许,权限仅本次授权,应用切换后台状态或退出后取消授权。 | 96| callback | AsyncCallback<void> | 是 | 授予应用user_grant权限。当授予权限成功时,err为undefined;否则为错误对象。 | 97 98**错误码:** 99 100以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。 101 102| 错误码ID | 错误信息 | 103| -------- | -------- | 104| 201 | Permission denied. Interface caller does not have permission. | 105| 202 | Not System App. Interface caller is not a system app. | 106| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 107| 12100001 | Invalid parameter. The tokenID is 0, or the permissionName exceeds 256 characters, or the flags value is invalid. | 108| 12100002 | The specified tokenID does not exist. | 109| 12100003 | The specified permission does not exist. | 110| 12100006 | The application specified by the tokenID is not allowed to be granted with the specified permission. Either the application is a sandbox or the tokenID is from a remote device. | 111| 12100007 | The service is abnormal. | 112 113**示例:** 114 115```ts 116import { abilityAccessCtrl } from '@kit.AbilityKit'; 117import { BusinessError } from '@kit.BasicServicesKit'; 118 119let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 120let tokenID: number = 0; // 系统应用可以通过bundleManager.getApplicationInfo获取,普通应用可以通过bundleManager.getBundleInfoForSelf获取 121let permissionFlags: number = 1; 122atManager.grantUserGrantedPermission(tokenID, 'ohos.permission.READ_AUDIO', permissionFlags, (err: BusinessError, data: void) => { 123 if (err) { 124 console.error(`grantUserGrantedPermission fail, err->${JSON.stringify(err)}`); 125 } else { 126 console.log('grantUserGrantedPermission success'); 127 } 128}); 129``` 130 131### revokeUserGrantedPermission 132 133revokeUserGrantedPermission(tokenID: number, permissionName: Permissions, permissionFlags: number): Promise<void> 134 135撤销应用user_grant权限。使用Promise异步回调。 136 137**系统接口:** 此接口为系统接口。 138 139**需要权限:** ohos.permission.REVOKE_SENSITIVE_PERMISSIONS,仅系统应用可用。 140 141**系统能力:** SystemCapability.Security.AccessToken 142 143**参数:** 144 145| 参数名 | 类型 | 必填 | 说明 | 146| --------- | ------------------- | ---- | ------------------------------------------------------------ | 147| tokenID | number | 是 | 目标应用的身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。| 148| permissionName | Permissions | 是 | 被撤销的权限名称,合法的权限名取值可在[应用权限列表](../../security/AccessToken/app-permissions.md)中查询。 | 149| permissionFlags | number | 是 | 授权选项<br>- 1表示当次用户若选择禁止该权限,下次权限弹窗仍可以弹出申请用户授权。<br>- 2表示当次用户若选择禁止该权限,下次不会再弹出权限弹窗,需要用户在setting的权限管理中进行授权。<br>- 64表示当次用户若选择仅本次允许,权限仅本次授权,应用切换后台状态或退出后取消授权。 | 150 151**返回值:** 152 153| 类型 | 说明 | 154| :------------ | :---------------------------------- | 155| Promise<void> | Promise对象。无返回结果的Promise对象。 | 156 157**错误码:** 158 159以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。 160 161| 错误码ID | 错误信息 | 162| -------- | -------- | 163| 201 | Permission denied. Interface caller does not have permission. | 164| 202 | Not System App. Interface caller is not a system app. | 165| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 166| 12100001 | Invalid parameter. The tokenID is 0, or the permissionName exceeds 256 characters, or the flags value is invalid. | 167| 12100002 | The specified tokenID does not exist. | 168| 12100003 | The specified permission does not exist. | 169| 12100006 | The application specified by the tokenID is not allowed to be revoked with the specified permission. Either the application is a sandbox or the tokenID is from a remote device. | 170| 12100007 | The service is abnormal. | 171 172**示例:** 173 174```ts 175import { abilityAccessCtrl } from '@kit.AbilityKit'; 176import { BusinessError } from '@kit.BasicServicesKit'; 177 178let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 179let tokenID: number = 0; // 系统应用可以通过bundleManager.getApplicationInfo获取,普通应用可以通过bundleManager.getBundleInfoForSelf获取 180let permissionFlags: number = 1; 181atManager.revokeUserGrantedPermission(tokenID, 'ohos.permission.READ_AUDIO', permissionFlags).then(() => { 182 console.log('revokeUserGrantedPermission success'); 183}).catch((err: BusinessError) => { 184 console.error(`revokeUserGrantedPermission fail, err->${JSON.stringify(err)}`); 185}); 186``` 187 188### revokeUserGrantedPermission 189 190revokeUserGrantedPermission(tokenID: number, permissionName: Permissions, permissionFlags: number, callback: AsyncCallback<void>): void 191 192撤销应用user_grant权限。使用callback异步回调。 193 194**系统接口:** 此接口为系统接口。 195 196**需要权限:** ohos.permission.REVOKE_SENSITIVE_PERMISSIONS,仅系统应用可用。 197 198**系统能力:** SystemCapability.Security.AccessToken 199 200**参数:** 201 202| 参数名 | 类型 | 必填 | 说明 | 203| --------- | ------------------- | ---- | ------------------------------------------------------------ | 204| tokenID | number | 是 | 目标应用的身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。| 205| permissionName | Permissions | 是 | 被撤销的权限名称,合法的权限名取值可在[应用权限列表](../../security/AccessToken/app-permissions.md)中查询。 | 206| permissionFlags | number | 是 | 授权选项<br>- 1表示当次用户若选择禁止该权限,下次权限弹窗仍可以弹出申请用户授权。<br>- 2表示当次用户若选择禁止该权限,下次不会再弹出权限弹窗,需要用户在setting的权限管理中进行授权。<br>- 64表示当次用户若选择仅本次允许,权限仅本次授权,应用切换后台状态或退出后取消授权。 | 207| callback | AsyncCallback<void> | 是 | 撤销应用user_grant权限。当撤销权限成功时,err为undefined;否则为错误对象。 | 208 209**错误码:** 210 211以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。 212 213| 错误码ID | 错误信息 | 214| -------- | -------- | 215| 201 | Permission denied. Interface caller does not have permission. | 216| 202 | Not System App. Interface caller is not a system app. | 217| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 218| 12100001 | Invalid parameter. The tokenID is 0, or the permissionName exceeds 256 characters, or the flags value is invalid. | 219| 12100002 | The specified tokenID does not exist. | 220| 12100003 | The specified permission does not exist. | 221| 12100006 | The application specified by the tokenID is not allowed to be revoked with the specified permission. Either the application is a sandbox or the tokenID is from a remote device. | 222| 12100007 | The service is abnormal. | 223 224**示例:** 225 226```ts 227import { abilityAccessCtrl } from '@kit.AbilityKit'; 228import { BusinessError } from '@kit.BasicServicesKit'; 229 230let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 231let tokenID: number = 0; // 系统应用可以通过bundleManager.getApplicationInfo获取,普通应用可以通过bundleManager.getBundleInfoForSelf获取 232let permissionFlags: number = 1; 233atManager.revokeUserGrantedPermission(tokenID, 'ohos.permission.READ_AUDIO', permissionFlags, (err: BusinessError, data: void) => { 234 if (err) { 235 console.error(`revokeUserGrantedPermission fail, err->${JSON.stringify(err)}`); 236 } else { 237 console.log('revokeUserGrantedPermission success'); 238 } 239}); 240``` 241 242### getPermissionFlags 243 244getPermissionFlags(tokenID: number, permissionName: Permissions): Promise<number> 245 246获取指定应用的指定权限的flag。使用Promise异步回调。 247 248**系统接口:** 此接口为系统接口。 249 250**需要权限:** ohos.permission.GET_SENSITIVE_PERMISSIONS or ohos.permission.GRANT_SENSITIVE_PERMISSIONS or ohos.permission.REVOKE_SENSITIVE_PERMISSIONS,仅系统应用可用。 251 252**系统能力:** SystemCapability.Security.AccessToken 253 254**参数:** 255 256| 参数名 | 类型 | 必填 | 说明 | 257| --------- | ------------------- | ---- | ------------------------------------------------------------ | 258| tokenID | number | 是 | 目标应用的身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。| 259| permissionName | Permissions | 是 | 查询的权限名称,合法的权限名取值可在[应用权限列表](../../security/AccessToken/app-permissions.md)中查询。 | 260 261**返回值:** 262 263| 类型 | 说明 | 264| :------------ | :---------------------------------- | 265| Promise<number> | Promise对象。返回查询结果。 | 266 267**错误码:** 268 269以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。 270 271| 错误码ID | 错误信息 | 272| -------- | -------- | 273| 201 | Permission denied. Interface caller does not have permission. | 274| 202 | Not System App. Interface caller is not a system app. | 275| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 276| 12100001 | Invalid parameter. The tokenID is 0, or the permissionName exceeds 256 characters. | 277| 12100002 | The specified tokenID does not exist. | 278| 12100003 | The specified permission does not exist. | 279| 12100006 | The operation is not allowed. Either the application is a sandbox or the tokenID is from a remote device. | 280| 12100007 | The service is abnormal. | 281 282**示例:** 283 284```ts 285import { abilityAccessCtrl } from '@kit.AbilityKit'; 286import { BusinessError } from '@kit.BasicServicesKit'; 287 288let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 289let tokenID: number = 0; // 系统应用可以通过bundleManager.getApplicationInfo获取,普通应用可以通过bundleManager.getBundleInfoForSelf获取 290atManager.getPermissionFlags(tokenID, 'ohos.permission.GRANT_SENSITIVE_PERMISSIONS').then((data: number) => { 291 console.log(`getPermissionFlags success, data->${JSON.stringify(data)}`); 292}).catch((err: BusinessError) => { 293 console.error(`getPermissionFlags fail, err->${JSON.stringify(err)}`); 294}); 295``` 296 297### setPermissionRequestToggleStatus<sup>12+</sup> 298 299setPermissionRequestToggleStatus(permissionName: Permissions, status: PermissionRequestToggleStatus): Promise<void> 300 301设置当前用户指定权限的弹窗开关状态。使用Promise异步回调。 302 303**系统接口:** 此接口为系统接口。 304 305**需要权限:** ohos.permission.DISABLE_PERMISSION_DIALOG。 306 307**系统能力:** SystemCapability.Security.AccessToken 308 309**参数:** 310 311| 参数名 | 类型 | 必填 | 说明 | 312| --------- | ------------------- | ---- | ------------------------------------------------------------ | 313| permissionName | Permissions | 是 | 待设置弹窗开关状态的权限名称,合法的权限名取值可在[应用权限列表](../../security/AccessToken/app-permissions.md)中查询。 | 314| status | [PermissionRequestToggleStatus](#permissionrequesttogglestatus12) | 是 | 指定权限的弹窗开关状态值。 | 315 316**返回值:** 317 318| 类型 | 说明 | 319| :------------ | :---------------------------------- | 320| Promise<void> | Promise对象。无返回结果的Promise对象。 | 321 322**错误码:** 323 324以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。 325 326| 错误码ID | 错误信息 | 327| -------- | -------- | 328| 201 | Permission denied. Interface caller does not have permission. | 329| 202 | Not System App. Interface caller is not a system app. | 330| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 331| 12100001 | Invalid parameter. The permissionName exceeds 256 characters, or the status value is invalid. | 332| 12100003 | The specified permission does not exist. | 333| 12100007 | The service is abnormal. | 334 335**示例:** 336 337```ts 338import { abilityAccessCtrl, Permissions } from '@kit.AbilityKit'; 339import { BusinessError } from '@kit.BasicServicesKit'; 340 341let atManager = abilityAccessCtrl.createAtManager(); 342let permission: Permissions = 'ohos.permission.CAMERA'; 343 344atManager.setPermissionRequestToggleStatus(permission, abilityAccessCtrl.PermissionRequestToggleStatus.CLOSED).then((err) => { 345 console.info('toggle_status: Set closed successful'); 346}).catch((err: BusinessError) => { 347 console.error('toggle_status: Code is ${err.code}, message is ${err.message}'); 348}); 349``` 350 351### getPermissionRequestToggleStatus<sup>12+</sup> 352 353getPermissionRequestToggleStatus(permissionName: Permissions): Promise<PermissionRequestToggleStatus> 354 355获取当前用户指定权限的弹窗开关状态。使用Promise异步回调。 356 357**系统接口:** 此接口为系统接口。 358 359**需要权限:** ohos.permission.GET_SENSITIVE_PERMISSIONS。 360 361**系统能力:** SystemCapability.Security.AccessToken 362 363**参数:** 364 365| 参数名 | 类型 | 必填 | 说明 | 366| --------- | ------------------- | ---- | ------------------------------------------------------------ | 367| permissionName | Permissions | 是 | 待查询弹窗开关状态的权限名称,合法的权限名取值可在[应用权限列表](../../security/AccessToken/app-permissions.md)中查询。 | 368 369**返回值:** 370 371| 类型 | 说明 | 372| :------------ | :---------------------------------- | 373| Promise<[PermissionRequestToggleStatus](#permissionrequesttogglestatus12)> | Promise对象。返回指定权限的弹窗开关状态值。 | 374 375**错误码:** 376 377以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。 378 379| 错误码ID | 错误信息 | 380| -------- | -------- | 381| 201 | Permission denied. Interface caller does not have permission. | 382| 202 | Not System App. Interface caller is not a system app. | 383| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 384| 12100001 | Invalid parameter. The permissionName exceeds 256 characters. | 385| 12100003 | The specified permission does not exist. | 386| 12100007 | The service is abnormal. | 387 388**示例:** 389 390```ts 391import { abilityAccessCtrl, Permissions } from '@kit.AbilityKit'; 392import { BusinessError } from '@kit.BasicServicesKit'; 393 394let atManager = abilityAccessCtrl.createAtManager(); 395let permission: Permissions = 'ohos.permission.CAMERA'; 396 397atManager.getPermissionRequestToggleStatus(permission).then((res) => { 398 if (res == abilityAccessCtrl.PermissionRequestToggleStatus.CLOSED) { 399 console.info('toggle_status: The toggle status is close'); 400 } else { 401 console.info('toggle_status: The toggle status is open'); 402 } 403}).catch((err: BusinessError) => { 404console.error('toggle_status: Code is ${err.code}, message is ${err.message}'); 405}); 406``` 407 408### getVersion<sup>9+</sup> 409 410getVersion(): Promise<number> 411 412获取当前权限管理的数据版本。使用Promise异步回调。 413 414**系统接口:** 此接口为系统接口。 415 416**系统能力:** SystemCapability.Security.AccessToken 417 418**返回值:** 419 420| 类型 | 说明 | 421| :------------ | :---------------------------------- | 422| Promise<number> | Promise对象。返回查询到的版本号。 | 423 424| 错误码ID | 错误信息 | 425| -------- | -------- | 426| 202 | Not System App. Interface caller is not a system app. | 427 428**示例:** 429 430```ts 431import { abilityAccessCtrl } from '@kit.AbilityKit'; 432 433let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 434let promise = atManager.getVersion(); 435promise.then((data: number) => { 436 console.log(`promise: data->${JSON.stringify(data)}`); 437}); 438``` 439 440### getPermissionsStatus<sup>12+</sup> 441 442getPermissionsStatus(tokenID: number, permissionList: Array<Permissions>): Promise<Array<PermissionStatus>> 443 444获取指定应用权限状态列表。使用Promise异步回调。 445 446**系统接口:** 此接口为系统接口。 447 448**需要权限:** ohos.permission.GET_SENSITIVE_PERMISSIONS,仅系统应用可用。 449 450**系统能力:** SystemCapability.Security.AccessToken 451 452**参数:** 453 454| 参数名 | 类型 | 必填 | 说明 | 455| --------- | ------------------- | ---- | ------------------------------------------------------------ | 456| tokenID | number | 是 | 目标应用的身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。| 457| permissionList | Array<Permissions> | 是 | 待获取权限状态的权限名列表,合法的权限名取值可在[应用权限列表](../../security/AccessToken/app-permissions.md)中查询。 | 458 459**返回值:** 460 461| 类型 | 说明 | 462| :------------ | :---------------------------------- | 463| Promise<Array<[PermissionStatus](#permissionstatus12)>> | Promise对象。返回查询到的权限状态列表。 | 464 465**错误码:** 466 467以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。 468 469| 错误码ID | 错误信息 | 470| -------- | -------- | 471| 201 | Permission denied. Interface caller does not have permission. | 472| 202 | Not System App. Interface caller is not a system app. | 473| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 474| 12100001 | Invalid parameter. The tokenID is 0, or the permissionName exceeds 256 characters. | 475| 12100002 | The specified tokenID does not exist. | 476| 12100007 | The service is abnormal. | 477 478**示例:** 479 480```ts 481import { abilityAccessCtrl } from '@kit.AbilityKit'; 482import { BusinessError } from '@kit.BasicServicesKit'; 483 484let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 485let tokenID: number = 0; // 系统应用可以通过bundleManager.getApplicationInfo获取,普通应用可以通过bundleManager.getBundleInfoForSelf获取 486atManager.getPermissionsStatus(tokenID, ['ohos.permission.CAMERA']).then((data: Array<abilityAccessCtrl.PermissionStatus>) => { 487 console.log(`getPermissionsStatus success, data->${JSON.stringify(data)}`); 488}).catch((err: BusinessError) => { 489 console.error(`getPermissionsStatus fail, err->${JSON.stringify(err)}`); 490}); 491``` 492 493### on<sup>9+</sup> 494 495on(type: 'permissionStateChange', tokenIDList: Array<number>, permissionList: Array<Permissions>, callback: Callback<PermissionStateChangeInfo>): void 496 497订阅指定tokenId列表与权限列表的权限状态变更事件。 498 499允许指定tokenId列表与权限列表订阅多个callback。 500 501不允许存在交集的tokenId列表与权限列表订阅相同callback。 502 503**系统接口:** 此接口为系统接口。 504 505**需要权限:** ohos.permission.GET_SENSITIVE_PERMISSIONS,仅系统应用可用。 506 507**系统能力:** SystemCapability.Security.AccessToken 508 509**参数:** 510 511| 参数名 | 类型 | 必填 | 说明 | 512| ------------------ | --------------------- | ---- | ------------------------------------------------------------ | 513| type | string | 是 | 订阅事件类型,固定为'permissionStateChange',权限状态变更事件。 | 514| tokenIDList | Array<number> | 是 | 订阅的tokenId列表,为空时表示订阅所有的应用的权限状态变化。 | 515| permissionList | Array<Permissions> | 是 | 订阅的权限名列表,为空时表示订阅所有的权限状态变化,合法的权限名取值可在[应用权限列表](../../security/AccessToken/app-permissions.md)中查询。| 516| callback | Callback<[PermissionStateChangeInfo](#permissionstatechangeinfo9)> | 是 | 订阅指定tokenId与指定权限名状态变更事件的回调。| 517 518**错误码:** 519 520以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。 521 522| 错误码ID | 错误信息 | 523| -------- | -------- | 524| 201 | Permission denied. Interface caller does not have permission. | 525| 202 | Not System App. Interface caller is not a system app. | 526| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 527| 12100001 | Invalid parameter. The tokenID is 0, or the permissionName exceeds 256 characters. | 528| 12100004 | The API is used repeatedly with the same input. | 529| 12100005 | The registration time has exceeded the limitation. | 530| 12100007 | The service is abnormal. | 531| 12100008 | Out of memory. | 532 533**示例:** 534 535```ts 536import { abilityAccessCtrl, Permissions, bundleManager } from '@kit.AbilityKit'; 537 538let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 539let appInfo: bundleManager.ApplicationInfo = bundleManager.getApplicationInfoSync('com.example.myapplication', 0, 100); 540let tokenIDList: Array<number> = [appInfo.accessTokenId]; 541let permissionList: Array<Permissions> = ['ohos.permission.DISTRIBUTED_DATASYNC']; 542try { 543 atManager.on('permissionStateChange', tokenIDList, permissionList, (data: abilityAccessCtrl.PermissionStateChangeInfo) => { 544 console.debug('receive permission state change, data:' + JSON.stringify(data)); 545 }); 546} catch(err) { 547 console.error(`catch err->${JSON.stringify(err)}`); 548} 549``` 550 551### off<sup>9+</sup> 552 553off(type: 'permissionStateChange', tokenIDList: Array<number>, permissionList: Array<Permissions>, callback?: Callback<PermissionStateChangeInfo>): void 554 555取消订阅指定tokenId列表与权限列表的权限状态变更事件,使用callback回调异步返回结果。 556 557取消订阅不传callback时,批量删除tokenIDList和permissionList下面的所有callback。 558 559**系统接口:** 此接口为系统接口。 560 561**需要权限:** ohos.permission.GET_SENSITIVE_PERMISSIONS,仅系统应用可用。 562 563**系统能力:** SystemCapability.Security.AccessToken 564 565**参数:** 566 567| 参数名 | 类型 | 必填 | 说明 | 568| ------------------ | --------------------- | ---- | ------------------------------------------------------------ | 569| type | string | 是 | 订阅事件类型,固定为'permissionStateChange',权限状态变更事件。 | 570| tokenIDList | Array<number> | 是 | 订阅的tokenId列表,为空时表示订阅所有的应用的权限状态变化,必须与on的输入一致。 | 571| permissionList | Array<Permissions> | 是 | 订阅的权限名列表,为空时表示订阅所有的权限状态变化,必须与on的输入一致,合法的权限名取值可在[应用权限列表](../../security/AccessToken/app-permissions.md)中查询。 | 572| callback | Callback<[PermissionStateChangeInfo](#permissionstatechangeinfo9)> | 否 | 取消订阅指定tokenId与指定权限名状态变更事件的回调。| 573 574**错误码:** 575 576以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。 577 578| 错误码ID | 错误信息 | 579| -------- | -------- | 580| 201 | Permission denied. Interface caller does not have permission. | 581| 202 | Not System App. Interface caller is not a system app. | 582| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 583| 12100001 | Invalid parameter. The tokenIDs or permissionNames in the list are all invalid. | 584| 12100004 | The API is not used in pair with 'on'. | 585| 12100007 | The service is abnormal. | 586| 12100008 | Out of memory. | 587 588**示例:** 589 590```ts 591import { abilityAccessCtrl, Permissions, bundleManager } from '@kit.AbilityKit'; 592 593let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 594let appInfo: bundleManager.ApplicationInfo = bundleManager.getApplicationInfoSync('com.example.myapplication', 0, 100); 595let tokenIDList: Array<number> = [appInfo.accessTokenId]; 596let permissionList: Array<Permissions> = ['ohos.permission.DISTRIBUTED_DATASYNC']; 597try { 598 atManager.off('permissionStateChange', tokenIDList, permissionList); 599} catch(err) { 600 console.error(`catch err->${JSON.stringify(err)}`); 601} 602``` 603 604### PermissionStateChangeType<sup>9+</sup> 605 606表示权限授权状态变化操作类型的枚举。 607 608**系统接口:** 此接口为系统接口。 609 610**系统能力:** SystemCapability.Security.AccessToken 611 612| 名称 | 值 | 说明 | 613| ----------------------- | ------ | ----------------- | 614| PERMISSION_REVOKED_OPER | 0 | 表示权限取消操作。 | 615| PERMISSION_GRANTED_OPER | 1 | 表示权限授予操作。 | 616 617### PermissionRequestToggleStatus<sup>12+</sup> 618 619表示指定权限对应的弹窗开关状态的枚举。 620 621**系统能力:** SystemCapability.Security.AccessToken 622 623| 名称 | 值 | 说明 | 624| ------------------ | ----- | ----------- | 625| CLOSED | 0 | 表示关闭状态。 | 626| OPEN | 1 | 表示开启状态。 | 627 628### PermissionStateChangeInfo<sup>9+</sup> 629 630表示某次权限授权状态变化的详情。 631 632**系统接口:** 此接口为系统接口。 633 634**系统能力:** SystemCapability.Security.AccessToken 635 636| 名称 | 类型 | 只读 | 必填 | 说明 | 637| -------------- | ------------------------- | ---- | ---- | ------------------ | 638| change | [PermissionStateChangeType](#permissionstatechangetype9) | 是 | 是 | 权限授权状态变化类型。 | 639| tokenID | number | 是 | 是 | 被订阅的应用身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。| 640| permissionName | Permissions | 是 | 是 | 当前授权状态发生变化的权限名,合法的权限名取值可在[应用权限列表](../../security/AccessToken/app-permissions.md)中查询。 | 641 642### PermissionStatus<sup>12+</sup> 643 644表示权限状态的枚举。 645 646**系统接口:** 此接口为系统接口。 647 648**系统能力:** SystemCapability.Security.AccessToken 649 650| 名称 | 值 | 说明 | 651| ------------------ | ----- | ----------- | 652| DENIED | -1 | 表示未授权。 | 653| GRANTED | 0 | 表示已授权。 | 654| NOT_DETERMINED | 1 | 表示未操作。 | 655| INVALID | 2 | 表示无效。 | 656| RESTRICTED | 3 | 表示受限。 | 657