• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/permissions-for-all.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**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
260
261**系统能力:** SystemCapability.Security.AccessToken
262
263**参数:**
264
265| 参数名          | 类型   | 必填 | 说明                                  |
266| -------------- | ------ | ---- | ------------------------------------ |
267| tokenID        | number | 是   | 调用方的应用身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。|
268| permissionName | Permissions | 是   | 需要使用的权限名,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
269
270**返回值:**
271
272| 类型          | 说明                                    |
273| ------------- | --------------------------------------- |
274| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。|
275
276**错误码:**
277
278以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
279
280| 错误码ID | 错误信息 |
281| -------- | -------- |
282| 201 | Permission denied. Interface caller does not have permission. |
283| 202 | Not System App. Interface caller is not a system app. |
284| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
285| 12100001 | Invalid parameter. The tokenID is 0, the permissionName exceeds 256 characters, or the count value is invalid. |
286| 12100002 | The specified tokenID does not exist or refer to an application process. |
287| 12100003 | The specified permission does not exist or is not an user_grant permission. |
288| 12100004 | The API is used repeatedly with the same input. It means the application specified by the tokenID has been using the specified permission. |
289| 12100007 | The service is abnormal. |
290| 12100008 | Out of memory. |
291
292**示例:**
293
294```ts
295import { privacyManager } from '@kit.AbilityKit';
296import { BusinessError } from '@kit.BasicServicesKit';
297
298let tokenID: number = 0; // 可以通过getApplicationInfo获取accessTokenId
299privacyManager.startUsingPermission(tokenID, 'ohos.permission.READ_AUDIO').then(() => {
300  console.log('startUsingPermission success');
301}).catch((err: BusinessError) => {
302  console.error(`startUsingPermission fail, err->${JSON.stringify(err)}`);
303});
304```
305
306## privacyManager.startUsingPermission
307
308startUsingPermission(tokenID: number, permissionName: Permissions, callback: AsyncCallback&lt;void&gt;): void
309
310应用开始使用某项权限,可监听应用在前后台使用权限,并将使用权限的记录落盘,由系统服务调用。使用callback异步回调。
311
312**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
313
314**系统能力:** SystemCapability.Security.AccessToken
315
316**参数:**
317
318| 参数名          | 类型                  | 必填 | 说明                                  |
319| -------------- | --------------------- | ---- | ------------------------------------ |
320| tokenID        | number                | 是   | 调用方的应用身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。|
321| permissionName | Permissions                | 是   | 需要使用的权限名,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
322| callback       | AsyncCallback&lt;void&gt; | 是   | 回调函数。当开始使用权限成功时,err为undefined;否则为错误对象。 |
323
324**错误码:**
325
326以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
327
328| 错误码ID | 错误信息 |
329| -------- | -------- |
330| 201 | Permission denied. Interface caller does not have permission. |
331| 202 | Not System App. Interface caller is not a system app. |
332| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
333| 12100001 | Invalid parameter. The tokenID is 0, the permissionName exceeds 256 characters, or the count value is invalid. |
334| 12100002 | The specified tokenID does not exist or refer to an application process. |
335| 12100003 | The specified permission does not exist or is not an user_grant permission. |
336| 12100004 | The API is used repeatedly with the same input. It means the application specified by the tokenID has been using the specified permission. |
337| 12100007 | The service is abnormal. |
338| 12100008 | Out of memory. |
339
340**示例:**
341
342```ts
343import { privacyManager } from '@kit.AbilityKit';
344import { BusinessError } from '@kit.BasicServicesKit';
345
346let tokenID: number = 0; // 可以通过getApplicationInfo获取accessTokenId
347privacyManager.startUsingPermission(tokenID, 'ohos.permission.READ_AUDIO', (err: BusinessError, data: void) => {
348  if (err) {
349    console.error(`startUsingPermission fail, err->${JSON.stringify(err)}`);
350  } else {
351    console.log('startUsingPermission success');
352  }
353});
354```
355
356## privacyManager.stopUsingPermission
357
358stopUsingPermission(tokenID: number, permissionName: Permissions): Promise&lt;void&gt;
359
360应用停止使用某项权限,与Start对应,由系统服务调用。使用Promise异步回调。
361
362**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
363
364**系统能力:** SystemCapability.Security.AccessToken
365
366**参数:**
367
368| 参数名          | 类型   | 必填 | 说明                                  |
369| -------------- | ------ | ---- | ------------------------------------ |
370| tokenID        | number | 是   | 调用方的应用身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。|
371| permissionName | Permissions | 是   | 需要使用的权限名,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
372
373**返回值:**
374
375| 类型          | 说明                                    |
376| ------------- | --------------------------------------- |
377| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。|
378
379**错误码:**
380
381以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
382
383| 错误码ID | 错误信息 |
384| -------- | -------- |
385| 201 | Permission denied. Interface caller does not have permission. |
386| 202 | Not System App. Interface caller is not a system app. |
387| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
388| 12100001 | Invalid parameter. The tokenID is 0, the permissionName exceeds 256 characters, or the count value is invalid. |
389| 12100002 | The specified tokenID does not exist or refer to an application process. |
390| 12100003 | The specified permission does not exist or is not an user_grant permission. |
391| 12100004 | The API is not used in pair with 'startUsingPermission'. |
392| 12100007 | The service is abnormal. |
393| 12100008 | Out of memory. |
394
395**示例:**
396
397```ts
398import { privacyManager } from '@kit.AbilityKit';
399import { BusinessError } from '@kit.BasicServicesKit';
400
401let tokenID: number = 0; // 可以通过getApplicationInfo获取accessTokenId
402privacyManager.stopUsingPermission(tokenID, 'ohos.permission.READ_AUDIO').then(() => {
403  console.log('stopUsingPermission success');
404}).catch((err: BusinessError) => {
405  console.error(`stopUsingPermission fail, err->${JSON.stringify(err)}`);
406});
407```
408
409## privacyManager.stopUsingPermission
410
411stopUsingPermission(tokenID: number, permissionName: Permissions, callback: AsyncCallback&lt;void&gt;): void
412
413应用停止使用某项权限,与Start对应,由系统服务调用。使用callback异步回调。
414
415**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
416
417**系统能力:** SystemCapability.Security.AccessToken
418
419**参数:**
420
421| 参数名          | 类型                  | 必填 | 说明                                  |
422| -------------- | --------------------- | ---- | ------------------------------------ |
423| tokenID        | number                | 是   | 调用方的应用身份标识。可通过应用的[ApplicationInfo](js-apis-bundleManager-applicationInfo.md)的accessTokenId字段获得。|
424| permissionName | Permissions                | 是   | 需要使用的权限名,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
425| callback       | AsyncCallback&lt;void&gt; | 是   | 回调函数。当停止使用权限成功时,err为undefined;否则为错误对象。 |
426
427**错误码:**
428
429以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
430
431| 错误码ID | 错误信息 |
432| -------- | -------- |
433| 201 | Permission denied. Interface caller does not have permission. |
434| 202 | Not System App. Interface caller is not a system app. |
435| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
436| 12100001 | Invalid parameter. The tokenID is 0, the permissionName exceeds 256 characters, or the count value is invalid. |
437| 12100002 | The specified tokenID does not exist or refer to an application process. |
438| 12100003 | The specified permission does not exist or is not an user_grant permission. |
439| 12100004 | The API is not used in pair with 'startUsingPermission'. |
440| 12100007 | The service is abnormal. |
441| 12100008 | Out of memory. |
442
443**示例:**
444
445```ts
446import { privacyManager } from '@kit.AbilityKit';
447import { BusinessError } from '@kit.BasicServicesKit';
448
449let tokenID: number = 0; // 可以通过getApplicationInfo获取accessTokenId
450privacyManager.stopUsingPermission(tokenID, 'ohos.permission.READ_AUDIO', (err: BusinessError, data: void) => {
451  if (err) {
452    console.error(`stopUsingPermission fail, err->${JSON.stringify(err)}`);
453  } else {
454    console.log('stopUsingPermission success');
455  }
456});
457```
458
459## privacyManager.on
460
461on(type: 'activeStateChange', permissionList: Array&lt;Permissions&gt;, callback: Callback&lt;ActiveChangeResponse&gt;): void
462
463订阅指定权限列表的权限使用状态变更事件。
464
465允许相同permissionList订阅多个callback。
466
467不允许存在交集的permissionList订阅相同callback。
468
469**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
470
471**系统能力:** SystemCapability.Security.AccessToken
472
473**参数:**
474
475| 参数名             | 类型                   | 必填 | 说明                                                          |
476| ------------------ | --------------------- | ---- | ------------------------------------------------------------ |
477| type               | string                | 是   | 订阅事件类型,固定为'activeStateChange',权限使用状态变更事件。   |
478| permissionList | Array&lt;Permissions&gt;   | 是   | 订阅的权限名列表,为空时表示订阅所有的权限使用状态变化,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
479| callback | Callback&lt;[ActiveChangeResponse](#activechangeresponse)&gt; | 是 | 订阅指定权限使用状态变更事件的回调。 |
480
481**错误码:**
482
483以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
484
485| 错误码ID | 错误信息 |
486| -------- | -------- |
487| 201 | Permission denied. Interface caller does not have permission. |
488| 202 | Not System App. Interface caller is not a system app. |
489| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
490| 12100001 | Invalid parameter. The tokenID is 0, or the permissionName exceeds 256 characters. |
491| 12100004 | The API is used repeatedly with the same input. |
492| 12100005 | The registration time has exceeded the limitation. |
493| 12100007 | The service is abnormal. |
494| 12100008 | Out of memory. |
495
496**示例:**
497
498```ts
499import { privacyManager, Permissions } from '@kit.AbilityKit';
500import { BusinessError } from '@kit.BasicServicesKit';
501
502let permissionList: Array<Permissions> = [];
503try {
504    privacyManager.on('activeStateChange', permissionList, (data: privacyManager.ActiveChangeResponse) => {
505        console.debug('receive permission state change, data:' + JSON.stringify(data));
506    });
507} catch(err) {
508    console.error(`catch err->${JSON.stringify(err)}`);
509}
510```
511
512## privacyManager.off
513
514off(type: 'activeStateChange', permissionList: Array&lt;Permissions&gt;, callback?: Callback&lt;ActiveChangeResponse&gt;): void
515
516取消订阅指定权限列表的权限使用状态变更事件。
517
518取消订阅不传callback时,批量删除permissionList下面的所有callback。
519
520**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
521
522**系统能力:** SystemCapability.Security.AccessToken
523
524**参数:**
525
526| 参数名             | 类型                   | 必填 | 说明                                                          |
527| ------------------ | --------------------- | ---- | ------------------------------------------------------------ |
528| type               | string                | 是   | 取消订阅事件类型,固定为'activeStateChange',权限使用状态变更事件。   |
529| permissionList | Array&lt;Permissions&gt;   | 是   | 取消订阅的权限名列表,为空时表示订阅所有的权限状态变化,必须与on的输入一致,合法的权限名取值可在[应用权限列表](../../security/AccessToken/permissions-for-all.md)中查询。|
530| callback | Callback&lt;[ActiveChangeResponse](#activechangeresponse)&gt; | 否 | 取消订阅指定tokenId与指定权限名状态变更事件的回调。|
531
532**错误码:**
533
534以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
535
536| 错误码ID | 错误信息 |
537| -------- | -------- |
538| 201 | Permission denied. Interface caller does not have permission. |
539| 202 | Not System App. Interface caller is not a system app. |
540| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
541| 12100001 | Invalid parameter. The permissionNames in the list are all invalid, or the list size exceeds 1024 bytes. |
542| 12100004 | The API is not used in pair with 'on'. |
543| 12100007 | The service is abnormal. |
544| 12100008 | Out of memory. |
545
546**示例:**
547
548```ts
549import { privacyManager, Permissions } from '@kit.AbilityKit';
550
551let permissionList: Array<Permissions> = [];
552try {
553    privacyManager.off('activeStateChange', permissionList);
554} catch(err) {
555    console.error(`catch err->${JSON.stringify(err)}`);
556}
557```
558
559## privacyManager.getPermissionUsedTypeInfos<sup>12+</sup>
560
561getPermissionUsedTypeInfos(tokenId?: number, permissionName?: Permissions): Promise&lt;Array&lt;PermissionUsedTypeInfo&gt;&gt;
562
563查询设备上指定应用访问敏感权限时的信息(包括敏感权限名称、敏感权限访问方式)。
564
565**需要权限:** ohos.permission.PERMISSION_USED_STATS,仅系统应用可用。
566
567**系统能力:** SystemCapability.Security.AccessToken
568
569**参数:**
570
571| 参数名             | 类型                   | 必填 | 说明                                                          |
572| ------------------ | --------------------- | ---- | ------------------------------------------------------------ |
573| tokenId            | number                | 否   | 访问敏感权限的应用身份标识,为空时表示查询所有应用的敏感权限访问类型信息。   |
574| permissionName     | Permissions           | 否   | 被访问的敏感权限名称,为空时标识查询所有敏感权限的访问类型信息。   |
575
576**返回值:**
577
578| 类型          | 说明                                    |
579| ------------- | --------------------------------------- |
580| Promise&lt;Array&lt;[PermissionUsedTypeInfo](#permissionusedtypeinfo12)&gt;&gt; | Promise对象。返回权限访问类型信息列表的Promise对象。|
581
582**错误码:**
583
584以下错误码的详细介绍请参见[访问控制错误码](errorcode-access-token.md)。
585
586| 错误码ID | 错误信息 |
587| -------- | -------- |
588| 201 | Permission denied. Interface caller does not have permission. |
589| 202 | Not System App. Interface caller is not a system app. |
590| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
591| 12100001 | Invalid parameter. PermissionName exceeds 256 characters. |
592| 12100002 | The input tokenId does not exist. |
593| 12100003 | The input permissionName does not exist. |
594
595**示例:**
596
597```ts
598import { privacyManager, Permissions } from '@kit.AbilityKit';
599import { BusinessError } from '@kit.BasicServicesKit';
600
601let tokenId: number = 0; // 可以通过bundleManager.getApplicationInfo获取accessTokenId
602let permissionName: Permissions = 'ohos.permission.CAMERA';
603// without any param
604privacyManager.getPermissionUsedTypeInfos().then(() => {
605  console.log('getPermissionUsedTypeInfos success');
606}).catch((err: BusinessError) => {
607  console.error(`getPermissionUsedTypeInfos fail, err->${JSON.stringify(err)}`);
608});
609// only tokenId
610privacyManager.getPermissionUsedTypeInfos(tokenId).then(() => {
611  console.log('getPermissionUsedTypeInfos success');
612}).catch((err: BusinessError) => {
613  console.error(`getPermissionUsedTypeInfos fail, err->${JSON.stringify(err)}`);
614});
615// only permissionName
616privacyManager.getPermissionUsedTypeInfos(null, permissionName).then(() => {
617  console.log('getPermissionUsedTypeInfos success');
618}).catch((err: BusinessError) => {
619  console.error(`getPermissionUsedTypeInfos fail, err->${JSON.stringify(err)}`);
620});
621// tokenId and permissionName
622privacyManager.getPermissionUsedTypeInfos(tokenId, permissionName).then(() => {
623  console.log('getPermissionUsedTypeInfos success');
624}).catch((err: BusinessError) => {
625  console.error(`getPermissionUsedTypeInfos fail, err->${JSON.stringify(err)}`);
626});
627```
628
629## PermissionUsageFlag
630
631使用记录的查询方式的枚举。
632
633**系统能力:** SystemCapability.Security.AccessToken
634
635| 名称                    | 值 | 说明                   |
636| ----------------------- | ------ | ---------------------- |
637| FLAG_PERMISSION_USAGE_SUMMARY             | 0    | 表示查询总览数据。 |
638| FLAG_PERMISSION_USAGE_DETAIL         | 1    | 表示查询详细数据。         |
639
640## PermissionUsedRequest
641
642表示使用记录的查询请求。
643
644**系统能力:** SystemCapability.Security.AccessToken
645
646| 名称       | 类型             | 必填   | 说明                                       |
647| -------- | -------------- | ---- | ---------------------------------------- |
648| tokenId  | number         | 否    | 目标应用的身份标识。<br/> 默认查询所有应用。         |
649| isRemote | boolean         | 否    | 指定是否查询远端设备。<br/> 默认值:false,默认查询本端设备。 |
650| deviceId  | string         | 否    | 目标应用所在设备的ID。<br/> 默认设备ID为本端设备ID。   |
651| bundleName | string         | 否    | 目标应用的包名。<br/> 默认查询所有应用。 |
652| permissionNames  | Array&lt;Permissions&gt;         | 否    | 需要查询的权限集合。<br/> 默认查询所有权限的使用记录。               |
653| beginTime | number         | 否    | 查询的起始时间,单位:ms。<br/>默认值0,不设定起始时间。 |
654| endTime | number         | 否    | 查询的终止时间,单位:ms。<br/>默认值0,不设定终止时间。 |
655| flag | [PermissionUsageFlag](#permissionusageflag)         | 是    | 指定查询方式。 |
656
657## PermissionUsedResponse
658
659表示所有应用的访问记录。
660
661**系统能力:** SystemCapability.Security.AccessToken
662
663| 名称       | 类型             | 可读 | 可写 | 说明                                       |
664| --------- | -------------- | ---- | ---- | ---------------------------------------- |
665| beginTime | number         | 是    | 否    | 查询记录的起始时间,单位:ms。 |
666| endTime   | number         | 是    | 否    | 查询记录的终止时间,单位:ms。 |
667| bundleRecords  | Array&lt;[BundleUsedRecord](#bundleusedrecord)&gt;         | 是    | 否    | 应用的权限使用记录集合。                                 |
668
669## BundleUsedRecord
670
671某个应用的访问记录。
672
673**系统能力:** SystemCapability.Security.AccessToken
674
675| 名称       | 类型             | 可读 | 可写 | 说明                                       |
676| -------- | -------------- | ---- | ---- | ---------------------------------------- |
677| tokenId  | number         | 是    | 否    | 目标应用的身份标识。                                 |
678| isRemote | boolean         | 是    | 否    | 默认值false。 |
679| deviceId  | string         | 是    | 否    | 目标应用所在设备的ID。                                 |
680| bundleName | string         | 是    | 否    | 目标应用的包名。 |
681| permissionRecords  | Array&lt;[PermissionUsedRecord](#permissionusedrecord)&gt;         | 是    | 否    | 每个应用的权限使用记录集合。                                 |
682
683## PermissionUsedRecord
684
685某个权限的访问记录。
686
687**系统能力:** SystemCapability.Security.AccessToken
688
689| 名称       | 类型             | 可读 | 可写 | 说明                                       |
690| -------- | -------------- | ---- | ---- | ---------------------------------------- |
691| permissionName  | Permissions         | 是    | 否    | 权限名。                                 |
692| accessCount | number         | 是    | 否    | 该权限访问总次数。 |
693| rejectCount | number         | 是    | 否    | 该权限拒绝总次数。 |
694| lastAccessTime | number         | 是    | 否    | 最后一次访问时间,单位:ms。 |
695| lastRejectTime | number         | 是    | 否    | 最后一次拒绝时间,单位:ms。 |
696| lastAccessDuration | number         | 是    | 否    | 最后一次访问时长,单位:ms。 |
697| accessRecords  | Array&lt;[UsedRecordDetail](#usedrecorddetail)&gt;         | 是    | 否    | 访问记录集合,当flag为FLAG_PERMISSION_USAGE_DETAIL时生效,默认查询10条。                                 |
698| rejectRecords  | Array&lt;[UsedRecordDetail](#usedrecorddetail)&gt;         | 是    | 否    | 拒绝记录集合,当flag为FLAG_PERMISSION_USAGE_DETAIL时生效,默认查询10条。                                 |
699
700## UsedRecordDetail
701
702单次访问记录详情。
703
704**系统能力:** SystemCapability.Security.AccessToken
705
706| 名称       | 类型             | 可读 | 可写 | 说明                                       |
707| -------- | -------------- | ---- | ---- | ---------------------------------------- |
708| status  | number         | 是    | 否    | 访问状态。                                 |
709| lockScreenStatus<sup>11+</sup>  | number         | 是    | 否    | 访问时的锁屏状态。<br> - 1,表示非锁屏场景使用权限。<br> - 2,表示锁屏场景使用权限。                                 |
710| timestamp | number         | 是    | 否    | 访问时的时间戳,单位:ms。 |
711| accessDuration  | number         | 是    | 否    | 访问时长,单位:ms。                                 |
712| count<sup>11+</sup> | number | 是 | 否    | 成功或失败次数。
713| usedType<sup>12+</sup> | [PermissionUsedType](#permissionusedtype12) | 是 | 否    | 敏感权限访问方式。 |
714
715## PermissionActiveStatus
716
717表示权限使用状态变化类型的枚举。
718
719**系统能力:** SystemCapability.Security.AccessToken
720
721| 名称                      | 值     | 说明              |
722| ------------------------- | ------ | ---------------- |
723| PERM_INACTIVE             | 0      | 表示未使用权限。   |
724| PERM_ACTIVE_IN_FOREGROUND | 1      | 表示前台使用权限。 |
725| PERM_ACTIVE_IN_BACKGROUND | 2      | 表示后台使用权限。 |
726
727## ActiveChangeResponse
728
729表示某次权限使用状态变化的详情。
730
731 **系统能力:** SystemCapability.Security.AccessToken
732
733| 名称           | 类型                    | 可读 | 可写 | 说明                   |
734| -------------- | ---------------------- | ---- | ---- | --------------------- |
735| tokenId        | number                 | 是   | 否   | 被订阅的应用身份标识    |
736| permissionName | Permissions            | 是   | 否   | 权限使用状态发生变化的权限名 |
737| deviceId       | string                 | 是   | 否   | 设备号                 |
738| activeStatus   | [PermissionActiveStatus](#permissionactivestatus) | 是   | 否   | 权限使用状态变化类型        |
739
740## PermissionUsedType<sup>12+</sup>
741
742表示通过何种方式使用敏感权限的枚举。
743
744**系统能力:** SystemCapability.Security.AccessToken
745
746| 名称                    | 值 | 说明              |
747| ----------------------- | -- | ---------------- |
748| NORMAL_TYPE             | 0  | 表示通过弹窗授权或设置授权的方式来使用敏感权限。   |
749| PICKER_TYPE             | 1  | 表示通过某个PICKER服务来使用敏感权限,此方式未授予权限。 |
750| SECURITY_COMPONENT_TYPE | 2  | 表示通过安全控件授权的方式来使用敏感权限。 |
751
752## PermissionUsedTypeInfo<sup>12+</sup>
753
754表示某次权限使用类型的详情。
755
756 **系统能力:** SystemCapability.Security.AccessToken
757
758| 名称           | 类型                    | 可读 | 可写 | 说明                   |
759| -------------- | ---------------------- | ---- | ---- | --------------------- |
760| tokenId        | number                 | 是   | 否   | 访问敏感权限的应用身份标识 |
761| permissionName | Permissions            | 是   | 否   | 被访问的敏感权限名称 |
762| usedType | [PermissionUsedType](#permissionusedtype12) | 是 | 否    | 敏感权限使用类型。 |
763
764## AddPermissionUsedRecordOptions<sup>12+</sup>
765
766添加权限使用记录可选参数集。
767
768 **系统能力:** SystemCapability.Security.AccessToken
769
770| 名称           | 类型                    | 可读 | 可写 | 说明                   |
771| -------------- | ---------------------- | ---- | ---- | --------------------- |
772| usedType | [PermissionUsedType](#permissionusedtype12) | 是 | 否    | 敏感权限使用类型。 |
773