
回复
【本文正在参加 2023「盲盒」+码有奖征文活动】 https://ost.51cto.com/posts/25284
@toc
在OpenHarmony4.0(API10)中,@ohos.wallpaper (壁纸) 模块增加了多个接口,我们可以通过 setCustomWallpaper、setVideo分别来设置系统的静态壁纸和动态壁纸,我们需要在应用中权限配置中加入 ohos.permission.SET_WALLPAPER 权限。
hdc file send test.png /data/app/el2/100/base/你的应用包名/haps/entry/files/
发送文件到应用目录let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.png";
try {
wallpaper.setCustomWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
console.log(`设置静态壁纸成功`);
}).catch((error) => {
console.error(`设置静态壁纸失败: ${JSON.stringify(error)}`);
});
} catch (error) {
console.error(`设置静态壁纸失败: ${JSON.stringify(error)}`);
}
let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.mp4";
try {
wallpaper.setVideo(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
console.log(`设置动态壁纸成功`);
}).catch((error) => {
console.error(`设置动态壁纸失败: ${JSON.stringify(error)}`);
});
} catch (error) {
console.error(`设置动态壁纸失败: ${JSON.stringify(error)}`);
}
try {
let listener = (wallpaperType, resourceType) => {
console.log(`壁纸变更...`);
};
wallpaper.on('wallpaperChange', listener);
} catch (error) {
console.error(`运行异常: ${JSON.stringify(error)}`);
}
API9虽然出具完备的开发体系,但目前看来API并没有想象中的那么稳定,因为在API10中,除了壁纸管理服务,还有网络连接管理等很多模块的接口都有所调整,有稳定性提升,也有对原有API不足的补充和增强,所以还是要不断的跟进学习呀!