1# @ohos.bundleState (Device Usage Statistics) (System API) 2 3This module provides APIs for collecting statistics on device usage. 4 5System applications can call these APIs to implement the following features: 6 7- 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. 8- Query the bundle group information of the invoking application itself. 9- Query the idle status of applications, including the invoking application itself. 10 11> **NOTE** 12> 13> 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. 14> 15> 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. 16> 17> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.bundleState](js-apis-deviceUsageStatistics.md). 18 19## Modules to Import 20 21```js 22import bundleState from '@ohos.bundleState' 23``` 24 25## bundleState.queryBundleStateInfos 26 27queryBundleStateInfos(begin: number, end: number, callback: AsyncCallback<BundleActiveInfoResponse>): void 28 29Queries the application usage duration statistics based on the specified start time and end time. This API uses an asynchronous callback to return the result. 30 31**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO 32 33**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App 34 35**System API**: This is a system API and cannot be called by third-party applications. 36 37**Parameters** 38 39| Name | Type | Mandatory | Description | 40| -------- | ---------------------------------------- | ---- | --------------------------------------- | 41| begin | number | Yes | Start time, in milliseconds. | 42| end | number | Yes | End time, in milliseconds. | 43| callback | AsyncCallback<[BundleActiveInfoResponse](js-apis-deviceUsageStatistics.md#bundleactiveinforesponse)> | Yes | Callback used to return the application usage duration statistics.| 44 45**Example** 46 47```ts 48import { BusinessError } from '@ohos.base'; 49 50bundleState.queryBundleStateInfos(0, 20000000000000, (err: BusinessError , 51 res: bundleState.BundleActiveInfoResponse ) => { 52 if (err) { 53 console.error('BUNDLE_ACTIVE queryBundleStateInfos callback failed, because: ' + err.code); 54 } else { 55 console.log('BUNDLE_ACTIVE queryBundleStateInfos callback success.'); 56 console.log('BUNDLE_ACTIVE queryBundleStateInfos callback result ' + JSON.stringify(res)); 57 } 58}); 59``` 60 61## bundleState.queryBundleStateInfos 62 63queryBundleStateInfos(begin: number, end: number): Promise<BundleActiveInfoResponse> 64 65Queries the application usage duration statistics based on the specified start time and end time. This API uses a promise to return the result. 66 67**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO 68 69**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App 70 71**System API**: This is a system API and cannot be called by third-party applications. 72 73**Parameters** 74 75| Name | Type | Mandatory | Description | 76| ----- | ------ | ---- | ----- | 77| begin | number | Yes | Start time, in milliseconds.| 78| end | number | Yes | End time, in milliseconds.| 79 80**Return value** 81 82| Type | Description | 83| ---------------------------------------- | -------------------------------------- | 84| Promise<[BundleActiveInfoResponse](js-apis-deviceUsageStatistics.md#bundleactiveinforesponse)> | Promise used to return the result. return the application usage duration statistics.| 85 86**Example** 87 88```ts 89import { BusinessError } from '@ohos.base'; 90 91bundleState.queryBundleStateInfos(0, 20000000000000).then((res: bundleState.BundleActiveInfoResponse) => { 92 console.log('BUNDLE_ACTIVE queryBundleStateInfos promise success.'); 93 console.log('BUNDLE_ACTIVE queryBundleStateInfos promise result ' + JSON.stringify(res)); 94}).catch((err: BusinessError) => { 95 console.error('BUNDLE_ACTIVE queryBundleStateInfos promise failed, because: ' + err.code); 96}); 97``` 98 99## bundleState.queryBundleStateInfoByInterval 100 101queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number, callback: AsyncCallback<Array<BundleStateInfo>>): void 102 103Queries 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. 104 105**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO 106 107**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App 108 109**System API**: This is a system API and cannot be called by third-party applications. 110 111**Parameters** 112 113| Name | Type | Mandatory | Description | 114| ---------- | ---------------------------------------- | ---- | ---------------------------------------- | 115| byInterval | [IntervalType](js-apis-deviceUsageStatistics.md#intervaltype) | Yes | Type of information to be queried. | 116| begin | number | Yes | Start time, in milliseconds. | 117| end | number | Yes | End time, in milliseconds. | 118| callback | AsyncCallback<Array<[BundleStateInfo](js-apis-deviceUsageStatistics.md#bundlestateinfo)>> | Yes | Callback used to return the application usage duration statistics.| 119 120**Example** 121 122```ts 123import { BusinessError } from '@ohos.base'; 124 125bundleState.queryBundleStateInfoByInterval(bundleState.IntervalType.BY_OPTIMIZED, 0, 20000000000000, (err: BusinessError, res: Array<bundleState.BundleStateInfo>) => { 126 if (err) { 127 console.error('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback failed, because: ' + err.code); 128 } else { 129 console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback success.'); 130 for (let i = 0; i < res.length; i++) { 131 console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback number : ' + (i + 1)); 132 console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback result ' + JSON.stringify(res[i])); 133 } 134 } 135}); 136``` 137 138## bundleState.queryBundleStateInfoByInterval 139 140queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number): Promise<Array<BundleStateInfo>> 141 142Queries 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. 143 144**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO 145 146**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App 147 148**System API**: This is a system API and cannot be called by third-party applications. 149 150**Parameters** 151 152| Name | Type | Mandatory | Description | 153| ---------- | ----------------------------- | ---- | ----- | 154| byInterval | [IntervalType](js-apis-deviceUsageStatistics.md#intervaltype) | Yes | Type of information to be queried.| 155| begin | number | Yes | Start time, in milliseconds.| 156| end | number | Yes | End time, in milliseconds.| 157 158**Return value** 159 160| Type | Description | 161| ---------------------------------------- | ---------------------------------------- | 162| Promise<Array<[BundleStateInfo](js-apis-deviceUsageStatistics.md#bundlestateinfo)>> | Promise used to return the result. return the application usage duration statistics.| 163 164**Example** 165 166```ts 167import { BusinessError } from '@ohos.base'; 168 169bundleState.queryBundleStateInfoByInterval(bundleState.IntervalType.BY_OPTIMIZED, 0, 20000000000000).then((res: Array<bundleState.BundleStateInfo>) => { 170 console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise success.'); 171 for (let i = 0; i < res.length; i++) { 172 console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise number : ' + (i + 1)); 173 console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise result ' + JSON.stringify(res[i])); 174 } 175}).catch((err: BusinessError) => { 176 console.error('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise failed, because: ' + err.code); 177}); 178``` 179 180## bundleState.queryBundleActiveStates 181 182queryBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void 183 184Queries events of all applications based on the specified start time and end time. This API uses an asynchronous callback to return the result. 185 186**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO 187 188**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App 189 190**System API**: This is a system API and cannot be called by third-party applications. 191 192**Parameters** 193 194| Name | Type | Mandatory | Description | 195| -------- | ---------------------------------------- | ---- | --------------------------------------- | 196| begin | number | Yes | Start time, in milliseconds. | 197| end | number | Yes | End time, in milliseconds. | 198| callback | AsyncCallback<Array<[BundleActiveState](js-apis-deviceUsageStatistics.md#bundleactivestate)>> | Yes | Callback used to return the events obtained.| 199 200**Example** 201 202```ts 203import { BusinessError } from '@ohos.base'; 204 205bundleState.queryBundleActiveStates(0, 20000000000000, (err: BusinessError, res: Array<bundleState.BundleActiveState>) => { 206 if (err) { 207 console.error('BUNDLE_ACTIVE queryBundleActiveStates callback failed, because: ' + err.code); 208 } else { 209 console.log('BUNDLE_ACTIVE queryBundleActiveStates callback success.'); 210 for (let i = 0; i < res.length; i++) { 211 console.log('BUNDLE_ACTIVE queryBundleActiveStates callback number : ' + (i + 1)); 212 console.log('BUNDLE_ACTIVE queryBundleActiveStates callback result ' + JSON.stringify(res[i])); 213 } 214 } 215}); 216``` 217 218## bundleState.queryBundleActiveStates 219 220queryBundleActiveStates(begin: number, end: number): Promise<Array<BundleActiveState>> 221 222Queries events of all applications based on the specified start time and end time. This API uses a promise to return the result. 223 224**Required permissions**: ohos.permission.BUNDLE_ACTIVE_INFO 225 226**System capability**: SystemCapability.ResourceSchedule.UsageStatistics.App 227 228**System API**: This is a system API and cannot be called by third-party applications. 229 230**Parameters** 231 232| Name | Type | Mandatory | Description | 233| ----- | ------ | ---- | ----- | 234| begin | number | Yes | Start time, in milliseconds.| 235| end | number | Yes | End time, in milliseconds.| 236 237**Return value** 238 239| Type | Description | 240| ---------------------------------------- | -------------------------------------- | 241| Promise<Array<[BundleActiveState](js-apis-deviceUsageStatistics.md#bundleactivestate)>> | Promise used to return the result. return the events obtained.| 242 243**Example** 244 245```ts 246import { BusinessError } from '@ohos.base'; 247 248bundleState.queryBundleActiveStates(0, 20000000000000).then((res: Array<bundleState.BundleActiveState>) => { 249 console.log('BUNDLE_ACTIVE queryBundleActiveStates promise success.'); 250 for (let i = 0; i < res.length; i++) { 251 console.log('BUNDLE_ACTIVE queryBundleActiveStates promise number : ' + (i + 1)); 252 console.log('BUNDLE_ACTIVE queryBundleActiveStates promise result ' + JSON.stringify(res[i])); 253 } 254}).catch((err: BusinessError) => { 255 console.error('BUNDLE_ACTIVE queryBundleActiveStates promise failed, because: ' + err.code); 256}); 257``` 258