• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Device Usage Statistics Development (API Version 9)
2
3## When to Use
4
5With device usage statistics APIs, you can have a better understanding of the application, notification, and system usage. For example, in application usage statistics, you can query the application usage, event log, and application group.
6The application records (usage history statistics and event records) cached by components are updated to the database for persistent storage within 30 minutes after an event is reported.
7
8## Available APIs
9Import the **stats** package to implement registration:
10```js
11import usageStatistics from '@ohos.resourceschedule.usageStatistics';
12```
13
14**Table 1** Major APIs for device usage statistics
15
16| API| Description|
17| -------- | -------- |
18| function queryBundleEvents(begin: number, end: number, callback: AsyncCallback<Array<BundleEvents>>): void | Queries events of all applications based on the specified start time and end time.|
19| function queryBundleStatsInfos(begin: number, end: number, callback: AsyncCallback<BundleStatsMap>): void | Queries the application usage duration statistics based on the specified start time and end time.|
20| function queryCurrentBundleEvents(begin: number, end: number, callback: AsyncCallback<Array<BundleEvents>>): void | Queries events of this application based on the specified start time and end time.|
21| function queryBundleStatsInfoByInterval(byInterval: IntervalType, begin: number, end: number, callback: AsyncCallback<Array<BundleStatsInfo>>): void | Queries the application usage duration statistics in the specified time frame at the specified interval (daily, weekly, monthly, or annually).|
22| function queryAppGroup(callback: AsyncCallback<number>): void | Queries the priority group of this application. This API uses an asynchronous callback to return the result.|
23| function queryAppGroup(): Promise<number>; | Queries the priority group of this application. This API uses a promise to return the result.|
24| function queryAppGroup(bundleName : string, callback: AsyncCallback<number>): void | Queries the priority group of the application specified by **bundleName**. This API uses an asynchronous callback to return the result.|
25| function queryAppGroup(bundleName : string): Promise<number>; | Queries the priority group of the application specified by **bundleName**. If **bundleName** is not specified, the priority group of the current application is queried. This API uses a promise to return the result.|
26| function isIdleState(bundleName: string, callback: AsyncCallback<boolean>): void | Checks whether the application specified by **bundleName** is in the idle state. |
27| function queryModuleUsageRecords(callback: AsyncCallback<HapModuleInfo>): void | Obtains a maximum of 1000 FA usage records.|
28| function queryModuleUsageRecords(maxNum: number, callback: AsyncCallback<HapModuleInfo>): void | Obtains the number of FA usage records specified by **maxNum**, which cannot exceed 1000.|
29| function queryNotificationEventStats(begin: number, end: number, callback: AsyncCallback<Array<DeviceEventStats>>): void | Queries the number of notifications from all applications based on the specified start time and end time.|
30| function queryDeviceEventStats(begin: number, end: number, callback: AsyncCallback<Array<DeviceEventStats>>): void | Queries statistics about system events (hibernation, wakeup, unlocking, and screen locking) that occur between the specified start time and end time.|
31| function setAppGroup(bundleName : string, newGroup: GroupType, callback: AsyncCallback>boolean>): void | Sets the group for the application specified by **bundleName**. This API uses an asynchronous callback to return the result.|
32| function setAppGroup(bundleName : string, newGroup : GroupType): Promise>boolean>; | Sets the group for the application specified by **bundleName**. This API uses a promise to return the result.|
33| function registerAppGroupCallBack(groupCallback: Callback>AppGroupCallbackInfo>, callback: AsyncCallback>boolean>): void | Registers a callback for application group changes. When an application group of the user changes, the change is returned to all applications that have registered the callback. This API uses an asynchronous callback to return the result.|
34| function registerAppGroupCallBack(groupCallback: Callback>AppGroupCallbackInfo>): Promise>boolean>; | Registers a callback for application group changes. When an application group of the user changes, the change is returned to all applications that have registered the callback. This API uses a promise to return the result.|
35| function unregisterAppGroupCallBack(callback: AsyncCallback>boolean>): void | Deregisters the callback for application group changes. This API uses an asynchronous callback to return the result.|
36| function unregisterAppGroupCallBack(): Promise>boolean>; | Deregisters the callback for application group changes. This API uses a promise to return the result.|
37
38## How to Develop
39
401. Before obtaining the device usage statistics, check whether the **ohos.permission.BUNDLE_ACTIVE_INFO** permission is configured. For details about how to configure a permission, see [Declaring Permissions](../security/accesstoken-guidelines.md).
41
422. Query events of all applications based on the specified start time and end time. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured.
43
44    ```js
45    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
46
47    // Promise mode
48    try{
49        usageStatistics.queryBundleEvents(0, 20000000000000).then( res => {
50            console.log('BUNDLE_ACTIVE queryBundleEvents promise success.');
51            for (let i = 0; i < res.length; i++) {
52                console.log('BUNDLE_ACTIVE queryBundleEvents promise number : ' + (i + 1));
53                console.log('BUNDLE_ACTIVE queryBundleEvents promise result ' + JSON.stringify(res[i]));
54            }
55        }).catch( err => {
56            console.log('BUNDLE_ACTIVE queryBundleEvents promise failed. code is: ' + err.code + ',message is: ' + err.message);
57        });
58    } catch (error) {
59        console.log('BUNDLE_ACTIVE queryBundleEvents throw error, code is: ' + error.code + ',message is: ' + error.message);
60    }
61
62    // Asynchronous callback mode
63    try{
64        usageStatistics.queryBundleEvents(0, 20000000000000, (err, res) => {
65            if (err) {
66                console.log('BUNDLE_ACTIVE queryBundleEvents callback failed. code is: ' + err.code + ',message is: ' + err.message);
67            } else {
68                console.log('BUNDLE_ACTIVE queryBundleEvents callback success.');
69            for (let i = 0; i < res.length; i++) {
70                console.log('BUNDLE_ACTIVE queryBundleEvents callback number : ' + (i + 1));
71                console.log('BUNDLE_ACTIVE queryBundleEvents callback result ' + JSON.stringify(res[i]));
72            }
73            }
74        });
75    } catch (error) {
76        console.log('BUNDLE_ACTIVE queryBundleEvents throw error, code is: ' + error.code + ',message is: ' + error.message);
77    }
78    ```
79
803. Query the application usage duration statistics based on the specified start time and end time. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured.
81
82    ```js
83    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
84
85    // Promise mode
86    try{
87        usageStatistics.queryBundleStatsInfos(0, 20000000000000).then( res => {
88            console.log('BUNDLE_ACTIVE queryBundleStatsInfos promise success.');
89            let i = 1;
90            for(let key in res){
91                console.log('BUNDLE_ACTIVE queryBundleStatsInfos promise number : ' + i);
92                console.log('BUNDLE_ACTIVE queryBundleStatsInfos promise result ' + JSON.stringify(res[key]));
93                i++;
94            }
95        }).catch( err => {
96            console.log('BUNDLE_ACTIVE queryBundleStatsInfos promise failed. code is: ' + err.code + ',message is: ' + err.message);
97        });
98    } catch (error) {
99        console.log('BUNDLE_ACTIVE queryBundleStatsInfos throw error, code is: ' + error.code + ',message is: ' + error.message);
100    }
101
102    // Asynchronous callback mode
103    try{
104        usageStatistics.queryBundleStatsInfos(0, 20000000000000, (err, res) => {
105            if (err) {
106                console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback failed. code is: ' + err.code + ',message is: ' + err.message);
107            } else {
108                console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback success.');
109            let i = 1;
110            for(let key in res){
111                console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback number : ' + i);
112                console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback result ' + JSON.stringify(res[key]));
113                i++;
114            }
115            }
116        });
117    } catch (error) {
118        console.log('BUNDLE_ACTIVE queryBundleStatsInfos throw error, code is: ' + error.code + ',message is: ' + error.message);
119    }
120    ```
121
1224. Query events of this application based on the specified start time and end time. This requires no permission to be configured.
123
124    ```js
125    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
126
127    // Promise mode
128    try{
129        usageStatistics.queryCurrentBundleEvents(0, 20000000000000).then( res => {
130            console.log('BUNDLE_ACTIVE queryCurrentBundleEvents promise success.');
131            for (let i = 0; i < res.length; i++) {
132                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents promise number : ' + (i + 1));
133                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents promise result ' + JSON.stringify(res[i]));
134            }
135        }).catch( err => {
136            console.log('BUNDLE_ACTIVE queryCurrentBundleEvents promise failed. code is: ' + err.code + ',message is: ' + err.message);
137        });
138    } catch (error) {
139        console.log('BUNDLE_ACTIVE queryCurrentBundleEvents throw error, code is: ' + error.code + ',message is: ' + error.message);
140    }
141
142    // Asynchronous callback mode
143    try{
144        usageStatistics.queryCurrentBundleEvents(0, 20000000000000, (err, res) => {
145            if (err) {
146                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback failed. code is: ' + err.code + ',message is: ' + err.message);
147            } else {
148                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback success.');
149            for (let i = 0; i < res.length; i++) {
150                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback number : ' + (i + 1));
151                console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback result ' + JSON.stringify(res[i]));
152            }
153            }
154        });
155    } catch (error) {
156        console.log('BUNDLE_ACTIVE queryCurrentBundleEvents throw error, code is: ' + error.code + ',message is: ' + error.message);
157    }
158    ```
159
1605. Query the application usage duration statistics in the specified time frame at the specified interval (daily, weekly, monthly, or annually). This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured.
161
162    ```js
163    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
164
165    // Promise mode
166    try{
167        usageStatistics.queryBundleStatsInfoByInterval(0, 0, 20000000000000).then( res => {
168            console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise success.');
169            for (let i = 0; i < res.length; i++) {
170                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise number : ' + (i + 1));
171                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise result ' + JSON.stringify(res[i]));
172            }
173        }).catch( err => {
174            console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise failed. code is: ' + err.code + ',message is: ' + err.message);
175        });
176    } catch (error) {
177        console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval throw error, code is: ' + error.code + ',message is: ' + error.message);
178    }
179
180    // Asynchronous callback mode
181    try{
182        usageStatistics.queryBundleStatsInfoByInterval(0, 0, 20000000000000, (err, res) => {
183            if (err) {
184                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback failed. code is: ' + err.code + ',message is: ' + err.message);
185            } else {
186                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback success.');
187            for (let i = 0; i < res.length; i++) {
188                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback number : ' + (i + 1));
189                console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback result ' + JSON.stringify(res[i]));
190            }
191            }
192        });
193    } catch (error) {
194        console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval throw error, code is: ' + error.code + ',message is: ' + error.message);
195    }
196    ```
197
1986. Query the priority group of the current application. This requires no permission to be configured.
199
200    ```js
201    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
202
203    // Promise mode
204    try{
205        usageStatistics.queryAppGroup().then( res => {
206            console.log('BUNDLE_ACTIVE queryAppGroup promise succeeded. result: ' + JSON.stringify(res));
207        }).catch( err => {
208            console.log('BUNDLE_ACTIVE queryAppGroup promise failed. code is: ' + err.code + ',message is: ' + err.message);
209        });
210    } catch (error) {
211        console.log('BUNDLE_ACTIVE queryAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
212    }
213
214    // Callback mode
215    try{
216        usageStatistics.queryAppGroup((err, res) => {
217            if(err) {
218                console.log('BUNDLE_ACTIVE queryAppGroup callback failed. code is: ' + err.code + ',message is: ' + err.message);
219            } else {
220                console.log('BUNDLE_ACTIVE queryAppGroup callback succeeded. result: ' + JSON.stringify(res));
221            }
222        });
223    } catch (error) {
224        console.log('BUNDLE_ACTIVE queryAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
225    }
226    ```
227
2287. Check whether the application specified by **bundleName** is in the idle state. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured.
229
230    ```js
231    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
232
233    // Promise mode
234    try{
235        usageStatistics.isIdleState("com.ohos.camera").then( res => {
236            console.log('BUNDLE_ACTIVE isIdleState promise succeeded, result: ' + JSON.stringify(res));
237        }).catch( err => {
238            console.log('BUNDLE_ACTIVE isIdleState promise failed. code is: ' + err.code + ',message is: ' + err.message);
239        });
240    } catch (error) {
241        console.log('BUNDLE_ACTIVE isIdleState throw error, code is: ' + error.code + ',message is: ' + error.message);
242    }
243
244    // Asynchronous callback mode
245    try{
246        usageStatistics.isIdleState("com.ohos.camera", (err, res) => {
247            if (err) {
248                console.log('BUNDLE_ACTIVE isIdleState callback failed. code is: ' + err.code + ',message is: ' + err.message);
249            } else {
250                console.log('BUNDLE_ACTIVE isIdleState callback succeeded, result: ' + JSON.stringify(res));
251            }
252        });
253    } catch(error) {
254        console.log('BUNDLE_ACTIVE isIdleState throw error, code is: ' + error.code + ',message is: ' + error.message);
255    }
256    ```
257
2588. Obtain the number of FA usage records specified by **maxNum**. If **maxNum** is not specified, the default value **1000** is used. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured.
259
260    ```js
261    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
262
263    // Promise mode
264    try{
265        usageStatistics.queryModuleUsageRecords(1000).then( res => {
266            console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise succeeded');
267            for (let i = 0; i < res.length; i++) {
268                console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise number : ' + (i + 1));
269                console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise result ' + JSON.stringify(res[i]));
270            }
271        }).catch( err=> {
272            console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise failed. code is: ' + err.code + ',message is: ' + err.message);
273        });
274    } catch (error) {
275        console.log('BUNDLE_ACTIVE queryModuleUsageRecords throw error, code is: ' + error.code + ',message is: ' + error.message);
276    }
277
278    // Promise mode when maxNum is not specified
279    try{
280        usageStatistics.queryModuleUsageRecords().then( res => {
281            console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise succeeded');
282            for (let i = 0; i < res.length; i++) {
283                console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise number : ' + (i + 1));
284                console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise result ' + JSON.stringify(res[i]));
285            }
286        }).catch( err=> {
287            console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise failed. code is: ' + err.code + ',message is: ' + err.message);
288        });
289    } catch (error) {
290        console.log('BUNDLE_ACTIVE queryModuleUsageRecords throw error, code is: ' + error.code + ',message is: ' + error.message);
291    }
292
293    // Asynchronous callback mode
294    try{
295        usageStatistics.queryModuleUsageRecords(1000, (err, res) => {
296            if(err) {
297                console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback failed. code is: ' + err.code + ',message is: ' + err.message);
298            } else {
299                console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback succeeded.');
300                for (let i = 0; i < res.length; i++) {
301                console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback number : ' + (i + 1));
302                console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback result ' + JSON.stringify(res[i]));
303                }
304            }
305        });
306    } catch (error) {
307        console.log('BUNDLE_ACTIVE queryModuleUsageRecords throw error, code is: ' + error.code + ',message is: ' + error.message);
308    }
309
310    // Asynchronous callback mode when maxNum is not specified
311    try{
312        usageStatistics.queryModuleUsageRecords((err, res) => {
313        if(err) {
314            console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback failed. code is: ' + err.code + ',message is: ' + err.message);
315        } else {
316            console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback succeeded.');
317            for (let i = 0; i < res.length; i++) {
318            console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback number : ' + (i + 1));
319            console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback result ' + JSON.stringify(res[i]));
320            }
321        }
322        });
323    } catch (error) {
324        console.log('BUNDLE_ACTIVE queryModuleUsageRecords throw error, code is: ' + error.code + ',message is: ' + error.message);
325    }
326    ```
327
3289. Query the number of notifications from all applications based on the specified start time and end time. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured.
329
330    ```js
331    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
332
333    // Promise mode
334    try{
335        usageStatistics.queryNotificationEventStats(0, 20000000000000).then( res => {
336            console.log('BUNDLE_ACTIVE queryNotificationEventStats promise success.');
337            console.log('BUNDLE_ACTIVE queryNotificationEventStats promise result ' + JSON.stringify(res));
338        }).catch( err=> {
339            console.log('BUNDLE_ACTIVE queryNotificationEventStats promise failed. code is: ' + err.code + ',message is: ' + err.message);
340        });
341    } catch (error) {
342        console.log('BUNDLE_ACTIVE queryNotificationEventStats throw error, code is: ' + error.code + ',message is: ' + error.message);
343    }
344
345    // Asynchronous callback mode
346    try{
347        usageStatistics.queryNotificationEventStats(0, 20000000000000, (err, res) => {
348            if(err) {
349                console.log('BUNDLE_ACTIVE queryNotificationEventStats callback failed. code is: ' + err.code + ',message is: ' + err.message);
350            } else {
351                console.log('BUNDLE_ACTIVE queryNotificationEventStats callback success.');
352                console.log('BUNDLE_ACTIVE queryNotificationEventStats callback result ' + JSON.stringify(res));
353            }
354        });
355    } catch (error) {
356        console.log('BUNDLE_ACTIVE queryNotificationEventStats throw error, code is: ' + error.code + ',message is: ' + error.message);
357    }
358    ```
359
36010. Query statistics about system events (hibernation, wakeup, unlocking, and screen locking) that occur between the specified start time and end time. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured.
361
362    ```js
363    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
364
365    // Promise mode
366    try{
367        usageStatistics.queryDeviceEventStats(0, 20000000000000).then( res => {
368            console.log('BUNDLE_ACTIVE queryDeviceEventStates promise success.');
369            console.log('BUNDLE_ACTIVE queryDeviceEventStates promise result ' + JSON.stringify(res));
370        }).catch( err=> {
371            console.log('BUNDLE_ACTIVE queryDeviceEventStats promise failed. code is: ' + err.code + ',message is: ' + err.message);
372        });
373    } catch (error) {
374        console.log('BUNDLE_ACTIVE queryDeviceEventStats throw error, code is: ' + error.code + ',message is: ' + error.message);
375    }
376
377    // Asynchronous callback mode
378    try{
379        usageStatistics.queryDeviceEventStats(0, 20000000000000, (err, res) => {
380            if(err) {
381                console.log('BUNDLE_ACTIVE queryDeviceEventStats callback failed. code is: ' + err.code + ',message is: ' + err.message);
382            } else {
383                console.log('BUNDLE_ACTIVE queryDeviceEventStats callback success.');
384                console.log('BUNDLE_ACTIVE queryDeviceEventStats callback result ' + JSON.stringify(res));
385            }
386        });
387    } catch (error) {
388        console.log('BUNDLE_ACTIVE queryDeviceEventStats throw error, code is: ' + error.code + ',message is: ' + error.message);
389    }
390    ```
391
39211. Query the priority group of the application specified by **bundleName**. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured.
393
394     ```js
395     import usageStatistics from '@ohos.resourceschedule.usageStatistics'
396
397    // Promise mode when bundleName is specified
398    let bundleName = "com.ohos.camera";
399    try{
400        usageStatistics.queryAppGroup(bundleName).then( res => {
401            console.log('BUNDLE_ACTIVE queryAppGroup promise succeeded. result: ' + JSON.stringify(res));
402        }).catch( err => {
403            console.log('BUNDLE_ACTIVE queryAppGroup promise failed. code is: ' + err.code + ',message is: ' + err.message);
404        });
405    } catch (error) {
406        console.log('BUNDLE_ACTIVE queryAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
407    }
408
409     // Asynchronous callback mode when bundleName is specified
410    let bundleName = "com.ohos.camera";
411    try{
412        usageStatistics.queryAppGroup(bundleName, (err, res) => {
413            if(err) {
414                console.log('BUNDLE_ACTIVE queryAppGroup callback failed. code is: ' + err.code + ',message is: ' + err.message);
415            } else {
416                console.log('BUNDLE_ACTIVE queryAppGroup callback succeeded. result: ' + JSON.stringify(res));
417            }
418        });
419    } catch (error) {
420        console.log('BUNDLE_ACTIVE queryAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
421    }
422     ```
423
42412. Set the priority group of for application specified by **bundleName**. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured.
425
426    ```javascript
427    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
428
429    // Promise mode
430    let bundleName = "com.example.deviceUsageStatistics";
431    let newGroup = bundleState.GroupType.ACTIVE_GROUP_DAILY;
432
433    try{
434        usageStatistics.setAppGroup(bundleName, newGroup).then( () => {
435            console.log('BUNDLE_ACTIVE setAppGroup promise succeeded.');
436        }).catch( err => {
437            console.log('BUNDLE_ACTIVE setAppGroup promise failed. code is: ' + err.code + ',message is: ' + err.message);
438        });
439    } catch (error) {
440        console.log('BUNDLE_ACTIVE setAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
441    }
442
443    // Asynchronous callback mode
444    let bundleName = "com.example.deviceUsageStatistics";
445    let newGroup = bundleState.GroupType.ACTIVE_GROUP_DAILY;
446
447    try{
448        usageStatistics.setAppGroup(bundleName, newGroup, (err) => {
449            if(err) {
450                console.log('BUNDLE_ACTIVE setAppGroup callback failed. code is: ' + err.code + ',message is: ' + err.message);
451            } else {
452                console.log('BUNDLE_ACTIVE setAppGroup callback succeeded.');
453            }
454        });
455    } catch (error) {
456        console.log('BUNDLE_ACTIVE setAppGroup throw error, code is: ' + error.code + ',message is: ' + error.message);
457    }
458    ```
459
46013. Register a callback for application group changes. When an application group of the user changes, the change is returned to all applications that have registered the callback. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured.
461
462    ```javascript
463    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
464
465    // Promise mode
466    let onBundleGroupChanged = (res) =>{
467        console.log('BUNDLE_ACTIVE registerAppGroupCallBack RegisterGroupCallBack callback success.');
468        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appOldGroup is : ' + res.appOldGroup);
469        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appNewGroup is : ' + res.appNewGroup);
470        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result changeReason is : ' + res.changeReason);
471        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result userId is : ' + res.userId);
472        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result bundleName is : ' + res.bundleName);
473    };
474    try{
475        usageStatistics.registerAppGroupCallBack(onBundleGroupChanged).then( () => {
476            console.log('BUNDLE_ACTIVE registerAppGroupCallBack promise succeeded.');
477        }).catch( err => {
478            console.log('BUNDLE_ACTIVE registerAppGroupCallBack promise failed. code is: ' + err.code + ',message is: ' + err.message);
479        });
480    } catch (error) {
481        console.log('BUNDLE_ACTIVE registerAppGroupCallBack throw error, code is: ' + error.code + ',message is: ' + error.message);
482    }
483
484    // Asynchronous callback mode
485    let onBundleGroupChanged = (err, res) =>{
486        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack callback success.');
487        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appOldGroup is : ' + res.appOldGroup);
488        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appNewGroup is : ' + res.appNewGroup);
489        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result changeReason is : ' + res.changeReason);
490        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result userId is : ' + res.userId);
491        console.log('BUNDLE_ACTIVE registerAppGroupCallBack result bundleName is : ' + res.bundleName);
492    };
493    try{
494        usageStatistics.registerAppGroupCallBack(onBundleGroupChanged, err => {
495        if(err) {
496            console.log('BUNDLE_ACTIVE registerAppGroupCallBack callback failed. code is: ' + err.code + ',message is: ' + err.message);
497        } else {
498            console.log('BUNDLE_ACTIVE registerAppGroupCallBack callback success.');
499        }
500        });
501    } catch (error) {
502        console.log('BUNDLE_ACTIVE registerAppGroupCallBack throw error, code is: ' + error.code + ',message is: ' + error.message);
503    }
504    ```
505
50614. Deregister the callback for application group changes. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured.
507
508    ```javascript
509    import usageStatistics from '@ohos.resourceschedule.usageStatistics'
510
511    // promise
512    try{
513        usageStatistics.unregisterAppGroupCallBack().then( () => {
514            console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack promise succeeded.');
515        }).catch( err => {
516            console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack promise failed. code is: ' + err.code + ',message is: ' + err.message);
517        });
518    } catch (error) {
519        console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack throw error, code is: ' + error.code + ',message is: ' + error.message);
520    }
521
522    // callback
523    try{
524        usageStatistics.unregisterAppGroupCallBack(err => {
525            if(err) {
526                console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack callback failed. code is: ' + err.code + ',message is: ' + err.message);
527            } else {
528                console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack callback success.');
529            }
530        });
531    } catch (error) {
532        console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack throw error, code is: ' + error.code + ',message is: ' + error.message);
533    }
534    ```
535