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