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