device-usage-statistics-dev-guide.md 23.4 KB
Newer Older
E
ester.zhou 已提交
1
# Device Usage Statistics Development
E
ester.zhou 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

## When to Use

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
import stats from '@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).|
E
ester.zhou 已提交
22 23
| function queryAppUsagePriorityGroup(callback: AsyncCallback<number>): void | Queries the priority group of this application. This API uses an asynchronous callback to return the result.|
| function queryAppUsagePriorityGroup(): Promise<number>; | Queries the priority group of this application. This API uses a promise to return the result.|
E
ester.zhou 已提交
24
| function isIdleState(bundleName: string, callback: AsyncCallback<boolean>): void | Checks whether the application specified by **bundleName** is in the idle state. |
25 26
| function getRecentlyUsedModules(callback: AsyncCallback<BundleActiveModuleInfo>): void | Obtains the number of FA usage records specified by **1000**.|
| function getRecentlyUsedModules(maxNum : number, callback: AsyncCallback<BundleActiveModuleInfo>): void | Obtains the number of FA usage records specified by **maxNum**.|
E
ester.zhou 已提交
27 28
| function queryAppNotificationNumber(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveEventState>>): void | Queries the number of notifications from all applications based on the specified start time and end time.|
| function queryBundleActiveEventStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveEventState>>): void | Queries statistics about system events (hibernation, wakeup, unlocking, and screen locking) that occur between the specified start time and end time.|
29
| function queryAppUsagePriorityGroup(bundleName : string, callback: AsyncCallback<number>): void | Queries the priority group of the application specified by **bundleName**. This API uses an asynchronous callback to return the result.|
E
ester.zhou 已提交
30 31 32
| function queryAppUsagePriorityGroup(bundleName? : string): Promise<number>; | Queries the priority group of the application specified by **bundleName**. If **bundleName** is not specified, the priority group of the current application is queried. This API uses a promise to return the result.|
| function setBundleGroup(bundleName : string, newGroup: GroupType, callback: AsyncCallback>boolean>): void | Sets the group for the application specified by **bundleName**. This API uses an asynchronous callback to return the result.|
| function setBundleGroup(bundleName : string, newGroup : GroupType): Promise>boolean>; | Sets the group for the application specified by **bundleName**. This API uses a promise to return the result.|
33 34
| function registerGroupCallBack(groupCallback: Callback>BundleActiveGroupCallbackInfo>, callback: AsyncCallback>boolean>): void | Registers a callback for application group changes. When an application group of the user changes, the change is returned to all applications that have registered the callback. This API uses an asynchronous callback to return the result.|
| function registerGroupCallBack(groupCallback: Callback>BundleActiveGroupCallbackInfo>): Promise>boolean>; | Registers a callback for application group changes. When an application group of the user changes, the change is returned to all applications that have registered the callback. This API uses a promise to return the result.|
E
ester.zhou 已提交
35 36
| function unRegisterGroupCallBack(callback: AsyncCallback>boolean>): void | Deregisters the callback for application group changes. This API uses an asynchronous callback to return the result.|
| function unRegisterGroupCallBack(): Promise>boolean>; | Deregisters the callback for application group changes. This API uses a promise to return the result.|
E
ester.zhou 已提交
37 38 39 40 41 42 43 44 45 46 47

## How to Develop

1. Configure the device usage statistics permission in the **config.json** file.

    ```json
    "module": {
        "package": "com.example.deviceUsageStatistics",
        ...,
        "reqPermissions": [
            {
E
ester.zhou 已提交
48
                "name": "ohos.permission.BUNDLE_ACTIVE_INFO"
E
ester.zhou 已提交
49 50 51 52 53 54 55 56 57 58
            }
        ]
    }
    ```

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'

E
ester.zhou 已提交
59
    // Promise mode
E
ester.zhou 已提交
60
    stats.queryBundleActiveStates(0, 20000000000000).then(res => {
E
ester.zhou 已提交
61 62 63 64 65
        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]));
        }
E
ester.zhou 已提交
66
    }).catch(err => {
E
ester.zhou 已提交
67 68 69
        console.log('BUNDLE_ACTIVE queryBundleActiveStates promise failed, because: ' + err.code);
    });

E
ester.zhou 已提交
70
    // Asynchronous callback mode
E
ester.zhou 已提交
71
    stats.queryBundleActiveStates(0, 20000000000000, (err, res) => {
E
ester.zhou 已提交
72 73 74
        if (err) {
            console.log('BUNDLE_ACTIVE queryBundleActiveStates callback failed, because: ' + err.code);
        } else {
E
ester.zhou 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88
            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]));
            }
        }
    });
    ```

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'

E
ester.zhou 已提交
89
    // Promise mode
E
ester.zhou 已提交
90
    stats.queryBundleStateInfos(0, 20000000000000).then(res => {
E
ester.zhou 已提交
91 92
        console.log('BUNDLE_ACTIVE queryBundleStateInfos promise success.');
        let i = 1;
E
ester.zhou 已提交
93
        for (let key in res){
E
ester.zhou 已提交
94 95 96 97
            console.log('BUNDLE_ACTIVE queryBundleStateInfos promise number : ' + i);
            console.log('BUNDLE_ACTIVE queryBundleStateInfos promise result ' + JSON.stringify(res[key]));
            i++;
        }
E
ester.zhou 已提交
98
    }).catch(err => {
E
ester.zhou 已提交
99 100 101
        console.log('BUNDLE_ACTIVE queryBundleStateInfos promise failed, because: ' + err.code);
    });

E
ester.zhou 已提交
102
    // Asynchronous callback mode
E
ester.zhou 已提交
103
    stats.queryBundleStateInfos(0, 20000000000000, (err, res) => {
E
ester.zhou 已提交
104 105 106
        if (err) {
            console.log('BUNDLE_ACTIVE queryBundleStateInfos callback failed, because: ' + err.code);
        } else {
E
ester.zhou 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            console.log('BUNDLE_ACTIVE queryBundleStateInfos callback success.');
            let i = 1;
            for(let key in res){
                console.log('BUNDLE_ACTIVE queryBundleStateInfos callback number : ' + i);
                console.log('BUNDLE_ACTIVE queryBundleStateInfos callback result ' + JSON.stringify(res[key]));
                i++;
            }
        }
    });
    ```

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'

E
ester.zhou 已提交
123
    // Promise mode
E
ester.zhou 已提交
124
    stats.queryCurrentBundleActiveStates(0, 20000000000000).then(res => {
E
ester.zhou 已提交
125 126 127 128 129
        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]));
        }
E
ester.zhou 已提交
130
    }).catch(err => {
E
ester.zhou 已提交
131 132 133
        console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise failed, because: ' + err.code);
    });

E
ester.zhou 已提交
134
    // Asynchronous callback mode
E
ester.zhou 已提交
135
    stats.queryCurrentBundleActiveStates(0, 20000000000000, (err, res) => {
E
ester.zhou 已提交
136 137 138
        if (err) {
            console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback failed, because: ' + err.code);
        } else {
E
ester.zhou 已提交
139 140 141 142
            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]));
E
ester.zhou 已提交
143
            }
E
ester.zhou 已提交
144 145 146 147 148 149 150 151 152
        }
    });
    ```

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'

E
ester.zhou 已提交
153
    // Promise mode
E
ester.zhou 已提交
154
    stats.queryBundleStateInfoByInterval(0, 0, 20000000000000).then(res => {
E
ester.zhou 已提交
155 156 157 158 159
        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]));
        }
E
ester.zhou 已提交
160
    }).catch(err => {
E
ester.zhou 已提交
161 162 163
        console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise failed, because: ' + err.code);
    });

E
ester.zhou 已提交
164
    // Asynchronous callback mode
E
ester.zhou 已提交
165
    stats.queryBundleStateInfoByInterval(0, 0, 20000000000000, (err, res) => {
E
ester.zhou 已提交
166 167 168
        if (err) {
            console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback failed, because: ' + err.code);
        } else {
E
ester.zhou 已提交
169 170 171 172 173 174 175 176 177
            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]));
            }
        }
    });
    ```

E
ester.zhou 已提交
178
6. Query the priority group of the current application. This requires no permission to be configured in the **config.json** file.
E
ester.zhou 已提交
179 180 181

    ```js
    import stats from '@ohos.bundleState'
E
ester.zhou 已提交
182

E
ester.zhou 已提交
183
    // Promise mode
E
ester.zhou 已提交
184
    stats.queryAppUsagePriorityGroup().then(res => {
E
ester.zhou 已提交
185
        console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise succeeded. result: ' + JSON.stringify(res));
E
ester.zhou 已提交
186
    }).catch(err => {
E
ester.zhou 已提交
187 188
        console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise failed. because: ' + err.code);
    });
E
ester.zhou 已提交
189

E
ester.zhou 已提交
190
    // Callback mode
E
ester.zhou 已提交
191
    stats.queryAppUsagePriorityGroup((err, res) => {
E
ester.zhou 已提交
192
        if (err) {
E
ester.zhou 已提交
193
            console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback failed. because: ' + err.code);
E
ester.zhou 已提交
194 195
        } else {
            console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback succeeded. result: ' + JSON.stringify(res));
E
ester.zhou 已提交
196 197 198
        }
    });
    ```
E
ester.zhou 已提交
199

E
ester.zhou 已提交
200
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. A third-party application can only check the idle status of itself.
E
ester.zhou 已提交
201 202 203 204

    ```js
    import stats from '@ohos.bundleState'

E
ester.zhou 已提交
205
    // Promise mode
E
ester.zhou 已提交
206
    stats.isIdleState("com.ohos.camera").then(res => {
E
ester.zhou 已提交
207
        console.log('BUNDLE_ACTIVE isIdleState promise succeeded, result: ' + JSON.stringify(res));
E
ester.zhou 已提交
208
    }).catch(err => {
E
ester.zhou 已提交
209 210 211
        console.log('BUNDLE_ACTIVE isIdleState promise failed, because: ' + err.code);
    });

E
ester.zhou 已提交
212
    // Asynchronous callback mode
E
ester.zhou 已提交
213
    stats.isIdleState("com.ohos.camera", (err, res) => {
E
ester.zhou 已提交
214
        if (err) {
E
ester.zhou 已提交
215
            console.log('BUNDLE_ACTIVE isIdleState callback failed, because: ' + err.code);
E
ester.zhou 已提交
216 217
        } else {
            console.log('BUNDLE_ACTIVE isIdleState callback succeeded, result: ' + JSON.stringify(res));
E
ester.zhou 已提交
218 219 220
        }
    });
    ```
E
ester.zhou 已提交
221 222 223 224 225 226

8. Obtain the number of FA usage records specified by **maxNum**. If **maxNum** is not specified, the default value **1000** is used. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured in the **config.json** file.

    ```js
    import stats from '@ohos.bundleState'

E
ester.zhou 已提交
227
    // Promise mode
E
ester.zhou 已提交
228
    stats.getRecentlyUsedModules(1000).then(res => {
E
ester.zhou 已提交
229 230 231 232 233
        console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise succeeded');
        for (let i = 0; i < res.length; i++) {
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise number : ' + (i + 1));
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise result ' + JSON.stringify(res[i]));
        }
E
ester.zhou 已提交
234
    }).catch(err=> {
E
ester.zhou 已提交
235 236 237
        console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise failed, because: ' + err.code);
    });

E
ester.zhou 已提交
238
    // Promise mode when maxNum is not specified
E
ester.zhou 已提交
239
    stats.getRecentlyUsedModules().then(res => {
E
ester.zhou 已提交
240 241 242 243 244 245 246 247 248
        console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise succeeded');
        for (let i = 0; i < res.length; i++) {
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise number : ' + (i + 1));
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise result ' + JSON.stringify(res[i]));
        }
    }).catch( err=> {
        console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise failed, because: ' + err.code);
    });

E
ester.zhou 已提交
249
    // Asynchronous callback mode
E
ester.zhou 已提交
250
    stats.getRecentlyUsedModules(1000, (err, res) => {
E
ester.zhou 已提交
251 252 253 254 255 256 257 258 259 260 261
        if(err) {
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback failed, because: ' + err.code);
        } else {
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback succeeded.');
                for (let i = 0; i < res.length; i++) {
                    console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback number : ' + (i + 1));
                    console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback result ' + JSON.stringify(res[i]));
                }
            }
    });

E
ester.zhou 已提交
262
    // Asynchronous callback mode when maxNum is not specified
E
ester.zhou 已提交
263
    stats.getRecentlyUsedModules((err, res) => {
E
ester.zhou 已提交
264
        if (err) {
E
ester.zhou 已提交
265 266 267 268 269 270 271 272 273 274
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback failed, because: ' + err.code);
        } else {
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback succeeded.');
                for (let i = 0; i < res.length; i++) {
                    console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback number : ' + (i + 1));
                    console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback result ' + JSON.stringify(res[i]));
                }
            }
    });
    ```
E
ester.zhou 已提交
275 276 277 278 279 280 281

9. Query the number of notifications from 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'

    // Promise mode
E
ester.zhou 已提交
282
    stats.queryAppNotificationNumber(0, 20000000000000).then(res => {
E
ester.zhou 已提交
283 284
        console.log('BUNDLE_ACTIVE queryAppNotificationNumber promise success.');
        console.log('BUNDLE_ACTIVE queryAppNotificationNumber promise result ' + JSON.stringify(res));
E
ester.zhou 已提交
285
    }).catch(err => {
E
ester.zhou 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
        console.log('BUNDLE_ACTIVE queryAppNotificationNumber promise failed, because: ' + err.code);
    });

    // Asynchronous callback mode
    stats.queryAppNotificationNumber(0, 20000000000000, (err, res) => {
        if (err) {
            console.log('BUNDLE_ACTIVE queryAppNotificationNumber callback failed, because: ' + err.code);
        } else {
            console.log('BUNDLE_ACTIVE queryAppNotificationNumber callback success.');
            console.log('BUNDLE_ACTIVE queryAppNotificationNumber callback result ' + JSON.stringify(res));
        }
    });
    ```

10. Query statistics about system events (hibernation, wakeup, unlocking, and screen locking) that occur between 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'
E
ester.zhou 已提交
304

E
ester.zhou 已提交
305
    // Promise mode
E
ester.zhou 已提交
306
    stats.queryBundleActiveEventStates(0, 20000000000000).then(res => {
E
ester.zhou 已提交
307 308
        console.log('BUNDLE_ACTIVE queryBundleActiveEventStates promise success.');
        console.log('BUNDLE_ACTIVE queryBundleActiveEventStates promise result ' + JSON.stringify(res));
E
ester.zhou 已提交
309
    }).catch(err => {
E
ester.zhou 已提交
310 311
        console.log('BUNDLE_ACTIVE queryBundleActiveEventStates promise failed, because: ' + err.code);
    });
E
ester.zhou 已提交
312

E
ester.zhou 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    // Asynchronous callback mode
    stats.queryBundleActiveEventStates(0, 20000000000000, (err, res) => {
        if (err) {
            console.log('BUNDLE_ACTIVE queryBundleActiveEventStates callback failed, because: ' + err.code);
        } else {
            console.log('BUNDLE_ACTIVE queryBundleActiveEventStates callback success.');
            console.log('BUNDLE_ACTIVE queryBundleActiveEventStates callback result ' + JSON.stringify(res));
        }
    });
    ```

11. Query the priority group of the current application. This requires no permission to be configured in the **config.json** file. Query the priority group of a specified application. This requires the **ohos.permission.BUNDLE_ACTIVE_INFO** permission to be configured in the **config.json** file.

     ```js
     import stats from '@ohos.bundleState'
E
ester.zhou 已提交
328

E
ester.zhou 已提交
329
     // Promise mode without parameters
E
ester.zhou 已提交
330
     stats.queryAppUsagePriorityGroup().then(res => {
E
ester.zhou 已提交
331
         console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise succeeded. result: ' + JSON.stringify(res));
E
ester.zhou 已提交
332
     }).catch(err => {
E
ester.zhou 已提交
333 334
         console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise failed. because: ' + err.code);
     });
E
ester.zhou 已提交
335

E
ester.zhou 已提交
336 337 338 339 340 341 342 343
     // Asynchronous callback mode without parameters
     stats.queryAppUsagePriorityGroup((err, res) => {
         if (err) {
             console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback failed. because: ' + err.code);
         } else {
             console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback succeeded. result: ' + JSON.stringify(res));
         }
     });
E
ester.zhou 已提交
344

E
ester.zhou 已提交
345
     // Promise mode with parameters
E
ester.zhou 已提交
346
     stats.queryAppUsagePriorityGroup(this.bundleName).then(res => {
E
ester.zhou 已提交
347
         console.log('BUNDLE_ACTIVE QueryPackageGroup promise succeeded. result: ' + JSON.stringify(res));
E
ester.zhou 已提交
348
     }).catch(err => {
E
ester.zhou 已提交
349 350
         console.log('BUNDLE_ACTIVE QueryPackageGroup promise failed. because: ' + err.code);
     });
E
ester.zhou 已提交
351

E
ester.zhou 已提交
352 353
     // Asynchronous callback mode with parameters
     stats.queryAppUsagePriorityGroup(this.bundleName, (err, res) => {
E
ester.zhou 已提交
354
         if (err) {
E
ester.zhou 已提交
355 356 357 358 359 360 361 362 363 364 365
             console.log('BUNDLE_ACTIVE QueryPackageGroup callback failed. because: ' + err.code);
         } else {
             console.log('BUNDLE_ACTIVE QueryPackageGroup callback succeeded. result: ' + JSON.stringify(res));
         }
     });
     ```

11. Set the group for the application specified by **bundleName**.

    ```javascript
    import stats from '@ohos.bundleState'
E
ester.zhou 已提交
366

E
ester.zhou 已提交
367
    // Promise mode
E
ester.zhou 已提交
368
    stats.setBundleGroup(this.bundleName, this.newGroup).then(() => {
E
ester.zhou 已提交
369 370 371 372 373 374
        console.log('BUNDLE_ACTIVE SetBundleGroup promise succeeded.');
    }).catch( err => {
        console.log('BUNDLE_ACTIVE SetBundleGroup promise failed. because: ' + err.code);
    });
    // Asynchronous callback mode
    stats.setBundleGroup(this.bundleName, this.newGroup, (err) => {
E
ester.zhou 已提交
375
        if (err) {
E
ester.zhou 已提交
376 377 378 379 380 381 382 383 384 385 386
            console.log('BUNDLE_ACTIVE SetBundleGroup callback failed. because: ' + err.code);
        } else {
            console.log('BUNDLE_ACTIVE SetBundleGroup callback succeeded.');
        }
    });
    ```

12. Register a callback for application group changes. When an application group of the user changes, the change is returned to all applications that have registered the callback.

    ```javascript
    import stats from '@ohos.bundleState'
E
ester.zhou 已提交
387

E
ester.zhou 已提交
388
    // Promise mode
E
ester.zhou 已提交
389
    let onBundleGroupChanged = (err,res) => {
E
ester.zhou 已提交
390 391 392 393 394 395 396
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack callback success.');
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result oldGroup is : ' + res.oldGroup);
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result newGroup is : ' + res.newGroup);
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result changeReason is : ' + res.newGroup);
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result userId is : ' + res.userId);
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result bundleName is : ' + res.bundleName);
    };
E
ester.zhou 已提交
397
    stats.registerGroupCallBack(onBundleGroupChanged).then(() => {
E
ester.zhou 已提交
398
        console.log('BUNDLE_ACTIVE RegisterGroupCallBack promise succeeded.');
E
ester.zhou 已提交
399
    }).catch(err => {
E
ester.zhou 已提交
400 401 402
        console.log('BUNDLE_ACTIVE RegisterGroupCallBack promise failed. because: ' + err.code);
    });
    // Asynchronous callback mode
E
ester.zhou 已提交
403
    let onBundleGroupChanged = (err,res) => {
E
ester.zhou 已提交
404 405 406 407 408 409 410
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack callback success.');
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result's oldGroup is : ' + res.oldGroup);
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result's newGroup is : ' + res.newGroup);
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result's changeReason is : ' + res.newGroup);
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result's userId is : ' + res.userId);
        console.log('BUNDLE_ACTIVE onBundleGroupChanged RegisterGroupCallBack result's bundleName is : ' + res.bundleName);
    };
E
ester.zhou 已提交
411 412
    stats.registerGroupCallBack(onBundleGroupChanged, (err) => {
        if (err) {
E
ester.zhou 已提交
413 414 415 416 417 418
            console.log('BUNDLE_ACTIVE RegisterGroupCallBack callback failed, because: ' + err.code);
        } else {
            console.log('BUNDLE_ACTIVE RegisterGroupCallBack callback success.');
        }
    });
    ```
E
ester.zhou 已提交
419

E
ester.zhou 已提交
420 421 422 423
13. Deregister the callback for application group changes.

    ```javascript
    import stats from '@ohos.bundleState'
E
ester.zhou 已提交
424

425
    // Promise mode
E
ester.zhou 已提交
426
    stats.unRegisterGroupCallBack().then(() => {
E
ester.zhou 已提交
427
        console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack promise succeeded.');
E
ester.zhou 已提交
428
    }).catch(err => {
E
ester.zhou 已提交
429 430
        console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack promise failed. because: ' + err.code);
    });
431
    // Asynchronous callback mode
E
ester.zhou 已提交
432 433
    stats.unRegisterGroupCallBack((err) => {
        if (err) {
E
ester.zhou 已提交
434 435 436 437 438 439
            console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack callback failed, because: ' + err.code);
        } else {
            console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack callback success.');
        }
    });
    ```