# @ohos.bundleState (Device Usage Statistics) (System API)
This module provides APIs for collecting statistics on device usage.
System applications can call these APIs to implement the following features:
- Query the usage duration in different time segments, events (foreground, background, start and end of continuous tasks), and the number of notifications, on a per application basis.
- Query the bundle group information of the invoking application itself.
- Query the idle status of applications, including the invoking application itself.
> **NOTE**
>
> This module is deprecated since API version 9. You are advised to use [@ohos.resourceschedule.usageStatistics (Device Usage Statistics) (System API)](js-apis-resourceschedule-deviceUsageStatistics-sys.md) instead.
>
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>
> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.bundleState](js-apis-deviceUsageStatistics.md).
## Modules to Import
```js
import bundleState from '@ohos.bundleState'
```
## bundleState.queryBundleStateInfos(deprecated)
queryBundleStateInfos(begin: number, end: number, callback: AsyncCallback<BundleActiveInfoResponse>): void
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.queryBundleStatsInfos](js-apis-resourceschedule-deviceUsageStatistics-sys.md#usagestatisticsquerybundlestatsinfos) instead.
Queries the application usage duration statistics based on the specified start time and end time. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | --------------------------------------- |
| begin | number | Yes | Start time, in milliseconds. |
| end | number | Yes | End time, in milliseconds. |
| callback | AsyncCallback<[BundleActiveInfoResponse](js-apis-deviceUsageStatistics-sys.md#bundleactiveinforesponse)> | Yes | Callback used to return the application usage duration statistics.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
bundleState.queryBundleStateInfos(0, 20000000000000, (err: BusinessError ,
res: bundleState.BundleActiveInfoResponse ) => {
if (err) {
console.error('BUNDLE_ACTIVE queryBundleStateInfos callback failed, because: ' + err.code);
} else {
console.log('BUNDLE_ACTIVE queryBundleStateInfos callback success.');
console.log('BUNDLE_ACTIVE queryBundleStateInfos callback result ' + JSON.stringify(res));
}
});
```
## bundleState.queryBundleStateInfos(deprecated)
queryBundleStateInfos(begin: number, end: number): Promise<BundleActiveInfoResponse>
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.queryBundleStatsInfos](js-apis-resourceschedule-deviceUsageStatistics-sys.md#usagestatisticsquerybundlestatsinfos-1) instead.
Queries the application usage duration statistics based on the specified start time and end time. This API uses a promise to return the result.
**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| begin | number | Yes | Start time, in milliseconds.|
| end | number | Yes | End time, in milliseconds.|
**Return value**
| Type | Description |
| ---------------------------------------- | -------------------------------------- |
| Promise<[BundleActiveInfoResponse](js-apis-deviceUsageStatistics-sys.md#bundleactiveinforesponse)> | Promise used to return the application usage duration statistics.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
bundleState.queryBundleStateInfos(0, 20000000000000).then((res: bundleState.BundleActiveInfoResponse) => {
console.log('BUNDLE_ACTIVE queryBundleStateInfos promise success.');
console.log('BUNDLE_ACTIVE queryBundleStateInfos promise result ' + JSON.stringify(res));
}).catch((err: BusinessError) => {
console.error('BUNDLE_ACTIVE queryBundleStateInfos promise failed, because: ' + err.code);
});
```
## bundleState.queryBundleStateInfoByInterval(deprecated)
queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number, callback: AsyncCallback<Array<BundleStateInfo>>): void
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.queryBundleStatsInfoByInterval](js-apis-resourceschedule-deviceUsageStatistics-sys.md#usagestatisticsquerybundlestatsinfobyinterval) instead.
Queries the application usage duration statistics in the specified time frame at the specified interval (daily, weekly, monthly, or annually). This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
| byInterval | [IntervalType](js-apis-deviceUsageStatistics-sys.md#intervaltype) | Yes | Type of information to be queried. |
| begin | number | Yes | Start time, in milliseconds. |
| end | number | Yes | End time, in milliseconds. |
| callback | AsyncCallback<Array<[BundleStateInfo](js-apis-deviceUsageStatistics-sys.md#bundlestateinfodeprecated)>> | Yes | Callback used to return the application usage duration statistics.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
bundleState.queryBundleStateInfoByInterval(bundleState.IntervalType.BY_OPTIMIZED, 0, 20000000000000, (err: BusinessError, res: Array) => {
if (err) {
console.error('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback failed, because: ' + err.code);
} else {
console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback success.');
for (let i = 0; i < res.length; i++) {
console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback number : ' + (i + 1));
console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback result ' + JSON.stringify(res[i]));
}
}
});
```
## bundleState.queryBundleStateInfoByInterval(deprecated)
queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number): Promise<Array<BundleStateInfo>>
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.queryBundleStatsInfoByInterval](js-apis-resourceschedule-deviceUsageStatistics-sys.md#usagestatisticsquerybundlestatsinfobyinterval-1) instead.
Queries the application usage duration statistics in the specified time frame at the specified interval (daily, weekly, monthly, or annually). This API uses a promise to return the result.
**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| ---------- | ----------------------------- | ---- | ----- |
| byInterval | [IntervalType](js-apis-deviceUsageStatistics-sys.md#intervaltype) | Yes | Type of information to be queried.|
| begin | number | Yes | Start time, in milliseconds.|
| end | number | Yes | End time, in milliseconds.|
**Return value**
| Type | Description |
| ---------------------------------------- | ---------------------------------------- |
| Promise<Array<[BundleStateInfo](js-apis-deviceUsageStatistics-sys.md#bundlestateinfodeprecated)>> | Promise used to return the application usage duration statistics.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
bundleState.queryBundleStateInfoByInterval(bundleState.IntervalType.BY_OPTIMIZED, 0, 20000000000000).then((res: Array) => {
console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise success.');
for (let i = 0; i < res.length; i++) {
console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise number : ' + (i + 1));
console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise result ' + JSON.stringify(res[i]));
}
}).catch((err: BusinessError) => {
console.error('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise failed, because: ' + err.code);
});
```
## bundleState.queryBundleActiveStates(deprecated)
queryBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.queryBundleEvents](js-apis-resourceschedule-deviceUsageStatistics-sys.md#usagestatisticsquerybundleevents) instead.
Queries events of all applications based on the specified start time and end time. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | --------------------------------------- |
| begin | number | Yes | Start time, in milliseconds. |
| end | number | Yes | End time, in milliseconds. |
| callback | AsyncCallback<Array<[BundleActiveState](js-apis-deviceUsageStatistics-sys.md#bundleactivestatedeprecated)>> | Yes | Callback used to return the events obtained.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
bundleState.queryBundleActiveStates(0, 20000000000000, (err: BusinessError, res: Array) => {
if (err) {
console.error('BUNDLE_ACTIVE queryBundleActiveStates callback failed, because: ' + err.code);
} else {
console.log('BUNDLE_ACTIVE queryBundleActiveStates callback success.');
for (let i = 0; i < res.length; i++) {
console.log('BUNDLE_ACTIVE queryBundleActiveStates callback number : ' + (i + 1));
console.log('BUNDLE_ACTIVE queryBundleActiveStates callback result ' + JSON.stringify(res[i]));
}
}
});
```
## bundleState.queryBundleActiveStates(deprecated)
queryBundleActiveStates(begin: number, end: number): Promise<Array<BundleActiveState>>
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.queryBundleEvents](js-apis-resourceschedule-deviceUsageStatistics-sys.md#usagestatisticsquerybundleevents-1) instead.
Queries events of all applications based on the specified start time and end time. This API uses a promise to return the result.
**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| begin | number | Yes | Start time, in milliseconds.|
| end | number | Yes | End time, in milliseconds.|
**Return value**
| Type | Description |
| ---------------------------------------- | -------------------------------------- |
| Promise<Array<[BundleActiveState](js-apis-deviceUsageStatistics-sys.md#bundleactivestatedeprecated)>> | Promise used to return the events obtained.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
bundleState.queryBundleActiveStates(0, 20000000000000).then((res: Array) => {
console.log('BUNDLE_ACTIVE queryBundleActiveStates promise success.');
for (let i = 0; i < res.length; i++) {
console.log('BUNDLE_ACTIVE queryBundleActiveStates promise number : ' + (i + 1));
console.log('BUNDLE_ACTIVE queryBundleActiveStates promise result ' + JSON.stringify(res[i]));
}
}).catch((err: BusinessError) => {
console.error('BUNDLE_ACTIVE queryBundleActiveStates promise failed, because: ' + err.code);
});
```
## bundleState.queryAppUsagePriorityGroup(deprecated)
queryAppUsagePriorityGroup(): Promise<number>
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.queryAppGroup](js-apis-resourceschedule-deviceUsageStatistics-sys.md#usagestatisticsqueryappgroup) instead.
Queries the priority group of this application. This API uses a promise to return the result.
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.AppGroup
**System API**: This is a system API.
**Return value**
| Type | Description |
| --------------- | --------------------------- |
| Promise<number> | Promise used to return the priority group.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
bundleState.queryAppUsagePriorityGroup().then((res: number) => {
console.log('BUNDLE_ACTIVE QueryPackageGroup promise succeeded. result: ' + JSON.stringify(res));
}).catch((err: BusinessError) => {
console.error('BUNDLE_ACTIVE QueryPackageGroup promise failed. because: ' + err.code);
});
```
## bundleState.queryAppUsagePriorityGroup(deprecated)
queryAppUsagePriorityGroup(callback: AsyncCallback<number>): void
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.queryAppGroup](js-apis-resourceschedule-deviceUsageStatistics-sys.md#usagestatisticsqueryappgroup-1) instead.
Queries the priority group of this application. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.AppGroup
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | --------------------- | ---- | -------------------------- |
| callback | AsyncCallback<number> | Yes | Callback used to return the priority group.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
bundleState.queryAppUsagePriorityGroup((err: BusinessError, res: number) => {
if(err) {
console.error('BUNDLE_ACTIVE QueryPackageGroup callback failed. because: ' + err.code);
} else {
console.log('BUNDLE_ACTIVE QueryPackageGroup callback succeeded. result: ' + JSON.stringify(res));
}
});
```
## bundleState.queryCurrentBundleActiveStates(deprecated)
queryCurrentBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.queryCurrentBundleEvents](js-apis-resourceschedule-deviceUsageStatistics-sys.md#usagestatisticsquerycurrentbundleevents) instead.
Queries events of this application based on the specified start time and end time. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | --------------------------------------- |
| begin | number | Yes | Start time, in milliseconds. |
| end | number | Yes | End time, in milliseconds. |
| callback | AsyncCallback<Array<[BundleActiveState](#bundleactivestatedeprecated)>> | Yes | Callback used to return the events.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
bundleState.queryCurrentBundleActiveStates(0, 20000000000000, (err: BusinessError, res: Array) => {
if (err) {
console.error('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback failed, because: ' + err.code);
} else {
console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback success.');
for (let i = 0; i < res.length; i++) {
console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback number : ' + (i + 1));
console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback result ' + JSON.stringify(res[i]));
}
}
});
```
## bundleState.queryCurrentBundleActiveStates(deprecated)
queryCurrentBundleActiveStates(begin: number, end: number): Promise<Array<BundleActiveState>>
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.queryCurrentBundleEvents](js-apis-resourceschedule-deviceUsageStatistics-sys.md#usagestatisticsquerycurrentbundleevents) instead.
Queries events of this application based on the specified start time and end time. This API uses a promise to return the result.
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | ----- |
| begin | number | Yes | Start time, in milliseconds.|
| end | number | Yes | End time, in milliseconds.|
**Return value**
| Type | Description |
| ---------------------------------------- | -------------------------------------- |
| Promise<Array<[BundleActiveState](#bundleactivestatedeprecated)>> | Promise used to return the events.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
bundleState.queryCurrentBundleActiveStates(0, 20000000000000).then((res: Array) => {
console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise success.');
for (let i = 0; i < res.length; i++) {
console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise number : ' + (i + 1));
console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise result ' + JSON.stringify(res[i]));
}
}).catch((err: BusinessError) => {
console.error('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise failed, because: ' + err.code);
});
```
## BundleStateInfo(deprecated)
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.BundleStatsInfo](js-apis-resourceschedule-deviceUsageStatistics-sys.md#bundlestatsinfo) instead.
Provides the usage duration information of an application.
### Properties
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
| Name | Type | Mandatory | Description |
| ------------------------ | ------ | ---- | ---------------------------------------- |
| bundleName | string | No | Bundle name of the application. |
| abilityPrevAccessTime | number | No | Last time when the application was used. |
| abilityInFgTotalTime | number | No | Total time that the application runs in the foreground. |
| id | number | Yes | User ID.|
| abilityPrevSeenTime | number | No | Last time when the application was visible in the foreground.|
| abilitySeenTotalTime | number | No | Total time that the application is visible in the foreground.|
| fgAbilityAccessTotalTime | number | No | Total time that the application accesses the foreground.|
| fgAbilityPrevAccessTime | number | No | Last time when the application accessed the foreground.|
| infosBeginTime | number | No | Time logged in the first application usage record in the **BundleActiveInfo** object.|
| infosEndTime | number | No | Time logged in the last application usage record in the **BundleActiveInfo** object.|
### merge(deprecated)
merge(toMerge: BundleStateInfo): void
Merges the device usage statistics of applications with the same bundle name.
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| toMerge | [BundleStateInfo](#bundlestateinfodeprecated) | Yes| Device usage statistics to merge.|
## BundleActiveState(deprecated)
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.BundleEvents](js-apis-resourceschedule-deviceUsageStatistics-sys.md#bundleevents) instead.
Provides information about an application event.
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
| Name | Type | Mandatory | Description |
| --------------------- | ------ | ---- | ---------------------------------------- |
| bundleName | string | No | Bundle name of the application. |
| stateType | number | No | Application event type. |
| stateOccurredTime | number | No | Timestamp when the application event occurs. |
| appUsagePriorityGroup | number | No | Group of the application by usage priority.|
| indexOfLink | string | No | Shortcut ID.|
| nameOfClass | string | No | Class name.|
## BundleActiveInfoResponse(deprecated)
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.BundleStatsMap](js-apis-resourceschedule-deviceUsageStatistics-sys.md#bundlestatsmap) instead.
Provides the usage duration information of an application.
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
| Name | Type | Mandatory | Description |
| ------------------------------ | ---------------------------------------- | ---- | -------------- |
| [key: string]: BundleStateInfo | [key: string]: [BundleStateInfo](#bundlestateinfodeprecated) | Yes | Usage duration information by application.|
## IntervalType(deprecated)
> This API is supported since API version 7 and deprecated since API version 9. Use [usageStatistics.intervaltype](js-apis-resourceschedule-deviceUsageStatistics-sys.md#intervaltype) instead.
Enumerates the interval types for querying the application usage duration.
**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App
| Name | Value | Description |
| ------------ | ---- | ---------------------------------------- |
| BY_OPTIMIZED | 0 | The system obtains the application usage duration statistics in the specified time frame at the interval the system deems appropriate.|
| BY_DAILY | 1 | The system queries the application usage duration statistics in the specified time frame on a daily basis. |
| BY_WEEKLY | 2 | The system queries the application usage duration statistics in the specified time frame on a weekly basis. |
| BY_MONTHLY | 3 | The system queries the application usage duration statistics in the specified time frame on a monthly basis. |
| BY_ANNUALLY | 4 | The system queries the application usage duration statistics in the specified time frame on an annual basis. |