• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Device Usage Statistics Development
2
3## When to Use
4
5With 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.
6The 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.
7
8## Available APIs
9Import the **stats** package to implement registration:
10```js
11import stats from '@ohos.bundleState';
12```
13
14**Table 1** Major APIs for device usage statistics
15
16| API| Description|
17| -------- | -------- |
18| 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.|
19| 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.|
20| 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.|
21| 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).|
22| function queryAppUsagePriorityGroup(callback: AsyncCallback<number>): void | Queries the priority group of the current invoker application.|
23| function isIdleState(bundleName: string, callback: AsyncCallback<boolean>): void | Checks whether the application specified by **bundleName** is in the idle state. |
24
25## How to Develop
26
271. Configure the device usage statistics permission in the **config.json** file.
28
29    ```json
30    "module": {
31        "package": "com.example.deviceUsageStatistics",
32        ...,
33        "reqPermissions": [
34            {
35            "name": "ohos.permission.BUNDLE_ACTIVE_INFO"
36            }
37        ]
38    }
39    ```
40
412. 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.
42
43    ```js
44    import stats from '@ohos.bundleState'
45
46    // Use a promise to return the result.
47    stats.queryBundleActiveStates(0, 20000000000000).then( res => {
48        console.log('BUNDLE_ACTIVE queryBundleActiveStates promise success.');
49        for (let i = 0; i < res.length; i++) {
50            console.log('BUNDLE_ACTIVE queryBundleActiveStates promise number : ' + (i + 1));
51            console.log('BUNDLE_ACTIVE queryBundleActiveStates promise result ' + JSON.stringify(res[i]));
52        }
53    }).catch( err => {
54        console.log('BUNDLE_ACTIVE queryBundleActiveStates promise failed, because: ' + err.code);
55    });
56
57    // Use an asynchronous callback to return the result.
58    stats.queryBundleActiveStates(0, 20000000000000, (err, res) => {
59        if (err) {
60            console.log('BUNDLE_ACTIVE queryBundleActiveStates callback failed, because: ' + err.code);
61        } else {
62            console.log('BUNDLE_ACTIVE queryBundleActiveStates callback success.');
63            for (let i = 0; i < res.length; i++) {
64                console.log('BUNDLE_ACTIVE queryBundleActiveStates callback number : ' + (i + 1));
65                console.log('BUNDLE_ACTIVE queryBundleActiveStates callback result ' + JSON.stringify(res[i]));
66            }
67        } else {
68            console.log('BUNDLE_ACTIVE queryBundleActiveStates callback failed, because: ' + err.code);
69        }
70    });
71    ```
72
733. 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.
74
75    ```js
76    import stats from '@ohos.bundleState'
77
78    // Use a promise to return the result.
79    stats.queryBundleStateInfos(0, 20000000000000).then(res => {
80        console.log('BUNDLE_ACTIVE queryBundleStateInfos promise success.');
81        let i = 1;
82        for (let key in res) {
83            console.log('BUNDLE_ACTIVE queryBundleStateInfos promise number : ' + i);
84            console.log('BUNDLE_ACTIVE queryBundleStateInfos promise result ' + JSON.stringify(res[key]));
85            i++;
86        }
87    }).catch(err => {
88        console.log('BUNDLE_ACTIVE queryBundleStateInfos promise failed, because: ' + err.code);
89    });
90
91    // Use an asynchronous callback to return the result.
92    stats.queryBundleStateInfos(0, 20000000000000, (err, res) => {
93        if (err) {
94            console.log('BUNDLE_ACTIVE queryBundleStateInfos callback failed, because: ' + err.code);
95        } else {
96            console.log('BUNDLE_ACTIVE queryBundleStateInfos callback success.');
97            let i = 1;
98            for(let key in res){
99                console.log('BUNDLE_ACTIVE queryBundleStateInfos callback number : ' + i);
100                console.log('BUNDLE_ACTIVE queryBundleStateInfos callback result ' + JSON.stringify(res[key]));
101                i++;
102            }
103        } else {
104            console.log('BUNDLE_ACTIVE queryBundleStateInfos callback failed, because: ' + err.code);
105        }
106    });
107    ```
108
1094. 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.
110
111    ```js
112    import stats from '@ohos.bundleState'
113
114    // Use a promise to return the result.
115    stats.queryCurrentBundleActiveStates(0, 20000000000000).then(res => {
116        console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise success.');
117        for (let i = 0; i < res.length; i++) {
118            console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise number : ' + (i + 1));
119            console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise result ' + JSON.stringify(res[i]));
120        }
121    }).catch(err => {
122        console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise failed, because: ' + err.code);
123    });
124
125    // Use an asynchronous callback to return the result.
126    stats.queryCurrentBundleActiveStates(0, 20000000000000, (err, res) => {
127        if (err) {
128            console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback failed, because: ' + err.code);
129        } else {
130            console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback success.');
131            for (let i = 0; i < res.length; i++) {
132                console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback number : ' + (i + 1));
133                console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback result ' + JSON.stringify(res[i]));
134             }
135        }
136    });
137    ```
138
1395. 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.
140
141    ```js
142    import stats from '@ohos.bundleState'
143
144    // Use a promise to return the result.
145    stats.queryBundleStateInfoByInterval(0, 0, 20000000000000).then(res => {
146        console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise success.');
147        for (let i = 0; i < res.length; i++) {
148            console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise number : ' + (i + 1));
149            console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise result ' + JSON.stringify(res[i]));
150        }
151    }).catch(err => {
152        console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise failed, because: ' + err.code);
153    });
154
155    // Use an asynchronous callback to return the result.
156    stats.queryBundleStateInfoByInterval(0, 0, 20000000000000, (err, res) => {
157        if (err) {
158            console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback failed, because: ' + err.code);
159        } else {
160            console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback success.');
161            for (let i = 0; i < res.length; i++) {
162                console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback number : ' + (i + 1));
163                console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback result ' + JSON.stringify(res[i]));
164            }
165        }
166    });
167    ```
168
1696. Query the priority group of the current invoker application. This requires no permission to be configured in the **config.json** file.
170
171    ```js
172    import stats from '@ohos.bundleState'
173
174    // Use a promise to return the result.
175    stats.queryAppUsagePriorityGroup().then(res => {
176        console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise succeeded. result: ' + JSON.stringify(res));
177    }).catch(err => {
178        console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise failed. because: ' + err.code);
179    });
180
181    // Use an asynchronous callback to return the result.
182    stats.queryAppUsagePriorityGroup((err, res) => {
183        if (err) {
184            console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback failed. because: ' + err.code);
185        } else {
186            console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback succeeded. result: ' + JSON.stringify(res));
187        }
188    });
189    ```
190
1917. Check whether the application specified by **bundleName** is in the idle state. This requires no permission to be configured in the **config.json** file.
192
193    ```js
194    import stats from '@ohos.bundleState'
195
196    // Use a promise to return the result.
197    stats.isIdleState("com.ohos.camera").then(res => {
198        console.log('BUNDLE_ACTIVE isIdleState promise succeeded, result: ' + JSON.stringify(res));
199    }).catch(err => {
200        console.log('BUNDLE_ACTIVE isIdleState promise failed, because: ' + err.code);
201    });
202
203    // Use an asynchronous callback to return the result.
204    stats.isIdleState("com.ohos.camera", (err, res) => {
205        if (err) {
206            console.log('BUNDLE_ACTIVE isIdleState callback failed, because: ' + err.code);
207        } else {
208            console.log('BUNDLE_ACTIVE isIdleState callback succeeded, result: ' + JSON.stringify(res));
209        }
210    });
211    ```
212