目录

实现应用图标动态切换

AppGallery Kit为使用动态图标的应用客户端提供查询动态图标信息、切换动态图标、恢复默认图标功能。

从版本5.0.3(15)开始,支持实现应用图标动态切换。

场景介绍

业务流程

[h2]查询动态图标信息

  1. 用户查询可选的动态图标信息。

  2. 调用接口queryDynamicIcons获取动态图标信息。

  3. 接口返回动态图标信息给应用。

  4. 应用返回结果给用户。

[h2]切换动态图标

  1. 用户需要切换动态图标。

  2. 调用接口selectDynamicIcon切换动态图标。

  3. 接口返回选择结果给应用。

  4. 应用返回结果给用户。

[h2]恢复默认图标

  1. 用户需要恢复默认图标。

  2. 调用接口disableDynamicIcon禁用动态图标。

  3. 接口返回禁用结果给应用。

  4. 应用返回结果给用户。

约束与限制

接口说明

图标管理服务提供以下接口,具体API说明详见接口文档。

接口名 描述
queryDynamicIcons(): Promise<DynamicIconInfo[]> 查询动态图标信息接口,用于查询动态图标信息。
selectDynamicIcon(iconId: string): Promise<void> 切换动态图标接口,用于切换动态图标。
disableDynamicIcon(): Promise<void> 禁用动态图标接口,用于停止动态图标,恢复默认图标。

从版本6.0.0(20)开始,切换动态图标接口支持返回1006800013错误码。

开发步骤

[h2]查询动态图标信息

  1. 导入appInfoManager模块及相关公共模块。

    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { appInfoManager } from '@kit.AppGalleryKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. 调用queryDynamicIcons方法查询动态图标信息。

    try {
        appInfoManager.queryDynamicIcons().then((iconInfos: appInfoManager.DynamicIconInfo[]) => {
            hilog.info(0, TAG, `queryDynamicIcons success. iconInfos: ${JSON.stringify(iconInfos)}`);
            // ...
        }).catch((error: BusinessError) => {
            hilog.error(0, TAG,
                `queryDynamicIcons failed, code: ${error.code}, exception message: ${error.message}`);
            // ...
        })
    } catch (error) {
        hilog.error(0, TAG,
            `queryDynamicIcons exception, code: ${error.code}, exception message: ${error.message}`);
        // ...
    }

[h2]切换动态图标

  1. 导入appInfoManager模块及相关公共模块。

    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { appInfoManager } from '@kit.AppGalleryKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. 调用selectDynamicIcon方法切换动态图标。

    public selectDynamicIcon(iconId: string) {
        try {
            appInfoManager.selectDynamicIcon(iconId).then(() => {
                hilog.info(0, TAG, `selectDynamicIcon success. iconId: ${iconId}`);
                // ...
            }).catch((error: BusinessError) => {
                hilog.error(0, TAG,
                    `selectDynamicIcon failed, code: ${error.code}, exception message: ${error.message}`);
                // ...
            });
        } catch (error) {
            hilog.error(0, TAG,
                `selectDynamicIcon exception, code: ${error.code}, exception message: ${error.message}`);
            // ...
        }
    }

[h2]恢复默认图标

  1. 导入appInfoManager模块及相关公共模块。

    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { appInfoManager } from '@kit.AppGalleryKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. 调用disableDynamicIcon方法恢复默认图标。

    try {
        appInfoManager.disableDynamicIcon().then(() => {
            hilog.info(0, TAG, `disableDynamicIcon success.`);
            // ...
        }).catch((error: BusinessError) => {
            hilog.error(0, TAG,
                `disableDynamicIcon failed, code: ${error.code}, exception message: ${error.message}`);
            // ...
        });
    } catch (error) {
        hilog.error(0, TAG,
            `disableDynamicIcon exception, code: ${error.code}, exception message: ${error.message}`);
        // ...
    }