• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 设备使用信息统计(API9)
2
3## 场景介绍
4
5设备使用信息统计,包括app usage/notification usage/system usage等使用统计。例如应用使用信息统计,用于保存和查询应用使用详情(app usage)、事件日志数据(event log)、应用分组(app group)情况。
6部件缓存的应用记录(使用历史统计和使用事件记录)会在事件上报后30分钟内刷新到数据库持久化保存。
7
8## 接口说明
9注册相关接口包导入:
10```js
11import usageStatistics from '@ohos.resourceschedule.usageStatistics';
12```
13
14**表1** 设备使用信息统计主要接口
15
16| 接口名 | 描述 |
17| -------- | -------- |
18| function queryBundleEvents(begin: number, end: number, callback: AsyncCallback<Array<BundleEvents>>): void | 通过指定起始和结束时间查询所有应用的事件集合。 |
19| function queryBundleStatsInfos(begin: number, end: number, callback: AsyncCallback<BundleStatsMap>): void | 通过指定起始和结束时间查询应用使用时长统计信息。 |
20| function queryCurrentBundleEvents(begin: number, end: number, callback: AsyncCallback<Array<BundleEvents>>): void | 通过指定起始和结束时间查询当前应用的事件集合。 |
21| function queryBundleStatsInfoByInterval(byInterval: IntervalType, begin: number, end: number, callback: AsyncCallback<Array<BundleStatsInfo>>): void | 通过指定时间段间隔(天、周、月、年)查询应用使用时长统计信息。 |
22| function queryAppGroup(callback: AsyncCallback<number>): void | 查询当前应用的使用优先级群组。callback形式。 |
23| function queryAppGroup(): Promise<number>; | 查询当前应用的使用优先级群组。promise形式。 |
24| function queryAppGroup(bundleName : string, callback: AsyncCallback<number>): void | 查询指定应用的使用优先级群组。callback形式。 |
25| function queryAppGroup(bundleName : string): Promise<number>; | 查询当前调用者应用或者指定应用的使用优先级群组。promise形式。 |
26| function isIdleState(bundleName: string, callback: AsyncCallback<boolean>): void | 判断指定Bundle Name的应用当前是否是空闲状态。 |
27| function queryModuleUsageRecords(callback: AsyncCallback<HapModuleInfo>): void | 查询FA使用记录,返回不超过1000条FA使用记录。 |
28| function queryModuleUsageRecords(maxNum: number, callback: AsyncCallback<HapModuleInfo>): void | 根据maxNum,查询FA使用记录,返回不超过maxNum条FA使用记录。 maxNum不超过1000|
29| function queryNotificationEventStats(begin: number, end: number, callback: AsyncCallback<Array<DeviceEventStats>>): void | 通过指定起始和结束时间查询所有应用的通知次数。 |
30| function queryDeviceEventStats(begin: number, end: number, callback: AsyncCallback<Array<DeviceEventStats>>): void | 通过指定起始和结束时间查询系统事件(休眠、唤醒、解锁、锁屏)统计信息。 |
31| function setAppGroup(bundleName : string, newGroup: GroupType, callback: AsyncCallback>boolean>): void | 给应用名是bundleName的应用分组设置成newGroup,返回设置结果是否成功,以callback形式返回。 |
32| function setAppGroup(bundleName : string, newGroup : GroupType): Promise>boolean>; | 给应用名是bundleName的应用分组设置成newGroup,返回设置结果是否成功,以promise形式返回。 |
33| function registerAppGroupCallBack(groupCallback: Callback>AppGroupCallbackInfo>, callback: AsyncCallback>boolean>): void | 注册应用分组变化监听回调,返回注册是否成功,当应用分组发生变化时,会给所有已注册的监听者返回回调信息,以callback形式返回。 |
34| function registerAppGroupCallBack(groupCallback: Callback>AppGroupCallbackInfo>): Promise>boolean>; | 注册应用分组变化监听回调,返回注册是否成功,当应用分组发生变化时,会给所有已注册的监听者返回回调信息,以promise形式返回。 |
35| function unregisterAppGroupCallBack(callback: AsyncCallback>boolean>): void | 解除应用分组监听回调,以callback形式返回。 |
36| function unregisterAppGroupCallBack(): Promise>boolean>; | 解除应用分组监听回调,以promise形式返回。 |
37
38## 开发步骤
39
401. 获取设备使用信息之前,需要检查是否已经配置请求相应的权限。
41    系统提供的设备使用信息统计的权限是ohos.permission.BUNDLE_ACTIVE_INFO
42    具体配置方式请参考[权限申请声明](../security/accesstoken-guidelines.md)
43
442. 通过指定起始和结束时间查询所有应用的事件集合,需要配置ohos.permission.BUNDLE_ACTIVE_INFO权限。
45
46    ```js
47    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
48
49    // 异步方法promise方式
50    try{
51        usageStatistics.queryBundleEvents(0, 20000000000000).then( res => {
52            console.log('BUNDLE_ACTIVE queryBundleEvents promise success.');
53            for (let i = 0; i < res.length; i++) {
54                console.log('BUNDLE_ACTIVE queryBundleEvents promise number : ' + (i + 1));
55                console.log('BUNDLE_ACTIVE queryBundleEvents promise result ' + JSON.stringify(res[i]));
56            }
57        }).catch( err => {
58            console.log('BUNDLE_ACTIVE queryBundleEvents promise failed. code is: ' + err.code + ',message is: ' + err.message);
59        });
60    } catch (error) {
61        console.log('BUNDLE_ACTIVE queryBundleEvents throw error, code is: ' + error.code + ',message is: ' + error.message);
62    }
63
64    // 异步方法callback方式
65    try{
66        usageStatistics.queryBundleEvents(0, 20000000000000, (err, res) => {
67            if (err) {
68                console.log('BUNDLE_ACTIVE queryBundleEvents callback failed. code is: ' + err.code + ',message is: ' + err.message);
69            } else {
70                console.log('BUNDLE_ACTIVE queryBundleEvents callback success.');
71            for (let i = 0; i < res.length; i++) {
72                console.log('BUNDLE_ACTIVE queryBundleEvents callback number : ' + (i + 1));
73                console.log('BUNDLE_ACTIVE queryBundleEvents callback result ' + JSON.stringify(res[i]));
74            }
75            }
76        });
77    } catch (error) {
78        console.log('BUNDLE_ACTIVE queryBundleEvents throw error, code is: ' + error.code + ',message is: ' + error.message);
79    }
80    ```
81
823. 通过指定起始和结束时间查询应用使用时长统计信息,需要配置ohos.permission.BUNDLE_ACTIVE_INFO权限。
83
84    ```js
85    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
86
87    // 异步方法promise方式
88    try{
89        usageStatistics.queryBundleStatsInfos(0, 20000000000000).then( res => {
90            console.log('BUNDLE_ACTIVE queryBundleStatsInfos promise success.');
91            let i = 1;
92            for(let key in res){
93                console.log('BUNDLE_ACTIVE queryBundleStatsInfos promise number : ' + i);
94                console.log('BUNDLE_ACTIVE queryBundleStatsInfos promise result ' + JSON.stringify(res[key]));
95                i++;
96            }
97        }).catch( err => {
98            console.log('BUNDLE_ACTIVE queryBundleStatsInfos promise failed. code is: ' + err.code + ',message is: ' + err.message);
99        });
100    } catch (error) {
101        console.log('BUNDLE_ACTIVE queryBundleStatsInfos throw error, code is: ' + error.code + ',message is: ' + error.message);
102    }
103
104    // 异步方法callback方式
105    try{
106        usageStatistics.queryBundleStatsInfos(0, 20000000000000, (err, res) => {
107            if (err) {
108                console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback failed. code is: ' + err.code + ',message is: ' + err.message);
109            } else {
110                console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback success.');
111            let i = 1;
112            for(let key in res){
113                console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback number : ' + i);
114                console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback result ' + JSON.stringify(res[key]));
115                i++;
116            }
117            }
118        });
119    } catch (error) {
120        console.log('BUNDLE_ACTIVE queryBundleStatsInfos throw error, code is: ' + error.code + ',message is: ' + error.message);
121    }
122    ```
123
1244. 通过指定起始和结束时间查询当前应用的事件集合,不需要配置权限。
125
126    ```js
127    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
128
129    // 异步方法promise方式
130    try{
131        usageStatistics.queryCurrentBundleEvents(0, 20000000000000).then( res => {
132            console.log('BUNDLE_ACTIVE queryCurrentBundleEvents promise success.');
133            for (let i = 0; i < res.length; i++) {
134                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents promise number : ' + (i + 1));
135                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents promise result ' + JSON.stringify(res[i]));
136            }
137        }).catch( err => {
138            console.log('BUNDLE_ACTIVE queryCurrentBundleEvents promise failed. code is: ' + err.code + ',message is: ' + err.message);
139        });
140    } catch (error) {
141        console.log('BUNDLE_ACTIVE queryCurrentBundleEvents throw error, code is: ' + error.code + ',message is: ' + error.message);
142    }
143
144    // 异步方法callback方式
145    try{
146        usageStatistics.queryCurrentBundleEvents(0, 20000000000000, (err, res) => {
147            if (err) {
148                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback failed. code is: ' + err.code + ',message is: ' + err.message);
149            } else {
150                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback success.');
151            for (let i = 0; i < res.length; i++) {
152                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback number : ' + (i + 1));
153                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback result ' + JSON.stringify(res[i]));
154            }
155            }
156        });
157    } catch (error) {
158        console.log('BUNDLE_ACTIVE queryCurrentBundleEvents throw error, code is: ' + error.code + ',message is: ' + error.message);
159    }
160    ```
161
1625. 通过指定时间段间隔(天、周、月、年)查询应用使用时长统计信息,需要配置ohos.permission.BUNDLE_ACTIVE_INFO权限。
163
164    ```js
165    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
166
167    // 异步方法promise方式
168    try{
169        usageStatistics.queryBundleStatsInfoByInterval(0, 0, 20000000000000).then( res => {
170            console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise success.');
171            for (let i = 0; i < res.length; i++) {
172                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise number : ' + (i + 1));
173                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise result ' + JSON.stringify(res[i]));
174            }
175        }).catch( err => {
176            console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise failed. code is: ' + err.code + ',message is: ' + err.message);
177        });
178    } catch (error) {
179        console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval throw error, code is: ' + error.code + ',message is: ' + error.message);
180    }
181
182    // 异步方法callback方式
183    try{
184        usageStatistics.queryBundleStatsInfoByInterval(0, 0, 20000000000000, (err, res) => {
185            if (err) {
186                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback failed. code is: ' + err.code + ',message is: ' + err.message);
187            } else {
188                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback success.');
189            for (let i = 0; i < res.length; i++) {
190                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback number : ' + (i + 1));
191                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback result ' + JSON.stringify(res[i]));
192            }
193            }
194        });
195    } catch (error) {
196        console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval throw error, code is: ' + error.code + ',message is: ' + error.message);
197    }
198    ```
199
2006. 查询当前应用的使用优先级群组,不需要配置权限。
201
202    ```js
203    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
204
205    // promise方式
206    try{
207        usageStatistics.queryAppGroup().then( res => {
208            console.log('BUNDLE_ACTIVE queryAppGroup promise succeeded. result: ' + JSON.stringify(res));
209        }).catch( err => {
210            console.log('BUNDLE_ACTIVE queryAppGroup promise failed. code is: ' + err.code + ',message is: ' + err.message);
211        });
212    } catch (error) {
213        console.log('BUNDLE_ACTIVE queryAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
214    }
215
216    // callback方式
217    try{
218        usageStatistics.queryAppGroup((err, res) => {
219            if(err) {
220                console.log('BUNDLE_ACTIVE queryAppGroup callback failed. code is: ' + err.code + ',message is: ' + err.message);
221            } else {
222                console.log('BUNDLE_ACTIVE queryAppGroup callback succeeded. result: ' + JSON.stringify(res));
223            }
224        });
225    } catch (error) {
226        console.log('BUNDLE_ACTIVE queryAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
227    }
228    ```
229
2307. 判断指定Bundle Name的应用当前是否是空闲状态,需要配置ohos.permission.BUNDLE_ACTIVE_INFO权限。
231
232    ```js
233    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
234
235    // 异步方法promise方式
236    try{
237        usageStatistics.isIdleState("com.ohos.camera").then( res => {
238            console.log('BUNDLE_ACTIVE isIdleState promise succeeded, result: ' + JSON.stringify(res));
239        }).catch( err => {
240            console.log('BUNDLE_ACTIVE isIdleState promise failed. code is: ' + err.code + ',message is: ' + err.message);
241        });
242    } catch (error) {
243        console.log('BUNDLE_ACTIVE isIdleState throw error, code is: ' + error.code + ',message is: ' + error.message);
244    }
245
246    // 异步方法callback方式
247    try{
248        usageStatistics.isIdleState("com.ohos.camera", (err, res) => {
249            if (err) {
250                console.log('BUNDLE_ACTIVE isIdleState callback failed. code is: ' + err.code + ',message is: ' + err.message);
251            } else {
252                console.log('BUNDLE_ACTIVE isIdleState callback succeeded, result: ' + JSON.stringify(res));
253            }
254        });
255    } catch(error) {
256        console.log('BUNDLE_ACTIVE isIdleState throw error, code is: ' + error.code + ',message is: ' + error.message);
257    }
258    ```
259
2608. 查询FA使用记录。返回数量最大不超过maxNum设置的值,若不传入maxNum参数,则默认maxNum为1000, 需要配置ohos.permission.BUNDLE_ACTIVE_INFO权限。
261
262    ```js
263    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
264
265    // 异步方法promise方式
266    try{
267        usageStatistics.queryModuleUsageRecords(1000).then( res => {
268            console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise succeeded');
269            for (let i = 0; i < res.length; i++) {
270                console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise number : ' + (i + 1));
271                console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise result ' + JSON.stringify(res[i]));
272            }
273        }).catch( err=> {
274            console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise failed. code is: ' + err.code + ',message is: ' + err.message);
275        });
276    } catch (error) {
277        console.log('BUNDLE_ACTIVE queryModuleUsageRecords throw error, code is: ' + error.code + ',message is: ' + error.message);
278    }
279
280    // 无maxNum参数异步方法promise方式
281    try{
282        usageStatistics.queryModuleUsageRecords().then( res => {
283            console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise succeeded');
284            for (let i = 0; i < res.length; i++) {
285                console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise number : ' + (i + 1));
286                console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise result ' + JSON.stringify(res[i]));
287            }
288        }).catch( err=> {
289            console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise failed. code is: ' + err.code + ',message is: ' + err.message);
290        });
291    } catch (error) {
292        console.log('BUNDLE_ACTIVE queryModuleUsageRecords throw error, code is: ' + error.code + ',message is: ' + error.message);
293    }
294
295    // 异步方法callback方式
296    try{
297        usageStatistics.queryModuleUsageRecords(1000, (err, res) => {
298            if(err) {
299                console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback failed. code is: ' + err.code + ',message is: ' + err.message);
300            } else {
301                console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback succeeded.');
302                for (let i = 0; i < res.length; i++) {
303                console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback number : ' + (i + 1));
304                console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback result ' + JSON.stringify(res[i]));
305                }
306            }
307        });
308    } catch (error) {
309        console.log('BUNDLE_ACTIVE queryModuleUsageRecords throw error, code is: ' + error.code + ',message is: ' + error.message);
310    }
311
312    // 无maxNum参数异步方法callback方式
313    try{
314        usageStatistics.queryModuleUsageRecords((err, res) => {
315        if(err) {
316            console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback failed. code is: ' + err.code + ',message is: ' + err.message);
317        } else {
318            console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback succeeded.');
319            for (let i = 0; i < res.length; i++) {
320            console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback number : ' + (i + 1));
321            console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback result ' + JSON.stringify(res[i]));
322            }
323        }
324        });
325    } catch (error) {
326        console.log('BUNDLE_ACTIVE queryModuleUsageRecords throw error, code is: ' + error.code + ',message is: ' + error.message);
327    }
328    ```
329
3309. 通过指定起始和结束时间查询所有应用的通知次数,需要配置ohos.permission.BUNDLE_ACTIVE_INFO权限。
331
332    ```js
333    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
334
335    // 异步方法promise方式
336    try{
337        usageStatistics.queryNotificationEventStats(0, 20000000000000).then( res => {
338            console.log('BUNDLE_ACTIVE queryNotificationEventStats promise success.');
339            console.log('BUNDLE_ACTIVE queryNotificationEventStats promise result ' + JSON.stringify(res));
340        }).catch( err=> {
341            console.log('BUNDLE_ACTIVE queryNotificationEventStats promise failed. code is: ' + err.code + ',message is: ' + err.message);
342        });
343    } catch (error) {
344        console.log('BUNDLE_ACTIVE queryNotificationEventStats throw error, code is: ' + error.code + ',message is: ' + error.message);
345    }
346
347    // 异步方法callback方式
348    try{
349        usageStatistics.queryNotificationEventStats(0, 20000000000000, (err, res) => {
350            if(err) {
351                console.log('BUNDLE_ACTIVE queryNotificationEventStats callback failed. code is: ' + err.code + ',message is: ' + err.message);
352            } else {
353                console.log('BUNDLE_ACTIVE queryNotificationEventStats callback success.');
354                console.log('BUNDLE_ACTIVE queryNotificationEventStats callback result ' + JSON.stringify(res));
355            }
356        });
357    } catch (error) {
358        console.log('BUNDLE_ACTIVE queryNotificationEventStats throw error, code is: ' + error.code + ',message is: ' + error.message);
359    }
360    ```
361
36210. 通过指定起始和结束时间查询系统事件(休眠、唤醒、解锁、锁屏)统计信息,需要配置ohos.permission.BUNDLE_ACTIVE_INFO权限。
363
364    ```js
365    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
366
367    // 异步方法promise方式
368    try{
369        usageStatistics.queryDeviceEventStats(0, 20000000000000).then( res => {
370            console.log('BUNDLE_ACTIVE queryDeviceEventStates promise success.');
371            console.log('BUNDLE_ACTIVE queryDeviceEventStates promise result ' + JSON.stringify(res));
372        }).catch( err=> {
373            console.log('BUNDLE_ACTIVE queryDeviceEventStats promise failed. code is: ' + err.code + ',message is: ' + err.message);
374        });
375    } catch (error) {
376        console.log('BUNDLE_ACTIVE queryDeviceEventStats throw error, code is: ' + error.code + ',message is: ' + error.message);
377    }
378
379    // 异步方法callback方式
380    try{
381        usageStatistics.queryDeviceEventStats(0, 20000000000000, (err, res) => {
382            if(err) {
383                console.log('BUNDLE_ACTIVE queryDeviceEventStats callback failed. code is: ' + err.code + ',message is: ' + err.message);
384            } else {
385                console.log('BUNDLE_ACTIVE queryDeviceEventStats callback success.');
386                console.log('BUNDLE_ACTIVE queryDeviceEventStats callback result ' + JSON.stringify(res));
387            }
388        });
389    } catch (error) {
390        console.log('BUNDLE_ACTIVE queryDeviceEventStats throw error, code is: ' + error.code + ',message is: ' + error.message);
391    }
392    ```
393
39411. 查询指定bundleName的应用的使用优先级群组,返回查询的优先级分组结果,需要配置ohos.permission.BUNDLE_ACTIVE_INFO权限。
395
396     ```js
397     import usageStatistics from '@ohos.resourceschedule.usageStatistics'
398
399    // 有bundleName异步promise方式
400    let bundleName = "com.ohos.camera";
401    try{
402        usageStatistics.queryAppGroup(bundleName).then( res => {
403            console.log('BUNDLE_ACTIVE queryAppGroup promise succeeded. result: ' + JSON.stringify(res));
404        }).catch( err => {
405            console.log('BUNDLE_ACTIVE queryAppGroup promise failed. code is: ' + err.code + ',message is: ' + err.message);
406        });
407    } catch (error) {
408        console.log('BUNDLE_ACTIVE queryAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
409    }
410
411     // 有bundleName异步方法callback方式
412    let bundleName = "com.ohos.camera";
413    try{
414        usageStatistics.queryAppGroup(bundleName, (err, res) => {
415            if(err) {
416                console.log('BUNDLE_ACTIVE queryAppGroup callback failed. code is: ' + err.code + ',message is: ' + err.message);
417            } else {
418                console.log('BUNDLE_ACTIVE queryAppGroup callback succeeded. result: ' + JSON.stringify(res));
419            }
420        });
421    } catch (error) {
422        console.log('BUNDLE_ACTIVE queryAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
423    }
424     ```
425
42612. 给指定bundleName的应用的优先级分组设置成newGroup。 需要配置ohos.permission.BUNDLE_ACTIVE_INFO权限。
427
428    ```javascript
429    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
430
431    // 异步方法promise
432    let bundleName = "com.example.deviceUsageStatistics";
433    let newGroup = bundleState.GroupType.ACTIVE_GROUP_DAILY;
434
435    try{
436        usageStatistics.setAppGroup(bundleName, newGroup).then( () => {
437            console.log('BUNDLE_ACTIVE setAppGroup promise succeeded.');
438        }).catch( err => {
439            console.log('BUNDLE_ACTIVE setAppGroup promise failed. code is: ' + err.code + ',message is: ' + err.message);
440        });
441    } catch (error) {
442        console.log('BUNDLE_ACTIVE setAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
443    }
444
445    // 异步方法callback
446    let bundleName = "com.example.deviceUsageStatistics";
447    let newGroup = bundleState.GroupType.ACTIVE_GROUP_DAILY;
448
449    try{
450        usageStatistics.setAppGroup(bundleName, newGroup, (err) => {
451            if(err) {
452                console.log('BUNDLE_ACTIVE setAppGroup callback failed. code is: ' + err.code + ',message is: ' + err.message);
453            } else {
454                console.log('BUNDLE_ACTIVE setAppGroup callback succeeded.');
455            }
456        });
457    } catch (error) {
458        console.log('BUNDLE_ACTIVE setAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
459    }
460    ```
461
46213. 注册应用分组变化监听回调,返回注册是否成功,当应用分组发生变化时,会给所有已注册的监听者返回回调信息, 需要配置ohos.permission.BUNDLE_ACTIVE_INFO权限
463
464    ```javascript
465    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
466
467    // 异步方法promise形式
468    let onBundleGroupChanged = (res) =>{
469        console.log('BUNDLE_ACTIVE registerAppGroupCallBack RegisterGroupCallBack callback success.');
470        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appOldGroup is : ' + res.appOldGroup);
471        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appNewGroup is : ' + res.appNewGroup);
472        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result changeReason is : ' + res.changeReason);
473        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result userId is : ' + res.userId);
474        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result bundleName is : ' + res.bundleName);
475    };
476    try{
477        usageStatistics.registerAppGroupCallBack(onBundleGroupChanged).then( () => {
478            console.log('BUNDLE_ACTIVE registerAppGroupCallBack promise succeeded.');
479        }).catch( err => {
480            console.log('BUNDLE_ACTIVE registerAppGroupCallBack promise failed. code is: ' + err.code + ',message is: ' + err.message);
481        });
482    } catch (error) {
483        console.log('BUNDLE_ACTIVE registerAppGroupCallBack throw error, code is: ' + error.code + ',message is: ' + error.message);
484    }
485
486    // 异步方法callback形式
487    let onBundleGroupChanged = (err, res) =>{
488        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack callback success.');
489        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appOldGroup is : ' + res.appOldGroup);
490        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appNewGroup is : ' + res.appNewGroup);
491        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result changeReason is : ' + res.changeReason);
492        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result userId is : ' + res.userId);
493        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result bundleName is : ' + res.bundleName);
494    };
495    try{
496        usageStatistics.registerAppGroupCallBack(onBundleGroupChanged, err => {
497        if(err) {
498            console.log('BUNDLE_ACTIVE registerAppGroupCallBack callback failed. code is: ' + err.code + ',message is: ' + err.message);
499        } else {
500            console.log('BUNDLE_ACTIVE registerAppGroupCallBack callback success.');
501        }
502        });
503    } catch (error) {
504        console.log('BUNDLE_ACTIVE registerAppGroupCallBack throw error, code is: ' + error.code + ',message is: ' + error.message);
505    }
506    ```
507
50814. 解除应用分组监听回调, 需要配置ohos.permission.BUNDLE_ACTIVE_INFO权限。
509
510    ```javascript
511    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
512
513    // promise
514    try{
515        usageStatistics.unregisterAppGroupCallBack().then( () => {
516            console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack promise succeeded.');
517        }).catch( err => {
518            console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack promise failed. code is: ' + err.code + ',message is: ' + err.message);
519        });
520    } catch (error) {
521        console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack throw error, code is: ' + error.code + ',message is: ' + error.message);
522    }
523
524    // callback
525    try{
526        usageStatistics.unregisterAppGroupCallBack(err => {
527            if(err) {
528                console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack callback failed. code is: ' + err.code + ',message is: ' + err.message);
529            } else {
530                console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack callback success.');
531            }
532        });
533    } catch (error) {
534        console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack throw error, code is: ' + error.code + ',message is: ' + error.message);
535    }
536    ```
537## 相关实例
538
539针对设备使用信息统计,有以下相关实例可供参考:
540
541- [`DeviceUsageStatistics`:设备使用信息统计(ArkTS)(API8)(Full SDK)](https://gitee.com/openharmony/applications_app_samples/tree/OpenHarmony-3.2-Release/code/BasicFeature/DeviceUsageStatistics/DeviceUsageStatistics)
542
543