device-usage-statistics-dev-guide.md 21.2 KB
Newer Older
1
# 设备使用信息统计
W
wyuanchao 已提交
2

W
wyuanchao 已提交
3
## 场景介绍
W
wyuanchao 已提交
4

W
wyuanchao 已提交
5 6 7 8 9 10
设备使用信息统计,包括app usage/notification usage/system usage等使用统计。例如应用使用信息统计,用于保存和查询应用使用详情(app usage)、事件日志数据(event log)、应用分组(bundle group)情况。
部件缓存的应用记录(使用历史统计和使用事件记录)会在事件上报后30分钟内刷新到数据库持久化保存。

## 接口说明
注册相关接口包导入:
```js
W
wyuanchao 已提交
11
import stats from '@ohos.bundleState';
W
wyuanchao 已提交
12
```
W
wyuanchao 已提交
13 14 15 16 17

**表1** 设备使用信息统计主要接口

| 接口名 | 描述 |
| -------- | -------- |
W
wyuanchao 已提交
18 19 20 21
| function queryBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void | 通过指定起始和结束时间查询所有应用的事件集合。 |
| function queryBundleStateInfos(begin: number, end: number, callback: AsyncCallback<BundleActiveInfoResponse>): void | 通过指定起始和结束时间查询应用使用时长统计信息。 |
| function queryCurrentBundleActiveStates(begin: number, end: number, callback: AsyncCallback<Array<BundleActiveState>>): void | 通过指定起始和结束时间查询当前应用的事件集合。 |
| function queryBundleStateInfoByInterval(byInterval: IntervalType, begin: number, end: number, callback: AsyncCallback<Array<BundleStateInfo>>): void | 通过指定时间段间隔(天、周、月、年)查询应用使用时长统计信息。 |
Y
yupeng74@huawei.com 已提交
22 23
| function queryAppUsagePriorityGroup(bundleName? : string, callback: AsyncCallback<number>): void | 查询当前调用者应用或者指定应用的使用优先级群组。callback形式。 |
| function queryAppUsagePriorityGroup(bundleName? : string): Promise<number>; | 查询当前调用者应用或者指定应用的使用优先级群组。promise形式。 |
W
wyuanchao 已提交
24
| function isIdleState(bundleName: string, callback: AsyncCallback&lt;boolean&gt;): void | 判断指定Bundle Name的应用当前是否是空闲状态。 |
H
houdisheng 已提交
25
| function getRecentlyUsedModules(maxNum: number, callback: AsyncCallback&lt;BundleActiveModuleInfo&gt;): void | 根据maxNum,查询FA使用记录,返回不超过maxNum条FA使用记录。 |
W
wyuanchao 已提交
26 27
| function queryAppNotificationNumber(begin: number, end: number, callback: AsyncCallback&lt;Array&lt;BundleActiveEventState&gt;&gt;): void | 通过指定起始和结束时间查询所有应用的通知次数。 |
| function queryBundleActiveEventStates(begin: number, end: number, callback: AsyncCallback&lt;Array&lt;BundleActiveEventState&gt;&gt;): void | 通过指定起始和结束时间查询系统事件(休眠、唤醒、解锁、锁屏)统计信息。 |
Y
yupeng74@huawei.com 已提交
28 29 30 31 32 33
| function setBundleGroup(bundleName : string, newGroup: GroupType, callback: AsyncCallback<boolean>): void | 给应用名是bundleName的应用分组设置成newGroup,返回设置结果是否成功,以callback形式返回。 |
| function setBundleGroup(bundleName : string, newGroup : GroupType): Promise<boolean>; | 给应用名是bundleName的应用分组设置成newGroup,返回设置结果是否成功,以promise形式返回。 |
| function registerGroupCallBack(callback: Callback<BundleActiveGroupCallbackInfo>, callback: AsyncCallback<boolean>): void | 注册应用分组变化监听回调,返回注册是否成功,当应用分组发生变化时,会给所有已注册的监听者返回回调信息,以callback形式返回。 |
| function registerGroupCallBack(callback: Callback<BundleActiveGroupCallbackInfo>): Promise<boolean>; | 注册应用分组变化监听回调,返回注册是否成功,当应用分组发生变化时,会给所有已注册的监听者返回回调信息,以promise形式返回。 |
| function unRegisterGroupCallBack(callback: AsyncCallback<boolean>): void | 解除应用分组监听回调,以callback形式返回。 |
| function unRegisterGroupCallBack(): Promise<boolean>; | 解除应用分组监听回调,以promise形式返回。 |
W
wyuanchao 已提交
34 35 36

## 开发步骤

W
wyuanchao 已提交
37
1. 在config.json文件中配置设备使用信息统计权限。
W
wyuanchao 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50

    ```json
    "module": {
        "package": "com.example.deviceUsageStatistics",
        ...,
        "reqPermissions": [
            {
            "name": "ohos.permission.BUNDLE_ACTIVE_INFO"
            }
        ]
    }
    ```

W
wyuanchao 已提交
51
2. 通过指定起始和结束时间查询所有应用的事件集合,config.json中需要配置权限:ohos.permission.BUNDLE_ACTIVE_INFO。
W
wyuanchao 已提交
52 53

    ```js
W
wyuanchao 已提交
54
    import stats from '@ohos.bundleState'
W
wyuanchao 已提交
55 56 57

    // 异步方法promise方式
    stats.queryBundleActiveStates(0, 20000000000000).then( res => {
W
wyuanchao 已提交
58
        console.log('BUNDLE_ACTIVE queryBundleActiveStates promise success.');
W
wyuanchao 已提交
59
        for (let i = 0; i < res.length; i++) {
W
wyuanchao 已提交
60 61
            console.log('BUNDLE_ACTIVE queryBundleActiveStates promise number : ' + (i + 1));
            console.log('BUNDLE_ACTIVE queryBundleActiveStates promise result ' + JSON.stringify(res[i]));
W
wyuanchao 已提交
62 63
        }
    }).catch( err => {
W
wyuanchao 已提交
64
        console.log('BUNDLE_ACTIVE queryBundleActiveStates promise failed, because: ' + err.code);
W
wyuanchao 已提交
65 66 67
    });

    // 异步方法callback方式
W
wyuanchao 已提交
68
    stats.queryBundleActiveStates(0, 20000000000000, (err, res) => {
W
wyuanchao 已提交
69 70 71
        if (err) {
            console.log('BUNDLE_ACTIVE queryBundleActiveStates callback failed, because: ' + err.code);
        } else {
W
wyuanchao 已提交
72 73 74 75
            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]));
W
wyuanchao 已提交
76
            }
W
wyuanchao 已提交
77 78
        }
    });
W
wyuanchao 已提交
79 80
    ```

W
wyuanchao 已提交
81
3. 通过指定起始和结束时间查询应用使用时长统计信息,config.json中需要配置权限:ohos.permission.BUNDLE_ACTIVE_INFO。
W
wyuanchao 已提交
82 83

    ```js
W
wyuanchao 已提交
84
    import stats from '@ohos.bundleState'
W
wyuanchao 已提交
85 86 87

    // 异步方法promise方式
    stats.queryBundleStateInfos(0, 20000000000000).then( res => {
W
wyuanchao 已提交
88 89
        console.log('BUNDLE_ACTIVE queryBundleStateInfos promise success.');
        let i = 1;
W
wyuanchao 已提交
90
        for (let key in res){
W
wyuanchao 已提交
91 92 93
            console.log('BUNDLE_ACTIVE queryBundleStateInfos promise number : ' + i);
            console.log('BUNDLE_ACTIVE queryBundleStateInfos promise result ' + JSON.stringify(res[key]));
            i++;
W
wyuanchao 已提交
94 95
        }
    }).catch( err => {
W
wyuanchao 已提交
96
        console.log('BUNDLE_ACTIVE queryBundleStateInfos promise failed, because: ' + err.code);
W
wyuanchao 已提交
97 98 99
    });

    // 异步方法callback方式
W
wyuanchao 已提交
100
    stats.queryBundleStateInfos(0, 20000000000000, (err, res) => {
W
wyuanchao 已提交
101 102 103
        if (err) {
            console.log('BUNDLE_ACTIVE queryBundleStateInfos callback failed, because: ' + err.code);
        } else {
W
wyuanchao 已提交
104 105 106 107 108 109
            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++;
W
wyuanchao 已提交
110
            }
W
wyuanchao 已提交
111 112
        }
    });
W
wyuanchao 已提交
113 114
    ```

W
wyuanchao 已提交
115
4. 通过指定起始和结束时间查询当前应用的事件集合,config.json中不需要配置权限。
W
wyuanchao 已提交
116 117

    ```js
W
wyuanchao 已提交
118
    import stats from '@ohos.bundleState'
W
wyuanchao 已提交
119 120 121

    // 异步方法promise方式
    stats.queryCurrentBundleActiveStates(0, 20000000000000).then( res => {
W
wyuanchao 已提交
122
        console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise success.');
W
wyuanchao 已提交
123
        for (let i = 0; i < res.length; i++) {
W
wyuanchao 已提交
124 125
            console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise number : ' + (i + 1));
            console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise result ' + JSON.stringify(res[i]));
W
wyuanchao 已提交
126 127
        }
    }).catch( err => {
W
wyuanchao 已提交
128
        console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates promise failed, because: ' + err.code);
W
wyuanchao 已提交
129 130 131
    });

    // 异步方法callback方式
W
wyuanchao 已提交
132
    stats.queryCurrentBundleActiveStates(0, 20000000000000, (err, res) => {
W
wyuanchao 已提交
133 134 135
        if (err) {
            console.log('BUNDLE_ACTIVE queryCurrentBundleActiveStates callback failed, because: ' + err.code);
        } else {
W
wyuanchao 已提交
136 137 138 139
            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]));
W
wyuanchao 已提交
140
            }
W
wyuanchao 已提交
141 142
        }
    });
W
wyuanchao 已提交
143 144
    ```

W
wyuanchao 已提交
145
5. 通过指定时间段间隔(天、周、月、年)查询应用使用时长统计信息,config.json中需要配置权限:ohos.permission.BUNDLE_ACTIVE_INFO。
W
wyuanchao 已提交
146 147

    ```js
W
wyuanchao 已提交
148
    import stats from '@ohos.bundleState'
W
wyuanchao 已提交
149 150 151

    // 异步方法promise方式
    stats.queryBundleStateInfoByInterval(0, 0, 20000000000000).then( res => {
W
wyuanchao 已提交
152
        console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise success.');
W
wyuanchao 已提交
153
        for (let i = 0; i < res.length; i++) {
W
wyuanchao 已提交
154 155
            console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise number : ' + (i + 1));
            console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise result ' + JSON.stringify(res[i]));
W
wyuanchao 已提交
156 157
        }
    }).catch( err => {
W
wyuanchao 已提交
158
        console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval promise failed, because: ' + err.code);
W
wyuanchao 已提交
159 160 161
    });

    // 异步方法callback方式
W
wyuanchao 已提交
162
    stats.queryBundleStateInfoByInterval(0, 0, 20000000000000, (err, res) => {
W
wyuanchao 已提交
163 164 165
        if (err) {
            console.log('BUNDLE_ACTIVE queryBundleStateInfoByInterval callback failed, because: ' + err.code);
        } else {
W
wyuanchao 已提交
166 167 168 169
            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]));
W
wyuanchao 已提交
170
            }
W
wyuanchao 已提交
171 172
        }
    });
W
wyuanchao 已提交
173 174
    ```

Y
yupeng74@huawei.com 已提交
175
6. 查询(无参)当前调用者应用的使用优先级群组,config.json中不需要配置权限。查询(有参)指定应用的使用优先级群组,config.json中需要配置权限:ohos.permission.BUNDLE_ACTIVE_INFO。
W
wyuanchao 已提交
176 177

    ```js
W
wyuanchao 已提交
178
    import stats from '@ohos.bundleState'
Y
yupeng74@huawei.com 已提交
179 180
    
    // 无参异步方法promise方式
W
wyuanchao 已提交
181
    stats.queryAppUsagePriorityGroup().then( res => {
W
wyuanchao 已提交
182
        console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise succeeded. result: ' + JSON.stringify(res));
W
wyuanchao 已提交
183
    }).catch( err => {
W
wyuanchao 已提交
184
        console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup promise failed. because: ' + err.code);
W
wyuanchao 已提交
185
    });
Y
yupeng74@huawei.com 已提交
186 187
    
    // 无参异步方法callback方式
W
wyuanchao 已提交
188
    stats.queryAppUsagePriorityGroup((err, res) => {
W
wyuanchao 已提交
189
        if (err) {
W
wyuanchao 已提交
190
            console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback failed. because: ' + err.code);
W
wyuanchao 已提交
191 192
        } else {
            console.log('BUNDLE_ACTIVE queryAppUsagePriorityGroup callback succeeded. result: ' + JSON.stringify(res));
W
wyuanchao 已提交
193 194
        }
    });
Y
yupeng74@huawei.com 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    
    //有参异步promise方式
    stats.queryAppUsagePriorityGroup(this.bundleName).then( res => {
        console.log('BUNDLE_ACTIVE QueryPackageGroup promise succeeded. result: ' + JSON.stringify(res));
    }).catch( err => {
        console.log('BUNDLE_ACTIVE QueryPackageGroup promise failed. because: ' + err.code);
    });
    
    //有参异步方法callback方式
    stats.queryAppUsagePriorityGroup(this.bundleName, (err, res) => {
        if(err) {
            console.log('BUNDLE_ACTIVE QueryPackageGroup callback failed. because: ' + err.code);
        } else {
            console.log('BUNDLE_ACTIVE QueryPackageGroup callback succeeded. result: ' + JSON.stringify(res));
        }
    });
W
wyuanchao 已提交
211 212
    ```

W
wyuanchao 已提交
213
7. 判断指定Bundle Name的应用当前是否是空闲状态,config.json中不需要配置权限。
W
wyuanchao 已提交
214 215

    ```js
W
wyuanchao 已提交
216
    import stats from '@ohos.bundleState'
W
wyuanchao 已提交
217 218 219

    // 异步方法promise方式
    stats.isIdleState("com.ohos.camera").then( res => {
W
wyuanchao 已提交
220
        console.log('BUNDLE_ACTIVE isIdleState promise succeeded, result: ' + JSON.stringify(res));
W
wyuanchao 已提交
221
    }).catch( err => {
W
wyuanchao 已提交
222
        console.log('BUNDLE_ACTIVE isIdleState promise failed, because: ' + err.code);
W
wyuanchao 已提交
223 224 225
    });

    // 异步方法callback方式
W
wyuanchao 已提交
226
    stats.isIdleState("com.ohos.camera", (err, res) => {
W
wyuanchao 已提交
227
        if (err) {
W
wyuanchao 已提交
228
            console.log('BUNDLE_ACTIVE isIdleState callback failed, because: ' + err.code);
W
wyuanchao 已提交
229 230
        } else {
            console.log('BUNDLE_ACTIVE isIdleState callback succeeded, result: ' + JSON.stringify(res));
W
wyuanchao 已提交
231 232
        }
    });
H
houdisheng 已提交
233 234
    ```

H
houdisheng 已提交
235
8. 查询FA使用记录。返回数量最大不超过maxNum设置的值,若不传入maxNum参数,则默认maxNum为1000。config.json中需要配置权限:ohos.permission.BUNDLE_ACTIVE_INFO。
H
houdisheng 已提交
236 237 238 239 240

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

    // 异步方法promise方式
H
houdisheng 已提交
241
    stats.getRecentlyUsedModules(1000).then( res => {
H
houdisheng 已提交
242
        console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise succeeded');
H
houdisheng 已提交
243
        for (let i = 0; i < res.length; i++) {
H
houdisheng 已提交
244 245
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise number : ' + (i + 1));
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise result ' + JSON.stringify(res[i]));
H
houdisheng 已提交
246 247
        }
    }).catch( err=> {
H
houdisheng 已提交
248
        console.log('BUNDLE_ACTIVE getRecentlyUsedModules promise failed, because: ' + err.code);
H
houdisheng 已提交
249 250
    });

H
houdisheng 已提交
251
    // 无maNum参数异步方法promise方式
H
houdisheng 已提交
252 253 254 255 256 257 258 259 260 261
    stats.getRecentlyUsedModules().then( res => {
        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);
    });

H
houdisheng 已提交
262
    // 异步方法callback方式
H
houdisheng 已提交
263
    stats.getRecentlyUsedModules(1000,(err, res) => {
H
houdisheng 已提交
264
        if(err) {
H
houdisheng 已提交
265
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback failed, because: ' + err.code);
H
houdisheng 已提交
266
        } else {
H
houdisheng 已提交
267
            console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback succeeded.');
H
houdisheng 已提交
268
                for (let i = 0; i < res.length; i++) {
H
houdisheng 已提交
269 270
                    console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback number : ' + (i + 1));
                    console.log('BUNDLE_ACTIVE getRecentlyUsedModules callback result ' + JSON.stringify(res[i]));
H
houdisheng 已提交
271 272 273
                }
            }
    });
H
houdisheng 已提交
274

H
houdisheng 已提交
275
    // 无maNum参数异步方法callback方式
H
houdisheng 已提交
276 277 278 279 280 281 282 283 284 285 286
    stats.getRecentlyUsedModules((err, res) => {
        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]));
                }
            }
    });
H
houdisheng 已提交
287 288
    ```

W
wyuanchao 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
9. 通过指定起始和结束时间查询所有应用的通知次数,config.json中需要配置权限:ohos.permission.BUNDLE_ACTIVE_INFO。

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

    // 异步方法promise方式
    stats.queryAppNotificationNumber(0, 20000000000000).then( res => {
        console.log('BUNDLE_ACTIVE queryAppNotificationNumber promise success.');
        console.log('BUNDLE_ACTIVE queryAppNotificationNumber promise result ' + JSON.stringify(res));
    }).catch( err => {
        console.log('BUNDLE_ACTIVE queryAppNotificationNumber promise failed, because: ' + err.code);
    });

    // 异步方法callback方式
    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. 通过指定起始和结束时间查询系统事件(休眠、唤醒、解锁、锁屏)统计信息,config.json中需要配置权限:ohos.permission.BUNDLE_ACTIVE_INFO。

    ```js
    import stats from '@ohos.bundleState'
Y
yupeng74@huawei.com 已提交
317
    
W
wyuanchao 已提交
318 319 320 321 322 323 324
    // 异步方法promise方式
    stats.queryBundleActiveEventStates(0, 20000000000000).then( res => {
        console.log('BUNDLE_ACTIVE queryBundleActiveEventStates promise success.');
        console.log('BUNDLE_ACTIVE queryBundleActiveEventStates promise result ' + JSON.stringify(res));
    }).catch( err => {
        console.log('BUNDLE_ACTIVE queryBundleActiveEventStates promise failed, because: ' + err.code);
    });
Y
yupeng74@huawei.com 已提交
325
    
W
wyuanchao 已提交
326 327 328 329 330 331 332 333 334
    // 异步方法callback方式
    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));
        }
    });
Y
yupeng74@huawei.com 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
    ```

11. 给应用名是bundleName的应用分组设置成newGroup,返回设置结果是否成功

    ```javascript
    import stats from '@ohos.bundleState'
    
    //异步方法promise
    stats.setBundleGroup(this.bundleName, this.newGroup).then( res => {
        console.log('BUNDLE_ACTIVE SetBundleGroup promise succeeded. result: ' + JSON.stringify(res));
    }).catch( err => {
        console.log('BUNDLE_ACTIVE SetBundleGroup promise failed. because: ' + err.code);
    });
    //异步方法callback
    stats.setBundleGroup(this.bundleName, this.newGroup, (err, res) => {
        if(err) {
            console.log('BUNDLE_ACTIVE SetBundleGroup callback failed. because: ' + err.code);
        } else {
            console.log('BUNDLE_ACTIVE SetBundleGroup callback succeeded. result: ' + JSON.stringify(res));
        }
    });
    ```

12. 注册应用分组变化监听回调,返回注册是否成功,当应用分组发生变化时,会给所有已注册的监听者返回回调信息

    ```javascript
    import stats from '@ohos.bundleState'
    
    //异步方法promise形式
    let onBundleGroupChanged = (err,res) =>{
        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);
    };
    stats.registerGroupCallBack(onBundleGroupChanged).then( res => {
        console.log('BUNDLE_ACTIVE RegisterGroupCallBack promise succeeded. result1: ' + JSON.stringify(res));
    }).catch( err => {
        console.log('BUNDLE_ACTIVE RegisterGroupCallBack promise failed. because: ' + err.code);
    });
    //异步方法callback形式
    let onBundleGroupChanged = (err,res) =>{
        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);
    };
    stats.registerGroupCallBack(onBundleGroupChanged, (err,res)=>{
        if(err) {
            console.log('BUNDLE_ACTIVE RegisterGroupCallBack callback failed, because: ' + err.code);
        } else {
            console.log('BUNDLE_ACTIVE RegisterGroupCallBack callback success.');
            console.log('BUNDLE_ACTIVE RegisterGroupCallBack result is : ' + JSON.stringify(res));
        }
    });
    ```

13. 解除应用分组监听回调

    ```javascript
    import stats from '@ohos.bundleState'
    
    //promise
    stats.unRegisterGroupCallBack().then( res => {
        console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack promise succeeded. result: ' + JSON.stringify(res));
    }).catch( err => {
        console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack promise failed. because: ' + err.code);
    });
    //callback
    stats.unRegisterGroupCallBack((err,res)=>{
        if(err) {
            console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack callback failed, because: ' + err.code);
        } else {
            console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack callback success.');
            console.log('BUNDLE_ACTIVE UnRegisterGroupCallBack result is : ' + JSON.stringify(res));
        }
    });
    ```