1e41f4b71Sopenharmony_ci# @ohos.privacyManager (隐私管理)(系统接口)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci本模块主要提供权限使用记录等隐私管理接口。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci> - 本模块为系统接口。
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## 导入模块
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci```ts
13e41f4b71Sopenharmony_ciimport { privacyManager } from '@kit.AbilityKit';
14e41f4b71Sopenharmony_ci```
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## privacyManager.addPermissionUsedRecord
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciaddPermissionUsedRecord(tokenID: number, permissionName: Permissions, successCount: number, failCount: number, options?: AddPermissionUsedRecordOptions): Promise<void>
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci受应用权限保护的应用在被其他服务、应用调用时,可以使用该接口增加一条权限使用记录。使用Promise异步回调。
22e41f4b71Sopenharmony_ci权限使用记录包括:调用方的应用身份标识、使用的应用权限名称,和其访问本应用成功、失败的次数。
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci**参数:**
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci| 参数名   | 类型                 | 必填 | 说明                                       |
31e41f4b71Sopenharmony_ci| -------- | -------------------  | ---- | ------------------------------------------ |
32e41f4b71Sopenharmony_ci| tokenID   |  number   | 是   | 调用方的应用身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。|
33e41f4b71Sopenharmony_ci| permissionName | Permissions | 是   | 应用权限名称。 |
34e41f4b71Sopenharmony_ci| successCount | number | 是   | 访问成功的次数。 |
35e41f4b71Sopenharmony_ci| failCount | number | 是   | 访问失败的次数。 |
36e41f4b71Sopenharmony_ci| options<sup>12+</sup> | [AddPermissionUsedRecordOptions](#addpermissionusedrecordoptions12) | 否   | 添加权限使用记录可选参数,从API version 12开始支持。 |
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci**返回值:**
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci| 类型          | 说明                                |
41e41f4b71Sopenharmony_ci| :------------ | :---------------------------------- |
42e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci**错误码:**
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
49e41f4b71Sopenharmony_ci| -------- | -------- |
50e41f4b71Sopenharmony_ci| 201 | Permission denied. Interface caller does not have permission. |
51e41f4b71Sopenharmony_ci| 202 | Not System App. Interface caller is not a system app. |
52e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
53e41f4b71Sopenharmony_ci| 12100001 | Invalid parameter. The tokenID is 0, the permissionName exceeds 256 characters, the count value is invalid, or usedType in AddPermissionUsedRecordOptions is invalid. |
54e41f4b71Sopenharmony_ci| 12100002 | The specified tokenID does not exist or refer to an application process. |
55e41f4b71Sopenharmony_ci| 12100003 | The specified permission does not exist or is not an user_grant permission. |
56e41f4b71Sopenharmony_ci| 12100007 | The service is abnormal. |
57e41f4b71Sopenharmony_ci| 12100008 | Out of memory. |
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci**示例:**
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci```ts
62e41f4b71Sopenharmony_ciimport { privacyManager } from '@kit.AbilityKit';
63e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_cilet tokenID: number = 0; // 可以通过getApplicationInfo获取accessTokenId
66e41f4b71Sopenharmony_ciprivacyManager.addPermissionUsedRecord(tokenID, 'ohos.permission.READ_AUDIO', 1, 0).then(() => {
67e41f4b71Sopenharmony_ci  console.log('addPermissionUsedRecord success');
68e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
69e41f4b71Sopenharmony_ci  console.error(`addPermissionUsedRecord fail, err->${JSON.stringify(err)}`);
70e41f4b71Sopenharmony_ci});
71e41f4b71Sopenharmony_ci// with options param
72e41f4b71Sopenharmony_cilet options: privacyManager.AddPermissionUsedRecordOptions = {
73e41f4b71Sopenharmony_ci  usedType: privacyManager.PermissionUsedType.PICKER_TYPE
74e41f4b71Sopenharmony_ci};
75e41f4b71Sopenharmony_ciprivacyManager.addPermissionUsedRecord(tokenID, 'ohos.permission.READ_AUDIO', 1, 0, options).then(() => {
76e41f4b71Sopenharmony_ci  console.log('addPermissionUsedRecord success');
77e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
78e41f4b71Sopenharmony_ci  console.error(`addPermissionUsedRecord fail, err->${JSON.stringify(err)}`);
79e41f4b71Sopenharmony_ci});
80e41f4b71Sopenharmony_ci```
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci## privacyManager.addPermissionUsedRecord
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ciaddPermissionUsedRecord(tokenID: number, permissionName: Permissions, successCount: number, failCount: number, callback: AsyncCallback&lt;void&gt;): void
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci受应用权限保护的应用在被其他服务、应用调用时,可以使用该接口增加一条权限使用记录。使用callback异步回调。
87e41f4b71Sopenharmony_ci权限使用记录包括:调用方的应用身份标识、使用的应用权限名称,和其访问本应用成功、失败的次数。
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci**参数:**
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci| 参数名   | 类型                 | 必填 | 说明                                       |
96e41f4b71Sopenharmony_ci| -------- | -------------------  | ---- | ------------------------------------------ |
97e41f4b71Sopenharmony_ci| tokenID   |  number   | 是   | 调用方的应用身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。|
98e41f4b71Sopenharmony_ci| permissionName | Permissions | 是   | 应用权限名称,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。 |
99e41f4b71Sopenharmony_ci| successCount | number | 是   | 访问成功的次数。 |
100e41f4b71Sopenharmony_ci| failCount | number | 是   | 访问失败的次数。 |
101e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当添加使用记录成功时,err为undefined;否则为错误对象。 |
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci**错误码:**
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
108e41f4b71Sopenharmony_ci| -------- | -------- |
109e41f4b71Sopenharmony_ci| 201 | Permission denied. Interface caller does not have permission. |
110e41f4b71Sopenharmony_ci| 202 | Not System App. Interface caller is not a system app. |
111e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
112e41f4b71Sopenharmony_ci| 12100001 | Invalid parameter. The tokenID is 0, the permissionName exceeds 256 characters, or the count value is invalid. |
113e41f4b71Sopenharmony_ci| 12100002 | The specified tokenID does not exist or refer to an application process. |
114e41f4b71Sopenharmony_ci| 12100003 | The specified permission does not exist or is not an user_grant permission. |
115e41f4b71Sopenharmony_ci| 12100007 | The service is abnormal. |
116e41f4b71Sopenharmony_ci| 12100008 | Out of memory. |
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci**示例:**
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci```ts
121e41f4b71Sopenharmony_ciimport { privacyManager } from '@kit.AbilityKit';
122e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_cilet tokenID: number = 0; // 可以通过getApplicationInfo获取accessTokenId
125e41f4b71Sopenharmony_ciprivacyManager.addPermissionUsedRecord(tokenID, 'ohos.permission.READ_AUDIO', 1, 0, (err: BusinessError, data: void) => {
126e41f4b71Sopenharmony_ci  if (err) {
127e41f4b71Sopenharmony_ci    console.error(`addPermissionUsedRecord fail, err->${JSON.stringify(err)}`);
128e41f4b71Sopenharmony_ci  } else {
129e41f4b71Sopenharmony_ci    console.log('addPermissionUsedRecord success');
130e41f4b71Sopenharmony_ci  }
131e41f4b71Sopenharmony_ci});
132e41f4b71Sopenharmony_ci```
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci## privacyManager.getPermissionUsedRecord
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_cigetPermissionUsedRecord(request: PermissionUsedRequest): Promise&lt;PermissionUsedResponse&gt;
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci获取历史权限使用记录。使用Promise异步回调。
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci**参数:**
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci| 参数名   | 类型                 | 必填 | 说明                                       |
147e41f4b71Sopenharmony_ci| -------- | -------------------  | ---- | ------------------------------------------ |
148e41f4b71Sopenharmony_ci| request   |  [PermissionUsedRequest](#permissionusedrequest)   | 是   | 查询权限使用记录的请求。              |
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci**返回值:**
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci| 类型          | 说明                                |
153e41f4b71Sopenharmony_ci| :------------ | :---------------------------------- |
154e41f4b71Sopenharmony_ci| Promise<[PermissionUsedResponse](#permissionusedresponse)> | Promise对象。返回查询的权限使用记录。|
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci**错误码:**
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
161e41f4b71Sopenharmony_ci| -------- | -------- |
162e41f4b71Sopenharmony_ci| 201 | Permission denied. Interface caller does not have permission. |
163e41f4b71Sopenharmony_ci| 202 | Not System App. Interface caller is not a system app. |
164e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
165e41f4b71Sopenharmony_ci| 12100001 | Invalid parameter. The value of flag in request is invalid. |
166e41f4b71Sopenharmony_ci| 12100002 | The specified tokenID does not exist or refer to an application process. |
167e41f4b71Sopenharmony_ci| 12100003 | The specified permission does not exist or is not an user_grant permission. |
168e41f4b71Sopenharmony_ci| 12100007 | The service is abnormal. |
169e41f4b71Sopenharmony_ci| 12100008 | Out of memory. |
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci**示例:**
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci```ts
174e41f4b71Sopenharmony_ciimport { privacyManager } from '@kit.AbilityKit';
175e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_cilet request: privacyManager.PermissionUsedRequest = {
178e41f4b71Sopenharmony_ci    'tokenId': 1,
179e41f4b71Sopenharmony_ci    'isRemote': false,
180e41f4b71Sopenharmony_ci    'deviceId': 'device',
181e41f4b71Sopenharmony_ci    'bundleName': 'bundle',
182e41f4b71Sopenharmony_ci    'permissionNames': [],
183e41f4b71Sopenharmony_ci    'beginTime': 0,
184e41f4b71Sopenharmony_ci    'endTime': 1,
185e41f4b71Sopenharmony_ci    'flag':privacyManager.PermissionUsageFlag.FLAG_PERMISSION_USAGE_DETAIL,
186e41f4b71Sopenharmony_ci};
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ciprivacyManager.getPermissionUsedRecord(request).then((data) => {
189e41f4b71Sopenharmony_ci  console.log(`getPermissionUsedRecord success, data->${JSON.stringify(data)}`);
190e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
191e41f4b71Sopenharmony_ci  console.error(`getPermissionUsedRecord fail, err->${JSON.stringify(err)}`);
192e41f4b71Sopenharmony_ci});
193e41f4b71Sopenharmony_ci```
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci## privacyManager.getPermissionUsedRecord
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_cigetPermissionUsedRecord(request: PermissionUsedRequest, callback: AsyncCallback&lt;PermissionUsedResponse&gt;): void
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci获取历史权限使用记录。使用callback异步回调。
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci**参数:**
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci| 参数名   | 类型                 | 必填 | 说明                                       |
208e41f4b71Sopenharmony_ci| -------- | -------------------  | ---- | ------------------------------------------ |
209e41f4b71Sopenharmony_ci| request | [PermissionUsedRequest](#permissionusedrequest) | 是 | 查询权限使用记录的请求。 |
210e41f4b71Sopenharmony_ci| callback | AsyncCallback<[PermissionUsedResponse](#permissionusedresponse)> | 是 | 回调函数。当查询记录成功时,err为undefined,data为查询到的权限使用记录;否则为错误对象。 |
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci**错误码:**
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
217e41f4b71Sopenharmony_ci| -------- | -------- |
218e41f4b71Sopenharmony_ci| 201 | Permission denied. Interface caller does not have permission. |
219e41f4b71Sopenharmony_ci| 202 | Not System App. Interface caller is not a system app. |
220e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
221e41f4b71Sopenharmony_ci| 12100001 | Invalid parameter. The value of flag in request is invalid. |
222e41f4b71Sopenharmony_ci| 12100002 | The specified tokenID does not exist or refer to an application process. |
223e41f4b71Sopenharmony_ci| 12100003 | The specified permission does not exist or is not an user_grant permission. |
224e41f4b71Sopenharmony_ci| 12100007 | The service is abnormal. |
225e41f4b71Sopenharmony_ci| 12100008 | Out of memory. |
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci**示例:**
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ci```ts
230e41f4b71Sopenharmony_ciimport { privacyManager } from '@kit.AbilityKit';
231e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_cilet request: privacyManager.PermissionUsedRequest = {
234e41f4b71Sopenharmony_ci    'tokenId': 1,
235e41f4b71Sopenharmony_ci    'isRemote': false,
236e41f4b71Sopenharmony_ci    'deviceId': 'device',
237e41f4b71Sopenharmony_ci    'bundleName': 'bundle',
238e41f4b71Sopenharmony_ci    'permissionNames': [],
239e41f4b71Sopenharmony_ci    'beginTime': 0,
240e41f4b71Sopenharmony_ci    'endTime': 1,
241e41f4b71Sopenharmony_ci    'flag':privacyManager.PermissionUsageFlag.FLAG_PERMISSION_USAGE_DETAIL,
242e41f4b71Sopenharmony_ci};
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ciprivacyManager.getPermissionUsedRecord(request, (err: BusinessError, data: privacyManager.PermissionUsedResponse) => {
245e41f4b71Sopenharmony_ci  if (err) {
246e41f4b71Sopenharmony_ci    console.error(`getPermissionUsedRecord fail, err->${JSON.stringify(err)}`);
247e41f4b71Sopenharmony_ci  } else {
248e41f4b71Sopenharmony_ci    console.log(`getPermissionUsedRecord success, data->${JSON.stringify(data)}`);
249e41f4b71Sopenharmony_ci  }
250e41f4b71Sopenharmony_ci});
251e41f4b71Sopenharmony_ci```
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ci## privacyManager.startUsingPermission
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_cistartUsingPermission(tokenID: number, permissionName: Permissions): Promise&lt;void&gt;
256e41f4b71Sopenharmony_ci
257e41f4b71Sopenharmony_ci应用开始使用某项权限,可监听应用在前后台使用权限,并将使用权限的记录落盘,由系统服务调用。使用Promise异步回调。
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
262e41f4b71Sopenharmony_ci
263e41f4b71Sopenharmony_ci**参数:**
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci| 参数名          | 类型   | 必填 | 说明                                  |
266e41f4b71Sopenharmony_ci| -------------- | ------ | ---- | ------------------------------------ |
267e41f4b71Sopenharmony_ci| tokenID        | number | 是   | 调用方的应用身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。|
268e41f4b71Sopenharmony_ci| permissionName | Permissions | 是   | 需要使用的权限名,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
269e41f4b71Sopenharmony_ci
270e41f4b71Sopenharmony_ci**返回值:**
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci| 类型          | 说明                                    |
273e41f4b71Sopenharmony_ci| ------------- | --------------------------------------- |
274e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。|
275e41f4b71Sopenharmony_ci
276e41f4b71Sopenharmony_ci**错误码:**
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
281e41f4b71Sopenharmony_ci| -------- | -------- |
282e41f4b71Sopenharmony_ci| 201 | Permission denied. Interface caller does not have permission. |
283e41f4b71Sopenharmony_ci| 202 | Not System App. Interface caller is not a system app. |
284e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
285e41f4b71Sopenharmony_ci| 12100001 | Invalid parameter. The tokenID is 0, the permissionName exceeds 256 characters, or the count value is invalid. |
286e41f4b71Sopenharmony_ci| 12100002 | The specified tokenID does not exist or refer to an application process. |
287e41f4b71Sopenharmony_ci| 12100003 | The specified permission does not exist or is not an user_grant permission. |
288e41f4b71Sopenharmony_ci| 12100004 | The API is used repeatedly with the same input. It means the application specified by the tokenID has been using the specified permission. |
289e41f4b71Sopenharmony_ci| 12100007 | The service is abnormal. |
290e41f4b71Sopenharmony_ci| 12100008 | Out of memory. |
291e41f4b71Sopenharmony_ci
292e41f4b71Sopenharmony_ci**示例:**
293e41f4b71Sopenharmony_ci
294e41f4b71Sopenharmony_ci```ts
295e41f4b71Sopenharmony_ciimport { privacyManager } from '@kit.AbilityKit';
296e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
297e41f4b71Sopenharmony_ci
298e41f4b71Sopenharmony_cilet tokenID: number = 0; // 可以通过getApplicationInfo获取accessTokenId
299e41f4b71Sopenharmony_ciprivacyManager.startUsingPermission(tokenID, 'ohos.permission.READ_AUDIO').then(() => {
300e41f4b71Sopenharmony_ci  console.log('startUsingPermission success');
301e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
302e41f4b71Sopenharmony_ci  console.error(`startUsingPermission fail, err->${JSON.stringify(err)}`);
303e41f4b71Sopenharmony_ci});
304e41f4b71Sopenharmony_ci```
305e41f4b71Sopenharmony_ci
306e41f4b71Sopenharmony_ci## privacyManager.startUsingPermission
307e41f4b71Sopenharmony_ci
308e41f4b71Sopenharmony_cistartUsingPermission(tokenID: number, permissionName: Permissions, callback: AsyncCallback&lt;void&gt;): void
309e41f4b71Sopenharmony_ci
310e41f4b71Sopenharmony_ci应用开始使用某项权限,可监听应用在前后台使用权限,并将使用权限的记录落盘,由系统服务调用。使用callback异步回调。
311e41f4b71Sopenharmony_ci
312e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
313e41f4b71Sopenharmony_ci
314e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
315e41f4b71Sopenharmony_ci
316e41f4b71Sopenharmony_ci**参数:**
317e41f4b71Sopenharmony_ci
318e41f4b71Sopenharmony_ci| 参数名          | 类型                  | 必填 | 说明                                  |
319e41f4b71Sopenharmony_ci| -------------- | --------------------- | ---- | ------------------------------------ |
320e41f4b71Sopenharmony_ci| tokenID        | number                | 是   | 调用方的应用身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。|
321e41f4b71Sopenharmony_ci| permissionName | Permissions                | 是   | 需要使用的权限名,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
322e41f4b71Sopenharmony_ci| callback       | AsyncCallback&lt;void&gt; | 是   | 回调函数。当开始使用权限成功时,err为undefined;否则为错误对象。 |
323e41f4b71Sopenharmony_ci
324e41f4b71Sopenharmony_ci**错误码:**
325e41f4b71Sopenharmony_ci
326e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
329e41f4b71Sopenharmony_ci| -------- | -------- |
330e41f4b71Sopenharmony_ci| 201 | Permission denied. Interface caller does not have permission. |
331e41f4b71Sopenharmony_ci| 202 | Not System App. Interface caller is not a system app. |
332e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
333e41f4b71Sopenharmony_ci| 12100001 | Invalid parameter. The tokenID is 0, the permissionName exceeds 256 characters, or the count value is invalid. |
334e41f4b71Sopenharmony_ci| 12100002 | The specified tokenID does not exist or refer to an application process. |
335e41f4b71Sopenharmony_ci| 12100003 | The specified permission does not exist or is not an user_grant permission. |
336e41f4b71Sopenharmony_ci| 12100004 | The API is used repeatedly with the same input. It means the application specified by the tokenID has been using the specified permission. |
337e41f4b71Sopenharmony_ci| 12100007 | The service is abnormal. |
338e41f4b71Sopenharmony_ci| 12100008 | Out of memory. |
339e41f4b71Sopenharmony_ci
340e41f4b71Sopenharmony_ci**示例:**
341e41f4b71Sopenharmony_ci
342e41f4b71Sopenharmony_ci```ts
343e41f4b71Sopenharmony_ciimport { privacyManager } from '@kit.AbilityKit';
344e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
345e41f4b71Sopenharmony_ci
346e41f4b71Sopenharmony_cilet tokenID: number = 0; // 可以通过getApplicationInfo获取accessTokenId
347e41f4b71Sopenharmony_ciprivacyManager.startUsingPermission(tokenID, 'ohos.permission.READ_AUDIO', (err: BusinessError, data: void) => {
348e41f4b71Sopenharmony_ci  if (err) {
349e41f4b71Sopenharmony_ci    console.error(`startUsingPermission fail, err->${JSON.stringify(err)}`);
350e41f4b71Sopenharmony_ci  } else {
351e41f4b71Sopenharmony_ci    console.log('startUsingPermission success');
352e41f4b71Sopenharmony_ci  }
353e41f4b71Sopenharmony_ci});
354e41f4b71Sopenharmony_ci```
355e41f4b71Sopenharmony_ci
356e41f4b71Sopenharmony_ci## privacyManager.stopUsingPermission
357e41f4b71Sopenharmony_ci
358e41f4b71Sopenharmony_cistopUsingPermission(tokenID: number, permissionName: Permissions): Promise&lt;void&gt;
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci应用停止使用某项权限,与Start对应,由系统服务调用。使用Promise异步回调。
361e41f4b71Sopenharmony_ci
362e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
365e41f4b71Sopenharmony_ci
366e41f4b71Sopenharmony_ci**参数:**
367e41f4b71Sopenharmony_ci
368e41f4b71Sopenharmony_ci| 参数名          | 类型   | 必填 | 说明                                  |
369e41f4b71Sopenharmony_ci| -------------- | ------ | ---- | ------------------------------------ |
370e41f4b71Sopenharmony_ci| tokenID        | number | 是   | 调用方的应用身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。|
371e41f4b71Sopenharmony_ci| permissionName | Permissions | 是   | 需要使用的权限名,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
372e41f4b71Sopenharmony_ci
373e41f4b71Sopenharmony_ci**返回值:**
374e41f4b71Sopenharmony_ci
375e41f4b71Sopenharmony_ci| 类型          | 说明                                    |
376e41f4b71Sopenharmony_ci| ------------- | --------------------------------------- |
377e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。|
378e41f4b71Sopenharmony_ci
379e41f4b71Sopenharmony_ci**错误码:**
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
382e41f4b71Sopenharmony_ci
383e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
384e41f4b71Sopenharmony_ci| -------- | -------- |
385e41f4b71Sopenharmony_ci| 201 | Permission denied. Interface caller does not have permission. |
386e41f4b71Sopenharmony_ci| 202 | Not System App. Interface caller is not a system app. |
387e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
388e41f4b71Sopenharmony_ci| 12100001 | Invalid parameter. The tokenID is 0, the permissionName exceeds 256 characters, or the count value is invalid. |
389e41f4b71Sopenharmony_ci| 12100002 | The specified tokenID does not exist or refer to an application process. |
390e41f4b71Sopenharmony_ci| 12100003 | The specified permission does not exist or is not an user_grant permission. |
391e41f4b71Sopenharmony_ci| 12100004 | The API is not used in pair with 'startUsingPermission'. |
392e41f4b71Sopenharmony_ci| 12100007 | The service is abnormal. |
393e41f4b71Sopenharmony_ci| 12100008 | Out of memory. |
394e41f4b71Sopenharmony_ci
395e41f4b71Sopenharmony_ci**示例:**
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ci```ts
398e41f4b71Sopenharmony_ciimport { privacyManager } from '@kit.AbilityKit';
399e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_cilet tokenID: number = 0; // 可以通过getApplicationInfo获取accessTokenId
402e41f4b71Sopenharmony_ciprivacyManager.stopUsingPermission(tokenID, 'ohos.permission.READ_AUDIO').then(() => {
403e41f4b71Sopenharmony_ci  console.log('stopUsingPermission success');
404e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
405e41f4b71Sopenharmony_ci  console.error(`stopUsingPermission fail, err->${JSON.stringify(err)}`);
406e41f4b71Sopenharmony_ci});
407e41f4b71Sopenharmony_ci```
408e41f4b71Sopenharmony_ci
409e41f4b71Sopenharmony_ci## privacyManager.stopUsingPermission
410e41f4b71Sopenharmony_ci
411e41f4b71Sopenharmony_cistopUsingPermission(tokenID: number, permissionName: Permissions, callback: AsyncCallback&lt;void&gt;): void
412e41f4b71Sopenharmony_ci
413e41f4b71Sopenharmony_ci应用停止使用某项权限,与Start对应,由系统服务调用。使用callback异步回调。
414e41f4b71Sopenharmony_ci
415e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
416e41f4b71Sopenharmony_ci
417e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
418e41f4b71Sopenharmony_ci
419e41f4b71Sopenharmony_ci**参数:**
420e41f4b71Sopenharmony_ci
421e41f4b71Sopenharmony_ci| 参数名          | 类型                  | 必填 | 说明                                  |
422e41f4b71Sopenharmony_ci| -------------- | --------------------- | ---- | ------------------------------------ |
423e41f4b71Sopenharmony_ci| tokenID        | number                | 是   | 调用方的应用身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。|
424e41f4b71Sopenharmony_ci| permissionName | Permissions                | 是   | 需要使用的权限名,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
425e41f4b71Sopenharmony_ci| callback       | AsyncCallback&lt;void&gt; | 是   | 回调函数。当停止使用权限成功时,err为undefined;否则为错误对象。 |
426e41f4b71Sopenharmony_ci
427e41f4b71Sopenharmony_ci**错误码:**
428e41f4b71Sopenharmony_ci
429e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
430e41f4b71Sopenharmony_ci
431e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
432e41f4b71Sopenharmony_ci| -------- | -------- |
433e41f4b71Sopenharmony_ci| 201 | Permission denied. Interface caller does not have permission. |
434e41f4b71Sopenharmony_ci| 202 | Not System App. Interface caller is not a system app. |
435e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
436e41f4b71Sopenharmony_ci| 12100001 | Invalid parameter. The tokenID is 0, the permissionName exceeds 256 characters, or the count value is invalid. |
437e41f4b71Sopenharmony_ci| 12100002 | The specified tokenID does not exist or refer to an application process. |
438e41f4b71Sopenharmony_ci| 12100003 | The specified permission does not exist or is not an user_grant permission. |
439e41f4b71Sopenharmony_ci| 12100004 | The API is not used in pair with 'startUsingPermission'. |
440e41f4b71Sopenharmony_ci| 12100007 | The service is abnormal. |
441e41f4b71Sopenharmony_ci| 12100008 | Out of memory. |
442e41f4b71Sopenharmony_ci
443e41f4b71Sopenharmony_ci**示例:**
444e41f4b71Sopenharmony_ci
445e41f4b71Sopenharmony_ci```ts
446e41f4b71Sopenharmony_ciimport { privacyManager } from '@kit.AbilityKit';
447e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
448e41f4b71Sopenharmony_ci
449e41f4b71Sopenharmony_cilet tokenID: number = 0; // 可以通过getApplicationInfo获取accessTokenId
450e41f4b71Sopenharmony_ciprivacyManager.stopUsingPermission(tokenID, 'ohos.permission.READ_AUDIO', (err: BusinessError, data: void) => {
451e41f4b71Sopenharmony_ci  if (err) {
452e41f4b71Sopenharmony_ci    console.error(`stopUsingPermission fail, err->${JSON.stringify(err)}`);
453e41f4b71Sopenharmony_ci  } else {
454e41f4b71Sopenharmony_ci    console.log('stopUsingPermission success');
455e41f4b71Sopenharmony_ci  }
456e41f4b71Sopenharmony_ci});
457e41f4b71Sopenharmony_ci```
458e41f4b71Sopenharmony_ci
459e41f4b71Sopenharmony_ci## privacyManager.on
460e41f4b71Sopenharmony_ci
461e41f4b71Sopenharmony_cion(type: 'activeStateChange', permissionList: Array&lt;Permissions&gt;, callback: Callback&lt;ActiveChangeResponse&gt;): void
462e41f4b71Sopenharmony_ci
463e41f4b71Sopenharmony_ci订阅指定权限列表的权限使用状态变更事件。
464e41f4b71Sopenharmony_ci
465e41f4b71Sopenharmony_ci允许相同permissionList订阅多个callback。
466e41f4b71Sopenharmony_ci
467e41f4b71Sopenharmony_ci不允许存在交集的permissionList订阅相同callback。
468e41f4b71Sopenharmony_ci
469e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
470e41f4b71Sopenharmony_ci
471e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
472e41f4b71Sopenharmony_ci
473e41f4b71Sopenharmony_ci**参数:**
474e41f4b71Sopenharmony_ci
475e41f4b71Sopenharmony_ci| 参数名             | 类型                   | 必填 | 说明                                                          |
476e41f4b71Sopenharmony_ci| ------------------ | --------------------- | ---- | ------------------------------------------------------------ |
477e41f4b71Sopenharmony_ci| type               | string                | 是   | 订阅事件类型,固定为'activeStateChange',权限使用状态变更事件。   |
478e41f4b71Sopenharmony_ci| permissionList | Array&lt;Permissions&gt;   | 是   | 订阅的权限名列表,为空时表示订阅所有的权限使用状态变化,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
479e41f4b71Sopenharmony_ci| callback | Callback&lt;[ActiveChangeResponse](#activechangeresponse)&gt; | 是 | 订阅指定权限使用状态变更事件的回调。 |
480e41f4b71Sopenharmony_ci
481e41f4b71Sopenharmony_ci**错误码:**
482e41f4b71Sopenharmony_ci
483e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
484e41f4b71Sopenharmony_ci
485e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
486e41f4b71Sopenharmony_ci| -------- | -------- |
487e41f4b71Sopenharmony_ci| 201 | Permission denied. Interface caller does not have permission. |
488e41f4b71Sopenharmony_ci| 202 | Not System App. Interface caller is not a system app. |
489e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
490e41f4b71Sopenharmony_ci| 12100001 | Invalid parameter. The tokenID is 0, or the permissionName exceeds 256 characters. |
491e41f4b71Sopenharmony_ci| 12100004 | The API is used repeatedly with the same input. |
492e41f4b71Sopenharmony_ci| 12100005 | The registration time has exceeded the limitation. |
493e41f4b71Sopenharmony_ci| 12100007 | The service is abnormal. |
494e41f4b71Sopenharmony_ci| 12100008 | Out of memory. |
495e41f4b71Sopenharmony_ci
496e41f4b71Sopenharmony_ci**示例:**
497e41f4b71Sopenharmony_ci
498e41f4b71Sopenharmony_ci```ts
499e41f4b71Sopenharmony_ciimport { privacyManager, Permissions } from '@kit.AbilityKit';
500e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
501e41f4b71Sopenharmony_ci
502e41f4b71Sopenharmony_cilet permissionList: Array<Permissions> = [];
503e41f4b71Sopenharmony_citry {
504e41f4b71Sopenharmony_ci    privacyManager.on('activeStateChange', permissionList, (data: privacyManager.ActiveChangeResponse) => {
505e41f4b71Sopenharmony_ci        console.debug('receive permission state change, data:' + JSON.stringify(data));
506e41f4b71Sopenharmony_ci    });
507e41f4b71Sopenharmony_ci} catch(err) {
508e41f4b71Sopenharmony_ci    console.error(`catch err->${JSON.stringify(err)}`);
509e41f4b71Sopenharmony_ci}
510e41f4b71Sopenharmony_ci```
511e41f4b71Sopenharmony_ci
512e41f4b71Sopenharmony_ci## privacyManager.off
513e41f4b71Sopenharmony_ci
514e41f4b71Sopenharmony_cioff(type: 'activeStateChange', permissionList: Array&lt;Permissions&gt;, callback?: Callback&lt;ActiveChangeResponse&gt;): void
515e41f4b71Sopenharmony_ci
516e41f4b71Sopenharmony_ci取消订阅指定权限列表的权限使用状态变更事件。
517e41f4b71Sopenharmony_ci
518e41f4b71Sopenharmony_ci取消订阅不传callback时,批量删除permissionList下面的所有callback。
519e41f4b71Sopenharmony_ci
520e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
521e41f4b71Sopenharmony_ci
522e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
523e41f4b71Sopenharmony_ci
524e41f4b71Sopenharmony_ci**参数:**
525e41f4b71Sopenharmony_ci
526e41f4b71Sopenharmony_ci| 参数名             | 类型                   | 必填 | 说明                                                          |
527e41f4b71Sopenharmony_ci| ------------------ | --------------------- | ---- | ------------------------------------------------------------ |
528e41f4b71Sopenharmony_ci| type               | string                | 是   | 取消订阅事件类型,固定为'activeStateChange',权限使用状态变更事件。   |
529e41f4b71Sopenharmony_ci| permissionList | Array&lt;Permissions&gt;   | 是   | 取消订阅的权限名列表,为空时表示订阅所有的权限状态变化,必须与on的输入一致,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
530e41f4b71Sopenharmony_ci| callback | Callback&lt;[ActiveChangeResponse](#activechangeresponse)&gt; | 否 | 取消订阅指定tokenId与指定权限名状态变更事件的回调。|
531e41f4b71Sopenharmony_ci
532e41f4b71Sopenharmony_ci**错误码:**
533e41f4b71Sopenharmony_ci
534e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
535e41f4b71Sopenharmony_ci
536e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
537e41f4b71Sopenharmony_ci| -------- | -------- |
538e41f4b71Sopenharmony_ci| 201 | Permission denied. Interface caller does not have permission. |
539e41f4b71Sopenharmony_ci| 202 | Not System App. Interface caller is not a system app. |
540e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
541e41f4b71Sopenharmony_ci| 12100001 | Invalid parameter. The permissionNames in the list are all invalid, or the list size exceeds 1024 bytes. |
542e41f4b71Sopenharmony_ci| 12100004 | The API is not used in pair with 'on'. |
543e41f4b71Sopenharmony_ci| 12100007 | The service is abnormal. |
544e41f4b71Sopenharmony_ci| 12100008 | Out of memory. |
545e41f4b71Sopenharmony_ci
546e41f4b71Sopenharmony_ci**示例:**
547e41f4b71Sopenharmony_ci
548e41f4b71Sopenharmony_ci```ts
549e41f4b71Sopenharmony_ciimport { privacyManager, Permissions } from '@kit.AbilityKit';
550e41f4b71Sopenharmony_ci
551e41f4b71Sopenharmony_cilet permissionList: Array<Permissions> = [];
552e41f4b71Sopenharmony_citry {
553e41f4b71Sopenharmony_ci    privacyManager.off('activeStateChange', permissionList);
554e41f4b71Sopenharmony_ci} catch(err) {
555e41f4b71Sopenharmony_ci    console.error(`catch err->${JSON.stringify(err)}`);
556e41f4b71Sopenharmony_ci}
557e41f4b71Sopenharmony_ci```
558e41f4b71Sopenharmony_ci
559e41f4b71Sopenharmony_ci## privacyManager.getPermissionUsedTypeInfos<sup>12+</sup>
560e41f4b71Sopenharmony_ci
561e41f4b71Sopenharmony_cigetPermissionUsedTypeInfos(tokenId?: number, permissionName?: Permissions): Promise&lt;Array&lt;PermissionUsedTypeInfo&gt;&gt;
562e41f4b71Sopenharmony_ci
563e41f4b71Sopenharmony_ci查询设备上指定应用访问敏感权限时的信息(包括敏感权限名称、敏感权限访问方式)。
564e41f4b71Sopenharmony_ci
565e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
566e41f4b71Sopenharmony_ci
567e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
568e41f4b71Sopenharmony_ci
569e41f4b71Sopenharmony_ci**参数:**
570e41f4b71Sopenharmony_ci
571e41f4b71Sopenharmony_ci| 参数名             | 类型                   | 必填 | 说明                                                          |
572e41f4b71Sopenharmony_ci| ------------------ | --------------------- | ---- | ------------------------------------------------------------ |
573e41f4b71Sopenharmony_ci| tokenId            | number                | 否   | 访问敏感权限的应用身份标识,为空时表示查询所有应用的敏感权限访问类型信息。   |
574e41f4b71Sopenharmony_ci| permissionName     | Permissions           | 否   | 被访问的敏感权限名称,为空时标识查询所有敏感权限的访问类型信息。   |
575e41f4b71Sopenharmony_ci
576e41f4b71Sopenharmony_ci**返回值:**
577e41f4b71Sopenharmony_ci
578e41f4b71Sopenharmony_ci| 类型          | 说明                                    |
579e41f4b71Sopenharmony_ci| ------------- | --------------------------------------- |
580e41f4b71Sopenharmony_ci| Promise&lt;Array&lt;[PermissionUsedTypeInfo](#permissionusedtypeinfo12)&gt;&gt; | Promise对象。返回权限访问类型信息列表的Promise对象。|
581e41f4b71Sopenharmony_ci
582e41f4b71Sopenharmony_ci**错误码:**
583e41f4b71Sopenharmony_ci
584e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
585e41f4b71Sopenharmony_ci
586e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
587e41f4b71Sopenharmony_ci| -------- | -------- |
588e41f4b71Sopenharmony_ci| 201 | Permission denied. Interface caller does not have permission. |
589e41f4b71Sopenharmony_ci| 202 | Not System App. Interface caller is not a system app. |
590e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
591e41f4b71Sopenharmony_ci| 12100001 | Invalid parameter. PermissionName exceeds 256 characters. |
592e41f4b71Sopenharmony_ci| 12100002 | The input tokenId does not exist. |
593e41f4b71Sopenharmony_ci| 12100003 | The input permissionName does not exist. |
594e41f4b71Sopenharmony_ci
595e41f4b71Sopenharmony_ci**示例:**
596e41f4b71Sopenharmony_ci
597e41f4b71Sopenharmony_ci```ts
598e41f4b71Sopenharmony_ciimport { privacyManager, Permissions } from '@kit.AbilityKit';
599e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
600e41f4b71Sopenharmony_ci
601e41f4b71Sopenharmony_cilet tokenId: number = 0; // 可以通过bundleManager.getApplicationInfo获取accessTokenId
602e41f4b71Sopenharmony_cilet permissionName: Permissions = 'ohos.permission.CAMERA';
603e41f4b71Sopenharmony_ci// without any param
604e41f4b71Sopenharmony_ciprivacyManager.getPermissionUsedTypeInfos().then(() => {
605e41f4b71Sopenharmony_ci  console.log('getPermissionUsedTypeInfos success');
606e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
607e41f4b71Sopenharmony_ci  console.error(`getPermissionUsedTypeInfos fail, err->${JSON.stringify(err)}`);
608e41f4b71Sopenharmony_ci});
609e41f4b71Sopenharmony_ci// only tokenId
610e41f4b71Sopenharmony_ciprivacyManager.getPermissionUsedTypeInfos(tokenId).then(() => {
611e41f4b71Sopenharmony_ci  console.log('getPermissionUsedTypeInfos success');
612e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
613e41f4b71Sopenharmony_ci  console.error(`getPermissionUsedTypeInfos fail, err->${JSON.stringify(err)}`);
614e41f4b71Sopenharmony_ci});
615e41f4b71Sopenharmony_ci// only permissionName
616e41f4b71Sopenharmony_ciprivacyManager.getPermissionUsedTypeInfos(null, permissionName).then(() => {
617e41f4b71Sopenharmony_ci  console.log('getPermissionUsedTypeInfos success');
618e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
619e41f4b71Sopenharmony_ci  console.error(`getPermissionUsedTypeInfos fail, err->${JSON.stringify(err)}`);
620e41f4b71Sopenharmony_ci});
621e41f4b71Sopenharmony_ci// tokenId and permissionName
622e41f4b71Sopenharmony_ciprivacyManager.getPermissionUsedTypeInfos(tokenId, permissionName).then(() => {
623e41f4b71Sopenharmony_ci  console.log('getPermissionUsedTypeInfos success');
624e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
625e41f4b71Sopenharmony_ci  console.error(`getPermissionUsedTypeInfos fail, err->${JSON.stringify(err)}`);
626e41f4b71Sopenharmony_ci});
627e41f4b71Sopenharmony_ci```
628e41f4b71Sopenharmony_ci
629e41f4b71Sopenharmony_ci## PermissionUsageFlag
630e41f4b71Sopenharmony_ci
631e41f4b71Sopenharmony_ci使用记录的查询方式的枚举。
632e41f4b71Sopenharmony_ci
633e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
634e41f4b71Sopenharmony_ci
635e41f4b71Sopenharmony_ci| 名称                    | 值 | 说明                   |
636e41f4b71Sopenharmony_ci| ----------------------- | ------ | ---------------------- |
637e41f4b71Sopenharmony_ci| FLAG_PERMISSION_USAGE_SUMMARY             | 0    | 表示查询总览数据。 |
638e41f4b71Sopenharmony_ci| FLAG_PERMISSION_USAGE_DETAIL         | 1    | 表示查询详细数据。         |
639e41f4b71Sopenharmony_ci
640e41f4b71Sopenharmony_ci## PermissionUsedRequest
641e41f4b71Sopenharmony_ci
642e41f4b71Sopenharmony_ci表示使用记录的查询请求。
643e41f4b71Sopenharmony_ci
644e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
645e41f4b71Sopenharmony_ci
646e41f4b71Sopenharmony_ci| 名称       | 类型             | 必填   | 说明                                       |
647e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | ---------------------------------------- |
648e41f4b71Sopenharmony_ci| tokenId  | number         | 否    | 目标应用的身份标识。<br/> 默认查询所有应用。         |
649e41f4b71Sopenharmony_ci| isRemote | boolean         | 否    | 指定是否查询远端设备。<br/> 默认值:false,默认查询本端设备。 |
650e41f4b71Sopenharmony_ci| deviceId  | string         | 否    | 目标应用所在设备的ID。<br/> 默认设备ID为本端设备ID。   |
651e41f4b71Sopenharmony_ci| bundleName | string         | 否    | 目标应用的包名。<br/> 默认查询所有应用。 |
652e41f4b71Sopenharmony_ci| permissionNames  | Array&lt;Permissions&gt;         | 否    | 需要查询的权限集合。<br/> 默认查询所有权限的使用记录。               |
653e41f4b71Sopenharmony_ci| beginTime | number         | 否    | 查询的起始时间,单位:ms。<br/>默认值0,不设定起始时间。 |
654e41f4b71Sopenharmony_ci| endTime | number         | 否    | 查询的终止时间,单位:ms。<br/>默认值0,不设定终止时间。 |
655e41f4b71Sopenharmony_ci| flag | [PermissionUsageFlag](#permissionusageflag)         | 是    | 指定查询方式。 |
656e41f4b71Sopenharmony_ci
657e41f4b71Sopenharmony_ci## PermissionUsedResponse
658e41f4b71Sopenharmony_ci
659e41f4b71Sopenharmony_ci表示所有应用的访问记录。
660e41f4b71Sopenharmony_ci
661e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
662e41f4b71Sopenharmony_ci
663e41f4b71Sopenharmony_ci| 名称       | 类型             | 可读 | 可写 | 说明                                       |
664e41f4b71Sopenharmony_ci| --------- | -------------- | ---- | ---- | ---------------------------------------- |
665e41f4b71Sopenharmony_ci| beginTime | number         | 是    | 否    | 查询记录的起始时间,单位:ms。 |
666e41f4b71Sopenharmony_ci| endTime   | number         | 是    | 否    | 查询记录的终止时间,单位:ms。 |
667e41f4b71Sopenharmony_ci| bundleRecords  | Array&lt;[BundleUsedRecord](#bundleusedrecord)&gt;         | 是    | 否    | 应用的权限使用记录集合。                                 |
668e41f4b71Sopenharmony_ci
669e41f4b71Sopenharmony_ci## BundleUsedRecord
670e41f4b71Sopenharmony_ci
671e41f4b71Sopenharmony_ci某个应用的访问记录。
672e41f4b71Sopenharmony_ci
673e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
674e41f4b71Sopenharmony_ci
675e41f4b71Sopenharmony_ci| 名称       | 类型             | 可读 | 可写 | 说明                                       |
676e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | ---- | ---------------------------------------- |
677e41f4b71Sopenharmony_ci| tokenId  | number         | 是    | 否    | 目标应用的身份标识。                                 |
678e41f4b71Sopenharmony_ci| isRemote | boolean         | 是    | 否    | 默认值false。 |
679e41f4b71Sopenharmony_ci| deviceId  | string         | 是    | 否    | 目标应用所在设备的ID。                                 |
680e41f4b71Sopenharmony_ci| bundleName | string         | 是    | 否    | 目标应用的包名。 |
681e41f4b71Sopenharmony_ci| permissionRecords  | Array&lt;[PermissionUsedRecord](#permissionusedrecord)&gt;         | 是    | 否    | 每个应用的权限使用记录集合。                                 |
682e41f4b71Sopenharmony_ci
683e41f4b71Sopenharmony_ci## PermissionUsedRecord
684e41f4b71Sopenharmony_ci
685e41f4b71Sopenharmony_ci某个权限的访问记录。
686e41f4b71Sopenharmony_ci
687e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
688e41f4b71Sopenharmony_ci
689e41f4b71Sopenharmony_ci| 名称       | 类型             | 可读 | 可写 | 说明                                       |
690e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | ---- | ---------------------------------------- |
691e41f4b71Sopenharmony_ci| permissionName  | Permissions         | 是    | 否    | 权限名。                                 |
692e41f4b71Sopenharmony_ci| accessCount | number         | 是    | 否    | 该权限访问总次数。 |
693e41f4b71Sopenharmony_ci| rejectCount | number         | 是    | 否    | 该权限拒绝总次数。 |
694e41f4b71Sopenharmony_ci| lastAccessTime | number         | 是    | 否    | 最后一次访问时间,单位:ms。 |
695e41f4b71Sopenharmony_ci| lastRejectTime | number         | 是    | 否    | 最后一次拒绝时间,单位:ms。 |
696e41f4b71Sopenharmony_ci| lastAccessDuration | number         | 是    | 否    | 最后一次访问时长,单位:ms。 |
697e41f4b71Sopenharmony_ci| accessRecords  | Array&lt;[UsedRecordDetail](#usedrecorddetail)&gt;         | 是    | 否    | 访问记录集合,当flag为FLAG_PERMISSION_USAGE_DETAIL时生效,默认查询10条。                                 |
698e41f4b71Sopenharmony_ci| rejectRecords  | Array&lt;[UsedRecordDetail](#usedrecorddetail)&gt;         | 是    | 否    | 拒绝记录集合,当flag为FLAG_PERMISSION_USAGE_DETAIL时生效,默认查询10条。                                 |
699e41f4b71Sopenharmony_ci
700e41f4b71Sopenharmony_ci## UsedRecordDetail
701e41f4b71Sopenharmony_ci
702e41f4b71Sopenharmony_ci单次访问记录详情。
703e41f4b71Sopenharmony_ci
704e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
705e41f4b71Sopenharmony_ci
706e41f4b71Sopenharmony_ci| 名称       | 类型             | 可读 | 可写 | 说明                                       |
707e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | ---- | ---------------------------------------- |
708e41f4b71Sopenharmony_ci| status  | number         | 是    | 否    | 访问状态。                                 |
709e41f4b71Sopenharmony_ci| lockScreenStatus<sup>11+</sup>  | number         | 是    | 否    | 访问时的锁屏状态。<br> - 1,表示非锁屏场景使用权限。<br> - 2,表示锁屏场景使用权限。                                 |
710e41f4b71Sopenharmony_ci| timestamp | number         | 是    | 否    | 访问时的时间戳,单位:ms。 |
711e41f4b71Sopenharmony_ci| accessDuration  | number         | 是    | 否    | 访问时长,单位:ms。                                 |
712e41f4b71Sopenharmony_ci| count<sup>11+</sup> | number | 是 | 否    | 成功或失败次数。
713e41f4b71Sopenharmony_ci| usedType<sup>12+</sup> | [PermissionUsedType](#permissionusedtype12) | 是 | 否    | 敏感权限访问方式。 |
714e41f4b71Sopenharmony_ci
715e41f4b71Sopenharmony_ci## PermissionActiveStatus
716e41f4b71Sopenharmony_ci
717e41f4b71Sopenharmony_ci表示权限使用状态变化类型的枚举。
718e41f4b71Sopenharmony_ci
719e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
720e41f4b71Sopenharmony_ci
721e41f4b71Sopenharmony_ci| 名称                      | 值     | 说明              |
722e41f4b71Sopenharmony_ci| ------------------------- | ------ | ---------------- |
723e41f4b71Sopenharmony_ci| PERM_INACTIVE             | 0      | 表示未使用权限。   |
724e41f4b71Sopenharmony_ci| PERM_ACTIVE_IN_FOREGROUND | 1      | 表示前台使用权限。 |
725e41f4b71Sopenharmony_ci| PERM_ACTIVE_IN_BACKGROUND | 2      | 表示后台使用权限。 |
726e41f4b71Sopenharmony_ci
727e41f4b71Sopenharmony_ci## ActiveChangeResponse
728e41f4b71Sopenharmony_ci
729e41f4b71Sopenharmony_ci表示某次权限使用状态变化的详情。
730e41f4b71Sopenharmony_ci
731e41f4b71Sopenharmony_ci **系统能力:** SystemCapability.Security.AccessToken
732e41f4b71Sopenharmony_ci
733e41f4b71Sopenharmony_ci| 名称           | 类型                    | 可读 | 可写 | 说明                   |
734e41f4b71Sopenharmony_ci| -------------- | ---------------------- | ---- | ---- | --------------------- |
735e41f4b71Sopenharmony_ci| tokenId        | number                 | 是   | 否   | 被订阅的应用身份标识    |
736e41f4b71Sopenharmony_ci| permissionName | Permissions            | 是   | 否   | 权限使用状态发生变化的权限名 |
737e41f4b71Sopenharmony_ci| deviceId       | string                 | 是   | 否   | 设备号                 |
738e41f4b71Sopenharmony_ci| activeStatus   | [PermissionActiveStatus](#permissionactivestatus) | 是   | 否   | 权限使用状态变化类型        |
739e41f4b71Sopenharmony_ci
740e41f4b71Sopenharmony_ci## PermissionUsedType<sup>12+</sup>
741e41f4b71Sopenharmony_ci
742e41f4b71Sopenharmony_ci表示通过何种方式使用敏感权限的枚举。
743e41f4b71Sopenharmony_ci
744e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Security.AccessToken
745e41f4b71Sopenharmony_ci
746e41f4b71Sopenharmony_ci| 名称                    | 值 | 说明              |
747e41f4b71Sopenharmony_ci| ----------------------- | -- | ---------------- |
748e41f4b71Sopenharmony_ci| NORMAL_TYPE             | 0  | 表示通过弹窗授权或设置授权的方式来使用敏感权限。   |
749e41f4b71Sopenharmony_ci| PICKER_TYPE             | 1  | 表示通过某个PICKER服务来使用敏感权限,此方式未授予权限。 |
750e41f4b71Sopenharmony_ci| SECURITY_COMPONENT_TYPE | 2  | 表示通过安全控件授权的方式来使用敏感权限。 |
751e41f4b71Sopenharmony_ci
752e41f4b71Sopenharmony_ci## PermissionUsedTypeInfo<sup>12+</sup>
753e41f4b71Sopenharmony_ci
754e41f4b71Sopenharmony_ci表示某次权限使用类型的详情。
755e41f4b71Sopenharmony_ci
756e41f4b71Sopenharmony_ci **系统能力:** SystemCapability.Security.AccessToken
757e41f4b71Sopenharmony_ci
758e41f4b71Sopenharmony_ci| 名称           | 类型                    | 可读 | 可写 | 说明                   |
759e41f4b71Sopenharmony_ci| -------------- | ---------------------- | ---- | ---- | --------------------- |
760e41f4b71Sopenharmony_ci| tokenId        | number                 | 是   | 否   | 访问敏感权限的应用身份标识 |
761e41f4b71Sopenharmony_ci| permissionName | Permissions            | 是   | 否   | 被访问的敏感权限名称 |
762e41f4b71Sopenharmony_ci| usedType | [PermissionUsedType](#permissionusedtype12) | 是 | 否    | 敏感权限使用类型。 |
763e41f4b71Sopenharmony_ci
764e41f4b71Sopenharmony_ci## AddPermissionUsedRecordOptions<sup>12+</sup>
765e41f4b71Sopenharmony_ci
766e41f4b71Sopenharmony_ci添加权限使用记录可选参数集。
767e41f4b71Sopenharmony_ci
768e41f4b71Sopenharmony_ci **系统能力:** SystemCapability.Security.AccessToken
769e41f4b71Sopenharmony_ci
770e41f4b71Sopenharmony_ci| 名称           | 类型                    | 可读 | 可写 | 说明                   |
771e41f4b71Sopenharmony_ci| -------------- | ---------------------- | ---- | ---- | --------------------- |
772e41f4b71Sopenharmony_ci| usedType | [PermissionUsedType](#permissionusedtype12) | 是 | 否    | 敏感权限使用类型。 |
773