1e41f4b71Sopenharmony_ci# Device Usage Statistics Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciDevice usage statistics include the usage of applications, notifications, and the system. For example, you can use the APIs for application usage statistics to obtain information about the application usage, event logs, and application groups. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThe application records (usage history statistics and event records) cached by components are flushed to the database for persistent storage within 30 minutes after an event is reported. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Available APIs 10e41f4b71Sopenharmony_ciBefore using the APIs, import the **usageStatistics** module: 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport { usageStatistics } from '@kit.BackgroundTasksKit' 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci**Table 1** Major APIs for device usage statistics 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci| API | Description | 18e41f4b71Sopenharmony_ci| -------- | -------- | 19e41f4b71Sopenharmony_ci| 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. | 20e41f4b71Sopenharmony_ci| function queryBundleStatsInfos(begin: number, end: number, callback: AsyncCallback<BundleStatsMap>): void | Queries the application usage duration based on the specified start time and end time. | 21e41f4b71Sopenharmony_ci| 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. | 22e41f4b71Sopenharmony_ci| function queryBundleStatsInfoByInterval(byInterval: IntervalType, begin: number, end: number, callback: AsyncCallback<Array<BundleStatsInfo>>): void | Queries the application usage duration in the specified time frame at the specified interval (daily, weekly, monthly, or annually). | 23e41f4b71Sopenharmony_ci| function queryAppGroup(callback: AsyncCallback<number>): void | Queries the priority group of this application. This API uses an asynchronous callback to return the result. | 24e41f4b71Sopenharmony_ci| function queryAppGroup(): Promise<number>; | Queries the priority group of this application. This API uses a promise to return the result. | 25e41f4b71Sopenharmony_ci|function queryAppGroupSync(): number; | Queries the priority group of this application. This API returns the result synchronously.| 26e41f4b71Sopenharmony_ci| 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. | 27e41f4b71Sopenharmony_ci| 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. | 28e41f4b71Sopenharmony_ci|function queryAppGroupSync(bundleName: string): 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 returns the result synchronously.| 29e41f4b71Sopenharmony_ci| function isIdleState(bundleName: string, callback: AsyncCallback<boolean>): void | Checks whether the application specified by **bundleName** is in the idle state. | 30e41f4b71Sopenharmony_ci|function isIdleStateSync(bundleName: string): boolean; | Checks whether the application specified by **bundleName** is in the idle state. This API returns the result synchronously. | 31e41f4b71Sopenharmony_ci| function queryModuleUsageRecords(callback: AsyncCallback<HapModuleInfo>): void | Obtains a maximum of 1000 FA usage records. | 32e41f4b71Sopenharmony_ci| function queryModuleUsageRecords(maxNum: number, callback: AsyncCallback<HapModuleInfo>): void | Obtains the number of FA usage records specified by **maxNum**, which cannot exceed 1000.| 33e41f4b71Sopenharmony_ci| 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. | 34e41f4b71Sopenharmony_ci| function queryDeviceEventStats(begin: number, end: number, callback: AsyncCallback<Array<DeviceEventStats>>): void | Queries system events (hibernation, wakeup, lock, and unlock) that occur between the specified start time and end time. | 35e41f4b71Sopenharmony_ci| function setAppGroup(bundleName : string, newGroup: GroupType, callback: AsyncCallback<void>): void | Sets the group for the application specified by **bundleName**. This API uses an asynchronous callback to return the result. | 36e41f4b71Sopenharmony_ci| function setAppGroup(bundleName : string, newGroup : GroupType): Promise<void>; | Sets the group for the application specified by **bundleName**. This API uses a promise to return the result. | 37e41f4b71Sopenharmony_ci| function registerAppGroupCallBack(groupCallback: Callback<AppGroupCallbackInfo>, callback: AsyncCallback<void>): 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. | 38e41f4b71Sopenharmony_ci| function registerAppGroupCallBack(groupCallback: Callback<AppGroupCallbackInfo>): Promise<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 a promise to return the result. | 39e41f4b71Sopenharmony_ci| function unregisterAppGroupCallBack(callback: AsyncCallback<void>): void | Deregisters the callback for application group changes. This API uses an asynchronous callback to return the result. | 40e41f4b71Sopenharmony_ci| function unregisterAppGroupCallBack(): Promise<void>; | Deregisters the callback for application group changes. This API uses a promise to return the result. | 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci## How to Develop 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci1. Before obtaining the device usage statistics, check that the application has the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci For details, see [Requesting Permissions for system_basic Applications](../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications). 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci2. Query events of all applications based on the specified start time and end time. The caller must have the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci ```ts 51e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci // Promise mode 54e41f4b71Sopenharmony_ci usageStatistics.queryBundleEvents(0, 20000000000000).then( (res : Array<usageStatistics.BundleEvents>) => { 55e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleEvents promise success.'); 56e41f4b71Sopenharmony_ci for (let i = 0; i < res.length; i++) { 57e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleEvents promise number : ' + (i + 1)); 58e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleEvents promise result ' + JSON.stringify(res[i])); 59e41f4b71Sopenharmony_ci } 60e41f4b71Sopenharmony_ci }).catch((err : BusinessError)=> { 61e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE queryBundleEvents promise failed. code is: ' + err.code + ',message is: ' + err.message); 62e41f4b71Sopenharmony_ci }); 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci // Asynchronous callback mode 65e41f4b71Sopenharmony_ci usageStatistics.queryBundleEvents(0, 20000000000000, (err : BusinessError, res : Array<usageStatistics.BundleEvents>) => { 66e41f4b71Sopenharmony_ci if (err) { 67e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleEvents callback failed. code is: ' + err.code + ',message is: ' + err.message); 68e41f4b71Sopenharmony_ci } else { 69e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleEvents callback success.'); 70e41f4b71Sopenharmony_ci for (let i = 0; i < res.length; i++) { 71e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleEvents callback number : ' + (i + 1)); 72e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleEvents callback result ' + JSON.stringify(res[i])); 73e41f4b71Sopenharmony_ci } 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci }); 76e41f4b71Sopenharmony_ci ``` 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci3. Query the application usage duration based on the specified start time and end time. The caller must have the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci ```ts 81e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci // Promise mode 84e41f4b71Sopenharmony_ci usageStatistics.queryBundleStatsInfos(0, 20000000000000).then( (res : usageStatistics.BundleStatsMap) => { 85e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfos promise success.'); 86e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback result ' + JSON.stringify(res)); 87e41f4b71Sopenharmony_ci }).catch( (err : BusinessError) => { 88e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE queryBundleStatsInfos promise failed. code is: ' + err.code + ',message is: ' + err.message); 89e41f4b71Sopenharmony_ci }); 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci // Asynchronous callback mode 92e41f4b71Sopenharmony_ci usageStatistics.queryBundleStatsInfos(0, 20000000000000, (err : BusinessError, res : usageStatistics.BundleStatsMap) => { 93e41f4b71Sopenharmony_ci if (err) { 94e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback failed. code is: ' + err.code + ',message is: ' + err.message); 95e41f4b71Sopenharmony_ci } else { 96e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback success.'); 97e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfos callback result ' + JSON.stringify(res)); 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci }); 100e41f4b71Sopenharmony_ci ``` 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci4. Query events of this application based on the specified start time and end time. No permission is required for calling the **queryCurrentBundleEvents()** API. 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci ```ts 105e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci // Promise mode 108e41f4b71Sopenharmony_ci usageStatistics.queryCurrentBundleEvents(0, 20000000000000).then( (res : Array<usageStatistics.BundleEvents>) => { 109e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryCurrentBundleEvents promise success.'); 110e41f4b71Sopenharmony_ci for (let i = 0; i < res.length; i++) { 111e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryCurrentBundleEvents promise number : ' + (i + 1)); 112e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryCurrentBundleEvents promise result ' + JSON.stringify(res[i])); 113e41f4b71Sopenharmony_ci } 114e41f4b71Sopenharmony_ci }).catch( (err : BusinessError) => { 115e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE queryCurrentBundleEvents promise failed. code is: ' + err.code + ',message is: ' + err.message); 116e41f4b71Sopenharmony_ci }); 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci // Asynchronous callback mode 119e41f4b71Sopenharmony_ci usageStatistics.queryCurrentBundleEvents(0, 20000000000000, (err : BusinessError, res : Array<usageStatistics.BundleEvents>) => { 120e41f4b71Sopenharmony_ci if (err) { 121e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback failed. code is: ' + err.code + ',message is: ' + err.message); 122e41f4b71Sopenharmony_ci } else { 123e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback success.'); 124e41f4b71Sopenharmony_ci for (let i = 0; i < res.length; i++) { 125e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback number : ' + (i + 1)); 126e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryCurrentBundleEvents callback result ' + JSON.stringify(res[i])); 127e41f4b71Sopenharmony_ci } 128e41f4b71Sopenharmony_ci } 129e41f4b71Sopenharmony_ci }); 130e41f4b71Sopenharmony_ci ``` 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci5. Query the application usage duration in the specified time frame at the specified interval (daily, weekly, monthly, or annually). The caller must have the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci ```ts 135e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci // Promise mode 138e41f4b71Sopenharmony_ci usageStatistics.queryBundleStatsInfoByInterval(0, 0, 20000000000000).then( (res : Array<usageStatistics.BundleStatsInfo>) => { 139e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise success.'); 140e41f4b71Sopenharmony_ci for (let i = 0; i < res.length; i++) { 141e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise number : ' + (i + 1)); 142e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise result ' + JSON.stringify(res[i])); 143e41f4b71Sopenharmony_ci } 144e41f4b71Sopenharmony_ci }).catch( (err : BusinessError) => { 145e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE queryBundleStatsInfoByInterval promise failed. code is: ' + err.code + ',message is: ' + err.message); 146e41f4b71Sopenharmony_ci }); 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci // Asynchronous callback mode 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci usageStatistics.queryBundleStatsInfoByInterval(0, 0, 20000000000000, (err : BusinessError, res : Array<usageStatistics.BundleStatsInfo>) => { 151e41f4b71Sopenharmony_ci if (err) { 152e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback failed. code is: ' + err.code + ',message is: ' + err.message); 153e41f4b71Sopenharmony_ci } else { 154e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback success.'); 155e41f4b71Sopenharmony_ci for (let i = 0; i < res.length; i++) { 156e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback number : ' + (i + 1)); 157e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryBundleStatsInfoByInterval callback result ' + JSON.stringify(res[i])); 158e41f4b71Sopenharmony_ci } 159e41f4b71Sopenharmony_ci } 160e41f4b71Sopenharmony_ci }); 161e41f4b71Sopenharmony_ci ``` 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci6. Query the priority group of the current application. No permission is required for calling the **queryAppGroup()** API. 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci ```ts 166e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci // Promise mode 169e41f4b71Sopenharmony_ci usageStatistics.queryAppGroup().then( (res : number) => { 170e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryAppGroup promise succeeded. result: ' + JSON.stringify(res)); 171e41f4b71Sopenharmony_ci }).catch( (err : BusinessError) => { 172e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE queryAppGroup promise failed. code is: ' + err.code + ',message is: ' + err.message); 173e41f4b71Sopenharmony_ci }); 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci // Callback mode 176e41f4b71Sopenharmony_ci usageStatistics.queryAppGroup((err : BusinessError, res : number) => { 177e41f4b71Sopenharmony_ci if(err) { 178e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryAppGroup callback failed. code is: ' + err.code + ',message is: ' + err.message); 179e41f4b71Sopenharmony_ci } else { 180e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryAppGroup callback succeeded. result: ' + JSON.stringify(res)); 181e41f4b71Sopenharmony_ci } 182e41f4b71Sopenharmony_ci }); 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci // Synchronous mode 185e41f4b71Sopenharmony_ci let priorityGroup = usageStatistics.queryAppGroupSync(); 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci ``` 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci7. Check whether the application specified by **bundleName** is in the idle state. The caller must have the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci ```ts 192e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci // Promise mode 195e41f4b71Sopenharmony_ci usageStatistics.isIdleState("com.ohos.camera").then( (res : boolean) => { 196e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE isIdleState promise succeeded, result: ' + JSON.stringify(res)); 197e41f4b71Sopenharmony_ci }).catch( (err : BusinessError) => { 198e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE isIdleState promise failed. code is: ' + err.code + ',message is: ' + err.message); 199e41f4b71Sopenharmony_ci }); 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci // Asynchronous callback mode 202e41f4b71Sopenharmony_ci usageStatistics.isIdleState("com.ohos.camera", (err : BusinessError, res : boolean) => { 203e41f4b71Sopenharmony_ci if (err) { 204e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE isIdleState callback failed. code is: ' + err.code + ',message is: ' + err.message); 205e41f4b71Sopenharmony_ci } else { 206e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE isIdleState callback succeeded, result: ' + JSON.stringify(res)); 207e41f4b71Sopenharmony_ci } 208e41f4b71Sopenharmony_ci }); 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci // Synchronous mode 211e41f4b71Sopenharmony_ci let isIdleState = usageStatistics.isIdleStateSync("com.ohos.camera"); 212e41f4b71Sopenharmony_ci ``` 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci8. Obtain the number of FA usage records specified by **maxNum**. If **maxNum** is not specified, the default value **1000** is used. The caller must have the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci ```ts 217e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci // Promise mode 220e41f4b71Sopenharmony_ci usageStatistics.queryModuleUsageRecords(1000).then( (res : Array<usageStatistics.HapModuleInfo>) => { 221e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise succeeded'); 222e41f4b71Sopenharmony_ci for (let i = 0; i < res.length; i++) { 223e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise number : ' + (i + 1)); 224e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise result ' + JSON.stringify(res[i])); 225e41f4b71Sopenharmony_ci } 226e41f4b71Sopenharmony_ci }).catch( (err : BusinessError)=> { 227e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE queryModuleUsageRecords promise failed. code is: ' + err.code + ',message is: ' + err.message); 228e41f4b71Sopenharmony_ci }); 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci // Promise mode when maxNum is not specified 231e41f4b71Sopenharmony_ci usageStatistics.queryModuleUsageRecords().then( (res : Array<usageStatistics.HapModuleInfo>) => { 232e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise succeeded'); 233e41f4b71Sopenharmony_ci for (let i = 0; i < res.length; i++) { 234e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise number : ' + (i + 1)); 235e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords promise result ' + JSON.stringify(res[i])); 236e41f4b71Sopenharmony_ci } 237e41f4b71Sopenharmony_ci }).catch( (err : BusinessError)=> { 238e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE queryModuleUsageRecords promise failed. code is: ' + err.code + ',message is: ' + err.message); 239e41f4b71Sopenharmony_ci }); 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci // Asynchronous callback mode 242e41f4b71Sopenharmony_ci usageStatistics.queryModuleUsageRecords(1000, (err : BusinessError, res : Array<usageStatistics.HapModuleInfo>) => { 243e41f4b71Sopenharmony_ci if(err) { 244e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback failed. code is: ' + err.code + ',message is: ' + err.message); 245e41f4b71Sopenharmony_ci } else { 246e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback succeeded.'); 247e41f4b71Sopenharmony_ci for (let i = 0; i < res.length; i++) { 248e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback number : ' + (i + 1)); 249e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback result ' + JSON.stringify(res[i])); 250e41f4b71Sopenharmony_ci } 251e41f4b71Sopenharmony_ci } 252e41f4b71Sopenharmony_ci }); 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci // Asynchronous callback mode when maxNum is not specified 255e41f4b71Sopenharmony_ci usageStatistics.queryModuleUsageRecords((err : BusinessError, res : Array<usageStatistics.HapModuleInfo>) => { 256e41f4b71Sopenharmony_ci if(err) { 257e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback failed. code is: ' + err.code + ',message is: ' + err.message); 258e41f4b71Sopenharmony_ci } else { 259e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback succeeded.'); 260e41f4b71Sopenharmony_ci for (let i = 0; i < res.length; i++) { 261e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback number : ' + (i + 1)); 262e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryModuleUsageRecords callback result ' + JSON.stringify(res[i])); 263e41f4b71Sopenharmony_ci } 264e41f4b71Sopenharmony_ci } 265e41f4b71Sopenharmony_ci }); 266e41f4b71Sopenharmony_ci ``` 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci9. Query the number of notifications from all applications based on the specified start time and end time. The caller must have the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci ```ts 271e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci // Promise mode 274e41f4b71Sopenharmony_ci usageStatistics.queryNotificationEventStats(0, 20000000000000).then( (res : Array<usageStatistics.DeviceEventStats>) => { 275e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryNotificationEventStats promise success.'); 276e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryNotificationEventStats promise result ' + JSON.stringify(res)); 277e41f4b71Sopenharmony_ci }).catch( (err : BusinessError) => { 278e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE queryNotificationEventStats promise failed. code is: ' + err.code + ',message is: ' + err.message); 279e41f4b71Sopenharmony_ci }); 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci // Asynchronous callback mode 282e41f4b71Sopenharmony_ci usageStatistics.queryNotificationEventStats(0, 20000000000000, (err : BusinessError, res : Array<usageStatistics.DeviceEventStats>) => { 283e41f4b71Sopenharmony_ci if(err) { 284e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryNotificationEventStats callback failed. code is: ' + err.code + ',message is: ' + err.message); 285e41f4b71Sopenharmony_ci } else { 286e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryNotificationEventStats callback success.'); 287e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryNotificationEventStats callback result ' + JSON.stringify(res)); 288e41f4b71Sopenharmony_ci } 289e41f4b71Sopenharmony_ci }); 290e41f4b71Sopenharmony_ci ``` 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci10. Query statistics about system events (hibernation, wakeup, lock, and unlock) that occur between the specified start time and end time. The caller must have the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci ```ts 295e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci // Promise mode 298e41f4b71Sopenharmony_ci usageStatistics.queryDeviceEventStats(0, 20000000000000).then( (res : Array<usageStatistics.DeviceEventStats>) => { 299e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryDeviceEventStates promise success.'); 300e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryDeviceEventStates promise result ' + JSON.stringify(res)); 301e41f4b71Sopenharmony_ci }).catch( (err : BusinessError) => { 302e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE queryDeviceEventStats promise failed. code is: ' + err.code + ',message is: ' + err.message); 303e41f4b71Sopenharmony_ci }); 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci // Asynchronous callback mode 306e41f4b71Sopenharmony_ci usageStatistics.queryDeviceEventStats(0, 20000000000000, (err : BusinessError, res : Array<usageStatistics.DeviceEventStats>) => { 307e41f4b71Sopenharmony_ci if(err) { 308e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryDeviceEventStats callback failed. code is: ' + err.code + ',message is: ' + err.message); 309e41f4b71Sopenharmony_ci } else { 310e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryDeviceEventStats callback success.'); 311e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryDeviceEventStats callback result ' + JSON.stringify(res)); 312e41f4b71Sopenharmony_ci } 313e41f4b71Sopenharmony_ci }); 314e41f4b71Sopenharmony_ci ``` 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci11. Query the priority group of the application specified by **bundleName**. The caller must have the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci ```ts 319e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci // Promise mode when bundleName is specified 322e41f4b71Sopenharmony_ci let bundleName = "com.ohos.camera"; 323e41f4b71Sopenharmony_ci usageStatistics.queryAppGroup(bundleName).then( (res : number) => { 324e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryAppGroup promise succeeded. result: ' + JSON.stringify(res)); 325e41f4b71Sopenharmony_ci }).catch( (err : BusinessError) => { 326e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE queryAppGroup promise failed. code is: ' + err.code + ',message is: ' + err.message); 327e41f4b71Sopenharmony_ci }); 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci // Asynchronous callback mode when bundleName is specified 330e41f4b71Sopenharmony_ci let bundleName = "com.ohos.camera"; 331e41f4b71Sopenharmony_ci usageStatistics.queryAppGroup(bundleName, (err : BusinessError, res : number) => { 332e41f4b71Sopenharmony_ci if(err) { 333e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryAppGroup callback failed. code is: ' + err.code + ',message is: ' + err.message); 334e41f4b71Sopenharmony_ci } else { 335e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE queryAppGroup callback succeeded. result: ' + JSON.stringify(res)); 336e41f4b71Sopenharmony_ci } 337e41f4b71Sopenharmony_ci }); 338e41f4b71Sopenharmony_ci ``` 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ci12. Set the priority group of for application specified by **bundleName**. The caller must have the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci ```ts 343e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci // Promise mode 346e41f4b71Sopenharmony_ci let bundleName = "com.example.deviceUsageStatistics"; 347e41f4b71Sopenharmony_ci let newGroup = usageStatistics.GroupType.DAILY_GROUP; 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci usageStatistics.setAppGroup(bundleName, newGroup).then( () => { 350e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE setAppGroup promise succeeded.'); 351e41f4b71Sopenharmony_ci }).catch( (err : BusinessError) => { 352e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE setAppGroup promise failed. code is: ' + err.code + ',message is: ' + err.message); 353e41f4b71Sopenharmony_ci }); 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci // Asynchronous callback mode 356e41f4b71Sopenharmony_ci let bundleName = "com.example.deviceUsageStatistics"; 357e41f4b71Sopenharmony_ci let newGroup = usageStatistics.GroupType.DAILY_GROUP; 358e41f4b71Sopenharmony_ci usageStatistics.setAppGroup(bundleName, newGroup, (err : BusinessError) => { 359e41f4b71Sopenharmony_ci if(err) { 360e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE setAppGroup callback failed. code is: ' + err.code + ',message is: ' + err.message); 361e41f4b71Sopenharmony_ci } else { 362e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE setAppGroup callback succeeded.'); 363e41f4b71Sopenharmony_ci } 364e41f4b71Sopenharmony_ci }); 365e41f4b71Sopenharmony_ci ``` 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci13. 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. The caller must have the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci ```ts 370e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 371e41f4b71Sopenharmony_ci 372e41f4b71Sopenharmony_ci // Promise mode 373e41f4b71Sopenharmony_ci function onBundleGroupChanged (res : usageStatistics.AppGroupCallbackInfo) { 374e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack RegisterGroupCallBack callback success.'); 375e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appOldGroup is : ' + res.appOldGroup); 376e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appNewGroup is : ' + res.appNewGroup); 377e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack result changeReason is : ' + res.changeReason); 378e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack result userId is : ' + res.userId); 379e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack result bundleName is : ' + res.bundleName); 380e41f4b71Sopenharmony_ci }; 381e41f4b71Sopenharmony_ci usageStatistics.registerAppGroupCallBack(onBundleGroupChanged).then( () => { 382e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack promise succeeded.'); 383e41f4b71Sopenharmony_ci }).catch( (err : BusinessError) => { 384e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE registerAppGroupCallBack promise failed. code is: ' + err.code + ',message is: ' + err.message); 385e41f4b71Sopenharmony_ci }); 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ci // Asynchronous callback mode 388e41f4b71Sopenharmony_ci function onBundleGroupChanged (res : usageStatistics.AppGroupCallbackInfo) { 389e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack callback success.'); 390e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appOldGroup is : ' + res.appOldGroup); 391e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack result appNewGroup is : ' + res.appNewGroup); 392e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack result changeReason is : ' + res.changeReason); 393e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack result userId is : ' + res.userId); 394e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack result bundleName is : ' + res.bundleName); 395e41f4b71Sopenharmony_ci }; 396e41f4b71Sopenharmony_ci usageStatistics.registerAppGroupCallBack(onBundleGroupChanged, (err : BusinessError) => { 397e41f4b71Sopenharmony_ci if(err) { 398e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack callback failed. code is: ' + err.code + ',message is: ' + err.message); 399e41f4b71Sopenharmony_ci } else { 400e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE registerAppGroupCallBack callback success.'); 401e41f4b71Sopenharmony_ci } 402e41f4b71Sopenharmony_ci }); 403e41f4b71Sopenharmony_ci ``` 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci14. Deregister the callback for application group changes. The caller must have the **ohos.permission.BUNDLE_ACTIVE_INFO** permission. 406e41f4b71Sopenharmony_ci 407e41f4b71Sopenharmony_ci ```ts 408e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci // Promise mode 411e41f4b71Sopenharmony_ci usageStatistics.unregisterAppGroupCallBack().then( () => { 412e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack promise succeeded.'); 413e41f4b71Sopenharmony_ci }).catch( (err : BusinessError) => { 414e41f4b71Sopenharmony_ci console.error('BUNDLE_ACTIVE unregisterAppGroupCallBack promise failed. code is: ' + err.code + ',message is: ' + err.message); 415e41f4b71Sopenharmony_ci }); 416e41f4b71Sopenharmony_ci 417e41f4b71Sopenharmony_ci // Callback mode 418e41f4b71Sopenharmony_ci usageStatistics.unregisterAppGroupCallBack((err : BusinessError) => { 419e41f4b71Sopenharmony_ci if(err) { 420e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack callback failed. code is: ' + err.code + ',message is: ' + err.message); 421e41f4b71Sopenharmony_ci } else { 422e41f4b71Sopenharmony_ci console.log('BUNDLE_ACTIVE unregisterAppGroupCallBack callback success.'); 423e41f4b71Sopenharmony_ci } 424e41f4b71Sopenharmony_ci }); 425e41f4b71Sopenharmony_ci ``` 426