Android 设置系统桌面壁纸

本篇主要讲下如何设置系统桌面壁纸.

代码如下:

java 复制代码
    public static void setLauncherWallPaper(Context context, Bitmap bitmap) throws IOException {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            final int result;
            try {
                result = WallpaperManager.getInstance(context).setBitmap(bitmap, null, false, WallpaperManager.FLAG_SYSTEM);
            } catch (Throwable e) {
                throw new IOException(e);
            }
            if (result == 0) {
                throw new IOException("setLauncherWallPaper failed");
            }
        } else {
            try {
                WallpaperManager.getInstance(context).setBitmap(bitmap);
            } catch (Throwable e) {
                throw new IOException(e);
            }
        }
    }

本地调用如下:

java 复制代码
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.wallpaper);
        try {
            setLauncherWallPaper(this,bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
相关推荐
QING61822 分钟前
一文带你了解Android中常见的跨组件通信方案及其适用场景
android·架构·app
hrrrrb36 分钟前
【MySQL】多表操作 —— 外键约束
android·数据库·mysql
QING6181 小时前
一文带你吃透接口(Interface)结合 @AutoService 与 ServiceLoader 详解
android·kotlin·app
二十四桥明月夜ya1 小时前
如何配置Clion编写aosp的c++程序
android·intellij idea
QING6181 小时前
一文带你吃透Android 中 AIDL 与 bindService 的核心区别
android·kotlin·app
九思x2 小时前
解决 Android Studio “waiting for all target devices to come online“ 卡住问题
android·ide·android studio
QING6184 小时前
Android 冷启动优化实践:含主线程优化、资源预加载与懒加载、跨进程预热等
android·性能优化·app
砖厂小工4 小时前
Now In Android 精讲 7 - 你的代码谁来守护?
android
浩宇软件开发4 小时前
Androidstudio实现一个app引导页(超详细)
android·android studio·android开发
左小左5 小时前
用Compose撸一个CoordinatorLayout 🔥🔥🔥
android·android jetpack·composer