harmony 鸿蒙@ohos.wallpaper (壁纸)

  • 2022-08-09
  • 浏览 (782)

@ohos.wallpaper (壁纸)

壁纸管理服务是OpenHarmony中的系统服务,主要为系统提供壁纸管理服务能力,支持系统显示、设置、切换壁纸等功能。

说明:

本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import wallpaper from '@ohos.wallpaper';

WallpaperResourceType10+

定义壁纸资源的枚举类型。

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

名称 说明
DEFAULT 0 默认为图片资源。
PICTURE 1 图片资源。
VIDEO 2 视频资源。
PACKAGE 3 包资源。

WallpaperType7+

定义壁纸的枚举类型。

系统能力: SystemCapability.MiscServices.Wallpaper

名称 说明
WALLPAPER_SYSTEM 0 主屏幕壁纸标识。
WALLPAPER_LOCKSCREEN 1 锁屏壁纸标识。

RgbaColor(deprecated)

定义壁纸颜色信息结构。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

名称 类型 可读 可写 说明
red number 表示红色值,范围为 0 到 255。
green number 表示绿色值,范围为 0 到 255。
blue number 表示蓝色值,范围为 0 到 255。
alpha number 表示 alpha 值,范围为 0 到 255。

wallpaper.setVideo10+

setVideo(source: string, wallpaperType: WallpaperType, callback: AsyncCallback<void>): void

将视频资源设置为桌面或锁屏的动态壁纸。使用callback异步回调。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
source string mp4文件的Uri路径。
wallpaperType WallpaperType 壁纸类型。
callback AsyncCallback<void> 回调函数,设置壁纸成功,error为undefined,否则返回error信息。

示例:

import { BusinessError } from '@ohos.base';

let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.mp4";
try {
    wallpaper.setVideo(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
        if (error) {
            console.error(`failed to setVideo because: ${JSON.stringify(error)}`);
            return;
        }
        console.log(`success to setVideo.`);
    });
} catch (error) {
    console.error(`failed to setVideo because: ${JSON.stringify(error)}`);
}

wallpaper.setVideo10+

setVideo(source: string, wallpaperType: WallpaperType): Promise<void>

将视频资源设置为桌面或锁屏的动态壁纸。使用promise异步回调。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
source string mp4文件的Uri路径。
wallpaperType WallpaperType 壁纸类型。

返回值:

类型 说明
Promise<void> 无返回结果的Promise对象。

示例:

import { BusinessError } from '@ohos.base';

let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.mp4";
try {
    wallpaper.setVideo(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
        console.log(`success to setVideo.`);
    }).catch((error: BusinessError) => {
        console.error(`failed to setVideo because: ${JSON.stringify(error)}`);
    });
} catch (error) {
    console.error(`failed to setVideo because: ${JSON.stringify(error)}`);
}

wallpaper.setCustomWallpaper10+

setCustomWallpaper(source: string, wallpaperType: WallpaperType, callback: AsyncCallback<void>): void

将指定uri的内容设置为壁纸资源,仅当com.ohos.sceneboard存在时,支持使用该接口。使用callback异步回调。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
source string 自定义壁纸的Uri路径。
wallpaperType WallpaperType 壁纸类型。
callback AsyncCallback<void> 回调函数,设置壁纸成功,error为undefined,否则返回error信息。

示例:

import { BusinessError } from '@ohos.base';

let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.zip";
try {
    wallpaper.setCustomWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
        if (error) {
            console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`);
            return;
        }
        console.log(`success to setCustomWallpaper.`);
    });
} catch (error) {
    console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`);
}

wallpaper.setCustomWallpaper10+

setCustomWallpaper(source: string, wallpaperType: WallpaperType): Promise<void>

将指定uri的内容设置为壁纸资源,仅当com.ohos.sceneboard存在时,支持使用该接口。使用Promise异步回调。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
source string 自定义壁纸的Uri路径。
wallpaperType WallpaperType 壁纸类型。

返回值:

类型 说明
Promise<void> 无返回结果的Promise对象。

示例:

import { BusinessError } from '@ohos.base';

let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.zip";
try {
    wallpaper.setCustomWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
        console.log(`success to setCustomWallpaper.`);
    }).catch((error: BusinessError) => {
        console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`);
    });
} catch (error) {
    console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`);
}

wallpaper.on(‘wallpaperChange’)10+

on(type: ‘wallpaperChange’, callback: (wallpaperType: WallpaperType, resourceType: WallpaperResourceType, uri?: string) => void): void

订阅壁纸变化通知事件。

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
type string 事件回调类型。支持的事件为’wallpaperChange’,完成壁纸切换后触发该事件。
callback function 壁纸变化触发该回调方法,返回壁纸类型和壁纸资源类型。
- wallpaperType:壁纸类型。
- resourceType:壁纸资源类型。
- uri:壁纸资源地址。

示例:

try {
    let listener = (wallpaperType: wallpaper.WallpaperType, resourceType: wallpaper.WallpaperResourceType): void => {
        console.log(`wallpaper color changed.`);
    };
    wallpaper.on('wallpaperChange', listener);
} catch (error) {
    console.error(`failed to on because: ${JSON.stringify(error)}`);
}

wallpaper.off(‘wallpaperChange’)10+

off(type: ‘wallpaperChange’, callback?: (wallpaperType: WallpaperType, resourceType: WallpaperResourceType, uri?: string) => void): void

取消订阅壁纸变化通知事件。

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
type string 事件回调类型。支持的事件为’wallpaperChange’,完成壁纸切换后触发该事件。
callback function 表示要取消的壁纸变化回调,不填写该参数则取消订阅该type对应的所有回调。
- wallpaperType:壁纸类型。
- resourceType:壁纸资源类型。
- uri:壁纸资源地址。

示例:

let listener = (wallpaperType: wallpaper.WallpaperType, resourceType: wallpaper.WallpaperResourceType): void => {
    console.log(`wallpaper color changed.`);
};
try {
    wallpaper.on('wallpaperChange', listener);
} catch (error) {
    console.error(`failed to on because: ${JSON.stringify(error)}`);
}

try {
    // 取消订阅listener
    wallpaper.off('wallpaperChange', listener);
} catch (error) {
    console.error(`failed to off because: ${JSON.stringify(error)}`);
}

try {
    // 取消所有'wallpaperChange'类型的订阅
    wallpaper.off('wallpaperChange');
} catch (error) {
    console.error(`failed to off because: ${JSON.stringify(error)}`);
}

wallpaper.getColorsSync9+

getColorsSync(wallpaperType: WallpaperType): Array<RgbaColor>

获取指定类型壁纸的主要颜色信息。

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。

返回值

类型 说明
Array<RgbaColor> 返回壁纸的主要颜色信息。

示例

try {
    let colors = wallpaper.getColorsSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM);
    console.log(`success to getColorsSync: ${JSON.stringify(colors)}`);
} catch (error) {
    console.error(`failed to getColorsSync because: ${JSON.stringify(error)}`);
}

wallpaper.getMinHeightSync9+

getMinHeightSync(): number

获取壁纸的最小高度值。

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

返回值:

类型 说明
number 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。

示例:

let minHeight = wallpaper.getMinHeightSync();

wallpaper.getMinWidthSync9+

getMinWidthSync(): number

获取壁纸的最小宽度值。

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

返回值:

类型 说明
number 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。

示例:

let minWidth = wallpaper.getMinWidthSync();

wallpaper.restore9+

restore(wallpaperType: WallpaperType, callback: AsyncCallback<void>): void

移除指定类型的壁纸,恢复为默认显示的壁纸。使用callback异步回调。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。
callback AsyncCallback<void> 回调函数,移除壁纸成功,error为undefined,否则返回error信息。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
    if (error) {
        console.error(`failed to restore because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to restore.`);
});

wallpaper.restore9+

restore(wallpaperType: WallpaperType): Promise<void>

移除指定类型的壁纸,恢复为默认显示的壁纸。使用promise异步回调。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。

返回值:

类型 说明
Promise<void> 无返回结果的Promise对象。

示例:

import { BusinessError } from '@ohos.base';
 
wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
    console.log(`success to restore.`);
  }).catch((error: BusinessError) => {
    console.error(`failed to restore because: ${JSON.stringify(error)}`);
});

wallpaper.setImage9+

setImage(source: string|image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback<void>): void

将指定资源设置为指定类型的壁纸。使用callback异步回调。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
source string |image.PixelMap JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。
wallpaperType WallpaperType 壁纸类型。
callback AsyncCallback<void> 回调函数,设置壁纸成功,error为undefined,否则返回error信息。

示例:

import { BusinessError } from '@ohos.base';
import image from '@ohos.multimedia.image';

// source类型为string
let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg";
wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
    if (error) {
        console.error(`failed to setImage because: ${JSON.stringify(error)}`);
        return;
     }
    console.log(`success to setImage.`);
});
  
// source类型为image.PixelMap
let imageSource = image.createImageSource("file://" + wallpaperPath);
let opts: image.DecodingOptions = {
    desiredSize: {
        height: 3648,
        width: 2736
    }
};
imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => {
    wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
        if (error) {
            console.error(`failed to setImage because: ${JSON.stringify(error)}`);
            return;
        }
        console.log(`success to setImage.`);
    });
}).catch((error: BusinessError) => {
    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
});

wallpaper.setImage9+

setImage(source: string|image.PixelMap, wallpaperType: WallpaperType): Promise<void>

将指定资源设置为指定类型的壁纸。使用promise异步回调。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
source string |image.PixelMap JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。
wallpaperType WallpaperType 壁纸类型。

返回值:

类型 说明
Promise<void> 无返回结果的Promise对象。

示例:

import { BusinessError } from '@ohos.base';
import image from '@ohos.multimedia.image';

// source类型为string
let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg";
wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
    console.log(`success to setImage.`);
}).catch((error: BusinessError) => {
    console.error(`failed to setImage because: ${JSON.stringify(error)}`);
});

// source类型为image.PixelMap
let imageSource = image.createImageSource("file://" + wallpaperPath);
let opts: image.DecodingOptions = {
    desiredSize: {
        height: 3648,
        width: 2736
    }
};
imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => {
    wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
        console.log(`success to setImage.`);
    }).catch((error: BusinessError) => {
        console.error(`failed to setImage because: ${JSON.stringify(error)}`);
    });
}).catch((error: BusinessError) => {
    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
});

wallpaper.getImage9+

getImage(wallpaperType: WallpaperType, callback: AsyncCallback<image.PixelMap>): void;

获取壁纸图片的像素图。使用callback异步回调。

需要权限:ohos.permission.GET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。
callback AsyncCallback<image.PixelMap> 回调函数,调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。

示例:

import { BusinessError } from '@ohos.base';
import image from '@ohos.multimedia.image';

wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: image.PixelMap) => {
    if (error) {
        console.error(`failed to getImage because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to getImage: ${JSON.stringify(data)}`);
});

wallpaper.getImage9+

getImage(wallpaperType: WallpaperType): Promise<image.PixelMap>

获取壁纸图片的像素图。使用promise异步回调。

需要权限:ohos.permission.GET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。

返回值:

类型 说明
Promise<image.PixelMap> 调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。

示例:

import { BusinessError } from '@ohos.base';
import image from '@ohos.multimedia.image';

wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: image.PixelMap) => {
    console.log(`success to getImage: ${JSON.stringify(data)}`);
  }).catch((error: BusinessError) => {
    console.error(`failed to getImage because: ${JSON.stringify(error)}`);
});

wallpaper.on(‘colorChange’)(deprecated)

on(type: ‘colorChange’, callback: (colors: Array<RgbaColor>, wallpaperType: WallpaperType) => void): void

订阅壁纸颜色变化结果上报事件。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
type string 取值为’colorChange’,表示壁纸颜色变化结果上报事件。
callback function 壁纸颜色变化触发该回调方法,返回壁纸类型和壁纸的主要颜色信息。
- colors
壁纸的主要颜色信息,其类型见RgbaColor
- wallpaperType
壁纸类型。

示例:

try {
    let listener = (colors: Array<wallpaper.RgbaColor>, wallpaperType: wallpaper.WallpaperType): void => {
        console.log(`wallpaper color changed.`);
    };
    wallpaper.on('colorChange', listener);
} catch (error) {
    console.error(`failed to on because: ${JSON.stringify(error)}`);
}

wallpaper.off(‘colorChange’)(deprecated)

off(type: ‘colorChange’, callback?: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void

取消订阅壁纸颜色变化结果上报事件。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
type string 取值为’colorChange’,表示取消订阅壁纸颜色变化结果上报事件。
callback function 表示要取消的壁纸颜色变化的回调,不填写该参数则取消订阅该type对应的所有回调。
- colors
壁纸的主要颜色信息,其类型见RgbaColor
- wallpaperType
壁纸类型。

示例:

let listener = (colors: Array<wallpaper.RgbaColor>, wallpaperType: wallpaper.WallpaperType): void => {
    console.log(`wallpaper color changed.`);
};
try {
    wallpaper.on('colorChange', listener);
} catch (error) {
    console.error(`failed to on because: ${JSON.stringify(error)}`);
}

try {
    // 取消订阅listener
    wallpaper.off('colorChange', listener);
} catch (error) {
    console.error(`failed to off because: ${JSON.stringify(error)}`);
}

try {
    // 取消所有'colorChange'类型的订阅
    wallpaper.off('colorChange');
} catch (error) {
    console.error(`failed to off because: ${JSON.stringify(error)}`);
}

wallpaper.getColors(deprecated)

getColors(wallpaperType: WallpaperType, callback: AsyncCallback&lt;Array&lt;RgbaColor&gt;&gt;): void

获取指定类型壁纸的主要颜色信息。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。
callback AsyncCallback&lt;Array&lt;RgbaColor&gt;&gt; 回调函数,返回壁纸的主要颜色信息。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: Array<wallpaper.RgbaColor>) => {
    if (error) {
        console.error(`failed to getColors because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to getColors: ${JSON.stringify(data)}`);
});

wallpaper.getColors(deprecated)

getColors(wallpaperType: WallpaperType): Promise&lt;Array&lt;RgbaColor&gt;&gt;

获取指定类型壁纸的主要颜色信息。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。

返回值:

类型 说明
Promise&lt;Array&lt;RgbaColor&gt;&gt; 返回壁纸的主要颜色信息。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: Array<wallpaper.RgbaColor>) => {
    console.log(`success to getColors: ${JSON.stringify(data)}`);
  }).catch((error: BusinessError) => {
    console.error(`failed to getColors because: ${JSON.stringify(error)}`);
});

wallpaper.getId(deprecated)

getId(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void

获取指定类型壁纸的ID。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。
callback AsyncCallback&lt;number&gt; 回调函数,返回壁纸的ID。如果配置了指定类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1到(2^31-1)。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: Number) => {
    if (error) {
        console.error(`failed to getId because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to getId: ${JSON.stringify(data)}`);
});

wallpaper.getId(deprecated)

getId(wallpaperType: WallpaperType): Promise&lt;number&gt;

获取指定类型壁纸的ID。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。

返回值:

类型 说明
Promise&lt;number&gt; 壁纸的ID。如果配置了这种壁纸类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1到(2^31-1)。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: Number) => {
    console.log(`success to getId: ${JSON.stringify(data)}`);
  }).catch((error: BusinessError) => {
    console.error(`failed to getId because: ${JSON.stringify(error)}`);
});

wallpaper.getMinHeight(deprecated)

getMinHeight(callback: AsyncCallback&lt;number&gt;): void

获取壁纸的最小高度值。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
callback AsyncCallback&lt;number&gt; 回调函数,返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.getMinHeight((error: BusinessError, data: Number) => {
    if (error) {
        console.error(`failed to getMinHeight because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to getMinHeight: ${JSON.stringify(data)}`);
});

wallpaper.getMinHeight(deprecated)

getMinHeight(): Promise&lt;number&gt;

获取壁纸的最小高度值。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

返回值:

类型 说明
Promise&lt;number&gt; 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.getMinHeight().then((data: Number) => {
    console.log(`success to getMinHeight: ${JSON.stringify(data)}`);
}).catch((error: BusinessError) => {
    console.error(`failed to getMinHeight because: ${JSON.stringify(error)}`);
});

wallpaper.getMinWidth(deprecated)

getMinWidth(callback: AsyncCallback&lt;number&gt;): void

获取壁纸的最小宽度值。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
callback AsyncCallback&lt;number&gt; 回调函数,壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.getMinWidth((error: BusinessError, data: Number) => {
    if (error) {
        console.error(`failed to getMinWidth because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to getMinWidth: ${JSON.stringify(data)}`);
});

wallpaper.getMinWidth(deprecated)

getMinWidth(): Promise&lt;number&gt;

获取壁纸的最小宽度值。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

返回值:

类型 说明
Promise&lt;number&gt; 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.getMinWidth().then((data: Number) => {
    console.log(`success to getMinWidth: ${JSON.stringify(data)}`);
  }).catch((error: BusinessError) => {
    console.error(`failed to getMinWidth because: ${JSON.stringify(error)}`);
});

wallpaper.isChangePermitted(deprecated)

isChangePermitted(callback: AsyncCallback&lt;boolean&gt;): void

是否允许应用改变当前用户的壁纸。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
callback AsyncCallback&lt;boolean&gt; 回调函数,返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.isChangePermitted((error: BusinessError, data: Boolean) => {
    if (error) {
        console.error(`failed to isChangePermitted because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to isChangePermitted: ${JSON.stringify(data)}`);
});

wallpaper.isChangePermitted(deprecated)

isChangePermitted(): Promise&lt;boolean&gt;

是否允许应用改变当前用户的壁纸。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

返回值:

类型 说明
Promise&lt;boolean&gt; 返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.isChangePermitted().then((data: Boolean) => {
    console.log(`success to isChangePermitted: ${JSON.stringify(data)}`);
}).catch((error: BusinessError) => {
    console.error(`failed to isChangePermitted because: ${JSON.stringify(error)}`);
});

wallpaper.isOperationAllowed(deprecated)

isOperationAllowed(callback: AsyncCallback&lt;boolean&gt;): void

是否允许用户设置壁纸。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
callback AsyncCallback&lt;boolean&gt; 回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.isOperationAllowed((error: BusinessError, data: Boolean) => {
    if (error) {
        console.error(`failed to isOperationAllowed because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to isOperationAllowed: ${JSON.stringify(data)}`);
});

wallpaper.isOperationAllowed(deprecated)

isOperationAllowed(): Promise&lt;boolean&gt;

是否允许用户设置壁纸。

说明:

从 API version 7开始支持,从API version 9开始废弃。

系统能力: SystemCapability.MiscServices.Wallpaper

返回值:

类型 说明
Promise&lt;boolean&gt; 异步回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.isOperationAllowed().then((data: Boolean) => {
    console.log(`success to isOperationAllowed: ${JSON.stringify(data)}`);
  }).catch((error: BusinessError) => {
    console.error(`failed to isOperationAllowed because: ${JSON.stringify(error)}`);
});

wallpaper.reset(deprecated)

reset(wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void

移除指定类型的壁纸,恢复为默认显示的壁纸。

说明:

从 API version 7开始支持,从API version 9开始废弃。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。
callback AsyncCallback&lt;void&gt; 回调函数,移除壁纸成功,error为undefined,否则返回error信息。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
    if (error) {
        console.error(`failed to reset because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to reset.`);
});

wallpaper.reset(deprecated)

reset(wallpaperType: WallpaperType): Promise&lt;void&gt;

移除指定类型的壁纸,恢复为默认显示的壁纸。

说明:

从 API version 7开始支持,从API version 9开始废弃。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。

返回值:

类型 说明
Promise&lt;void&gt; 无返回结果的Promise对象。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
    console.log(`success to reset.`);
}).catch((error: BusinessError) => {
    console.error(`failed to reset because: ${JSON.stringify(error)}`);
});

wallpaper.setWallpaper(deprecated)

setWallpaper(source: string|image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void

将指定资源设置为指定类型的壁纸。

说明:

从 API version 7开始支持,从API version 9开始废弃。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
source string |image.PixelMap JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。
wallpaperType WallpaperType 壁纸类型。
callback AsyncCallback&lt;void&gt; 回调函数,设置壁纸成功,error为undefined,否则返回error信息。

示例:

import { BusinessError } from '@ohos.base';
import image from '@ohos.multimedia.image';

// source类型为string
let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg";
wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
    if (error) {
        console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
       return;
       }
    console.log(`success to setWallpaper.`);
});

// source类型为image.PixelMap
let imageSource = image.createImageSource("file://" + wallpaperPath);
let opts: image.DecodingOptions = {
    desiredSize: {
        height: 3648,
        width: 2736
    }
};
imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => {
    wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
        if (error) {
            console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
            return;
        }
        console.log(`success to setWallpaper.`);
    });
}).catch((error: BusinessError) => {
    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
});

wallpaper.setWallpaper(deprecated)

setWallpaper(source: string|image.PixelMap, wallpaperType: WallpaperType): Promise&lt;void&gt;

将指定资源设置为指定类型的壁纸。

说明:

从 API version 7开始支持,从API version 9开始废弃。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
source string |image.PixelMap JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。
wallpaperType WallpaperType 壁纸类型。

返回值:

类型 说明
Promise&lt;void&gt; 无返回结果的Promise对象。

示例:

import { BusinessError } from '@ohos.base';
import image from '@ohos.multimedia.image';

// source类型为string
let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg";
wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
    console.log(`success to setWallpaper.`);
  }).catch((error: BusinessError) => {
    console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
});
  
// source类型为image.PixelMap
let imageSource = image.createImageSource("file://" + wallpaperPath);
let opts: image.DecodingOptions = {
    desiredSize: {
        height: 3648,
        width: 2736
    }
};
imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => {
    wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
        console.log(`success to setWallpaper.`);
    }).catch((error: BusinessError) => {
        console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
    });
  }).catch((error: BusinessError) => {
    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
});

wallpaper.getFile(deprecated)

getFile(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void

获取指定类型的壁纸文件。

说明:

从 API version 8开始支持,从API version 9开始废弃。

需要权限:ohos.permission.GET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。
callback AsyncCallback&lt;number&gt; 回调函数,调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: number) => {
    if (error) {
        console.error(`failed to getFile because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to getFile: ${JSON.stringify(data)}`);
});

wallpaper.getFile(deprecated)

getFile(wallpaperType: WallpaperType): Promise&lt;number&gt;

获取指定类型的壁纸文件。

说明:

从 API version 8开始支持,从API version 9开始废弃。

需要权限:ohos.permission.GET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。

返回值:

类型 说明
Promise&lt;number&gt; 调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。

示例:

import { BusinessError } from '@ohos.base';

wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: number) => {
    console.log(`success to getFile: ${JSON.stringify(data)}`);
  }).catch((error: BusinessError) => {
    console.error(`failed to getFile because: ${JSON.stringify(error)}`);
});

wallpaper.getPixelMap(deprecated)

getPixelMap(wallpaperType: WallpaperType, callback: AsyncCallback&lt;image.PixelMap&gt;): void;

获取壁纸图片的像素图。

说明:

从 API version 7开始支持,从API version 9开始废弃。

需要权限:ohos.permission.GET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。
callback AsyncCallback&lt;image.PixelMap&gt; 回调函数,调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。

示例:

import { BusinessError } from '@ohos.base';
import image from '@ohos.multimedia.image';

wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: image.PixelMap) => {
    if (error) {
        console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to getPixelMap : ${JSON.stringify(data)}`);
  });

wallpaper.getPixelMap(deprecated)

getPixelMap(wallpaperType: WallpaperType): Promise&lt;image.PixelMap&gt;

获取壁纸图片的像素图。

说明:

从 API version 7开始支持,从API version 9开始废弃。

需要权限:ohos.permission.GET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

系统接口:此接口为系统接口。

参数:

参数名 类型 必填 说明
wallpaperType WallpaperType 壁纸类型。

返回值:

类型 说明
Promise&lt;image.PixelMap&gt; 调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。

示例:

import { BusinessError } from '@ohos.base';
import image from '@ohos.multimedia.image';

wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: image.PixelMap) => {
    console.log(`success to getPixelMap : ${JSON.stringify(data)}`);
  }).catch((error: BusinessError) => {
    console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`);
});

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

1  赞