Android 设置系统桌面壁纸

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

    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);
            }
        }
    }

本地调用如下:

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.wallpaper);
        try {
            setLauncherWallPaper(this,bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

你可能感兴趣的:(android,android)