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();
        }
相关推荐
梦里花开知多少5 分钟前
浅谈ThreadPool
android·面试
帅次11 分钟前
单例初始化中的耗时操作如何拖死主线程
android·webview·android runtime
用户08748819991739 分钟前
Android 资源类型全解析及四大常用布局资源深度指南
android
火锅鸡的味道1 小时前
解决AOSP工程Android Studio打开卡顿
android·python·android studio
2501_915921431 小时前
2026 iOS 上架新趋势 iOS 发布流程模块化
android·ios·小程序·https·uni-app·iphone·webview
毕设源码-钟学长1 小时前
【开题答辩全过程】以 基于Android的高校二手交易系统为例,包含答辩的问题和答案
android
FLEMMINGS1 小时前
当 Android Studio 模拟器提示emulator failed to connect within 5 minutes
android·ide·android studio
鹏程十八少1 小时前
6. Android Shadow与众不同?一文解决插件化四大核心难题:ClassLoader冲突、Activity代理、资源隔离、动态更新(源码分析上)
android·前端·面试
不会写DN1 小时前
PHP mysqli 实用开发指南
android·开发语言·php
studyForMokey1 小时前
【Android面试】Handler专题
android·java·面试