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