AppGallery Kit enables apps to query custom icon information, switch to a custom icon, and revert to the default icon.
5.0.3(15) and later versions support dynamic icon switching.
Querying information about custom icons
Query information about available custom icons in your app.
Switching to a custom icon
Select an available custom icon that will be used by your app.
Reverting to the default icon
Stop using the selected custom icon and revert to the default icon.
A user requests to query information about available custom icons.
The app calls the queryDynamicIcons API to obtain information about custom icons.
The API returns information about the custom icons to your app.
Your app returns the result to the user.
A user requests to switch to a custom icon.
The app calls the selectDynamicIcon API to switch to a custom icon.
The API returns the selection result to your app.
Your app returns the result to the user.
A user requests to revert to the default icon.
The app calls the disableDynamicIcon API to disable the custom icon.
The API returns the result to your app.
Your app returns the result to the user.
The icon management service does not support the Emulator. Use a real device for debugging.
The icon management service is supported on phones, tablets, and PCs/2-in-1 devices. Support for wearables is available starting from 5.1.0(18), and for TVs starting from 5.1.1(19).
The icon management service provides the following APIs. For details, please refer to the API reference.
| API | Description |
|---|---|
| queryDynamicIcons(): Promise<DynamicIconInfo[]> | Queries information about custom icons. |
| selectDynamicIcon(iconId: string): Promise<void> | Switches to a custom icon. |
| disableDynamicIcon(): Promise<void> | Disables a custom icon and reverts to the default icon. |
From version 6.0.0(20), the selectDynamicIcon API can return error code 1006800013.
Import the appInfoManager module and related common modules.
import { hilog } from '@kit.PerformanceAnalysisKit';
import { appInfoManager } from '@kit.AppGalleryKit';
import { BusinessError } from '@kit.BasicServicesKit'; Call the queryDynamicIcons API to obtain information about custom icons.
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}`);
// ...
} Import the appInfoManager module and related common modules.
import { hilog } from '@kit.PerformanceAnalysisKit';
import { appInfoManager } from '@kit.AppGalleryKit';
import { BusinessError } from '@kit.BasicServicesKit'; Call the selectDynamicIcon API to switch to a custom icon.
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}`);
// ...
}
} Import the appInfoManager module and related common modules.
import { hilog } from '@kit.PerformanceAnalysisKit';
import { appInfoManager } from '@kit.AppGalleryKit';
import { BusinessError } from '@kit.BasicServicesKit'; Call the disableDynamicIcon API to revert to the default icon.
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}`);
// ...
}