With 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 bundle group.
The 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.
## Available APIs
Import the **stats** package to implement registration:
```js
importstatsfrom'@ohos.bundleState';
```
**Table 1** Major APIs for device usage statistics
| API| Description|
| -------- | -------- |
| function queryBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void | Queries events of all applications based on the specified start time and end time.|
| function queryBundleStateInfos(begin: number, end: number, callback: AsyncCallback<BundleActiveInfoResponse>): void | Queries the application usage duration statistics based on the specified start time and end time.|
| function queryCurrentBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void | Queries events of this application based on the specified start time and end time.|
| function queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number, callback: AsyncCallback<Array<BundleStateInfo>>): void | Queries the application usage duration statistics in the specified time frame at the specified interval (daily, weekly, monthly, or annually).|
| function queryAppUsagePriorityGroup(callback: AsyncCallback<number>): void | Queries the priority group of the current invoker application.|
| function isIdleState(bundleName: string, callback: AsyncCallback<boolean>): void | Checks whether the application specified by **bundleName** is in the idle state. |
## How to Develop
1. Configure the device usage statistics permission in the **config.json** file.
```json
"module": {
"package": "com.example.deviceUsageStatistics",
...,
"reqPermissions": [
{
"name": "ohos.permission.BUNDLE_ACTIVE_INFO"
}
]
}
```
2. 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 in the **config.json** file.
```js
import stats from '@ohos.bundleState'
// Use a promise to return the result.
stats.queryBundleActiveStates(0, 20000000000000).then( res => {
3. 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 in the **config.json** file.
```js
import stats from '@ohos.bundleState'
// Use a promise to return the result.
stats.queryBundleStateInfos(0, 20000000000000).then( res => {
4. Query events of this application based on the specified start time and end time. This requires no permission to be configured in the **config.json** file.
```js
import stats from '@ohos.bundleState'
// Use a promise to return the result.
stats.queryCurrentBundleActiveStates(0, 20000000000000).then( res => {
5. 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 in the **config.json** file.
```js
import stats from '@ohos.bundleState'
// Use a promise to return the result.
stats.queryBundleStateInfoByInterval(0, 0, 20000000000000).then( res => {
7. Check whether the application specified by **bundleName** is in the idle state. This requires no permission to be configured in the **config.json** file.
```js
import stats from '@ohos.bundleState'
// Use a promise to return the result.
stats.isIdleState("com.ohos.camera").then( res => {
With device usage statistics APIs, you can have a better understanding of the application, notification, and system usage. In application usage statistics, you can query the application usage, event log, and bundle group. The 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.
## Introduction
Currently you can have access to statistics on the application usage, and notification and system usage statistics feature will be available for use in later versions.
-**The application usage statistics is updated**:
>1. Every 30 minutes
>2. Upon system time change
>3. Upon start of a new day
-**The application usage statistics can include the following**:
>1. Events of all applications based on the specified start time and end time
>2. Application usage duration statistics based on the specified start time and end time
>3. Events of the current application based on the specified start time and end time
>4. Application usage duration statistics in the specified time frame at the specified interval (daily, weekly, monthly, or annually)
>5. Priority group of the current invoker application
>6. Whether a specific application is in the idle state
### Required Permissions
- The **queryBundleActiveStates**, **queryBundleStateInfos**, and **queryBundleStateInfoByInterval** APIs used for device usage statistics are system APIs. Before calling these APIs, you need to apply for the **ohos.permission.BUNDLE_ACTIVE_INFO** permission.
- This permission is not required for calling **queryCurrentBundleActiveStates**, **queryAppUsagePriorityGroup**, and **isIdleState**, which are third-party APIs.