Android 代码设置壁纸的方式,兼容各大ROM

废话不多说,主要代码:

public static void intent2SetWallPaper(Context context, String path) {
        Uri uriPath = getUriWithPath(path);
        Intent intent;

        // 针对EMUI
        if (RomUtil.isHuaweiRom()) {
            try {
                ComponentName componentName = new ComponentName("com.android.gallery3d", "com.android.gallery3d.app.Wallpaper");
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(uriPath, "image/*");
                intent.putExtra("mimeType", "image/*");
                intent.setComponent(componentName);
                context.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    WallpaperManager.getInstance(context.getApplicationContext()).setBitmap(ImageUtil.getImageBitmap(path));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

        // 针对MIUI
        } else if (RomUtil.isMiuiRom()) {
            try {
                ComponentName componentName = new ComponentName("com.android.thememanager", "com.android.thememanager.activity.WallpaperDetailActivity");
                intent = new Intent("miui.intent.action.START_WALLPAPER_DETAIL");
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(uriPath, "image/*");
                intent.putExtra("mimeType", "image/*");
                intent.setComponent(componentName);
                context.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    WallpaperManager.getInstance(context.getApplicationContext()).setBitmap(ImageUtil.getImageBitmap(path));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

        // 其他
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                context.startActivity(WallpaperManager.getInstance(context.getApplicationContext())
                    .getCropAndSetWallpaperIntent(getUriWithPath(path)));
            } else {
                try {
                    WallpaperManager.getInstance(context.getApplicationContext()).setBitmap(ImageUtil.getImageBitmap(path));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

为方便大家使用,封装到了github:
https://github.com/SherlockGougou/SetWallpaper

使用方式:

1.添加依赖:

Step 1. 在你project层级的build.gradle中,添加仓库地址:
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
Step 2. 在你主module的build.gradle中添加依赖:
dependencies {
     implementation 'com.github.SherlockGougou:SetWallpaper:v1.2.0'
}

2.调用代码:

SetWallpaper.setWallpaper(MainActivity.this, // 上下文
                path, // 图片绝对路径
                APP_AUTHORITY);// authority(7.0 文件共享权限)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,569评论 2 45
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    wgl0419阅读 6,360评论 1 9
  • 创建版本库 通过git init命令把这个目录变成Git可以管理的仓库 把文件添加到版本库 git add把文件提...
    九命丿相柳阅读 154评论 0 0
  • 诗三首 闲不语 之一一个人手握春天 一个人手握春天在大殿上批阅花的折章美丽的梦留在古代的远方风绕过历史的长廊掠过阴...
    闲不语阅读 246评论 3 7
  • 在孩子很小的时候,我们就开始教他颜色。路边的花花草草啦,家里的玩具啦。可是孩子总是听过就忘了。下一次问他,一脸茫然...
    ALICE_微笑阅读 229评论 0 1