设置壁纸(静态壁纸)

设置壁纸(静态壁纸)

1.权限
<uses-permission android:name="android.permission.SET_WALLPAPER" />
2.系统接口
//获取壁纸管理者
val wallpaperManager = WallpaperManager.getInstance(BaseApplication.context)
val inputStream = BaseApplication.context.contentResolver.openInputStream(uri)
//android 7.0之后可以设置锁屏壁纸
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    //参数    图片资源流   图片显示区域  是否允许备份  壁纸标志:WallpaperManager.FLAG_LOCK(锁屏)、WallpaperManager.FLAG_SYSTEM(桌面)
    wallpaperManager.setStream(inputStream,null,true,WallpaperManager.FLAG_SYSTEM)
}else{
    wallpaperManager.setStream(inputStream)
}
3.针对7.0以下锁屏壁纸无法设置兼容
public static void setWallpaper(Context context, String uri, String authority) {
    if (context == null || TextUtils.isEmpty(uri) || TextUtils.isEmpty(authority)) {
        return;
    }
    Intent intent;
    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(Uri.parse(uri), "image/*");
            intent.putExtra("mimeType", "image/*");
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(uri));
                WallpaperManager.getInstance(context.getApplicationContext())
                .setStream(inputStream);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    } 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(Uri.parse(uri), "image/*");
            intent.putExtra("mimeType", "image/*");
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(uri));
                WallpaperManager.getInstance(context.getApplicationContext())
                .setStream(inputStream);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                intent =
                WallpaperManager.getInstance(context.getApplicationContext()).getCropAndSetWallpaperIntent(Uri.parse(uri));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.getApplicationContext().startActivity(intent);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                try {
                    InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(uri));
                    WallpaperManager.getInstance(context.getApplicationContext())
                    .setStream(inputStream);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        } else {
            try {
                InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(uri));
                WallpaperManager.getInstance(context.getApplicationContext())
                .setStream(inputStream);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

RomUtil

public class RomUtil {

    private static final String TAG = "RomUtil";

    /**
     * 判断是否为华为系统
     */
    public static boolean isHuaweiRom() {
        if (!TextUtils.isEmpty(getEmuiVersion()) && !getEmuiVersion().equals("")) {
            Log.d(TAG, "isHuaweiRom: true");
            return true;
        }
        Log.d(TAG, "isHuaweiRom: false");
        return false;
    }

    /**
     * 判断是否为小米系统
     */
    public static boolean isMiuiRom() {
        if (!TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name"))) {
            Log.d(TAG, "isMiuiRom: true");
            return true;
        }
        Log.d(TAG, "isMiuiRom: false");
        return false;
    }

    private static String getSystemProperty(String propName) {
        String line;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + propName);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            Log.e(TAG, "Unable to read sysprop " + propName, ex);
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    Log.e(TAG, "Exception while closing InputStream", e);
                }
            }
        }
        return line;
    }

    /**
     * 判断是否为Flyme系统
     */
    public static boolean isFlymeRom(Context context) {
        Object obj = isInstalledByPkgName(context, "com.meizu.flyme.update") ? Log.d(TAG, "isFlymeRom: true")
            : Log.d(TAG, "isFlymeRom: false");
        return isInstalledByPkgName(context, "com.meizu.flyme.update");
    }

    /**
     * 判断是否是Smartisan系统
     */
    public static boolean isSmartisanRom(Context context) {
        Object obj = isInstalledByPkgName(context, "com.smartisanos.security") ? Log.d(TAG, "isSmartisanRom: true")
            : Log.d(TAG, "isSmartisanRom: false");
        return isInstalledByPkgName(context, "com.smartisanos.security");
    }

    /**
     * 根据包名判断这个app是否已安装
     */
    public static boolean isInstalledByPkgName(Context context, String pkgName) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(pkgName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
        }
        if (packageInfo == null) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @return 只要返回不是"",则是EMUI版本
     */
    private static String getEmuiVersion() {
        String emuiVerion = "";
        Class<?>[] clsArray = new Class<?>[] { String.class };
        Object[] objArray = new Object[] { "ro.build.version.emui" };
        try {
            Class<?> SystemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method get = SystemPropertiesClass.getDeclaredMethod("get", clsArray);
            String version = (String) get.invoke(SystemPropertiesClass, objArray);
            Log.d(TAG, "get EMUI version is:" + version);
            if (!TextUtils.isEmpty(version)) {
                return version;
            }
        } catch (ClassNotFoundException e) {
            Log.e(TAG, " getEmuiVersion wrong, ClassNotFoundException");
        } catch (LinkageError e) {
            Log.e(TAG, " getEmuiVersion wrong, LinkageError");
        } catch (NoSuchMethodException e) {
            Log.e(TAG, " getEmuiVersion wrong, NoSuchMethodException");
        } catch (NullPointerException e) {
            Log.e(TAG, " getEmuiVersion wrong, NullPointerException");
        } catch (Exception e) {
            Log.e(TAG, " getEmuiVersion wrong");
        }
        return emuiVerion;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是详解。 Android 壁纸可以分为静态壁纸和动态壁纸两种类型。其中,静态壁纸主要是指一张图片或者一组图片作为壁纸,而动态壁纸则是指一些具有动态效果的壁纸,比如动态的天气、时钟、星空等等。 在 Android 中,设置壁纸可以使用 WallpaperManager 类。这个类提供了一些方法来设置壁纸,包括设置静态壁纸和动态壁纸。 下面是设置静态壁纸的代码示例: ```java // 获取 WallpaperManager 实例 WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); // 设置壁纸 try { // 设置一张本地图片作为壁纸 wallpaperManager.setBitmap(bitmap); // 或者设置一张网络图片作为壁纸 wallpaperManager.setStream(inputStream); } catch (IOException e) { e.printStackTrace(); } ``` 上面的代码中,我们首先通过 `WallpaperManager.getInstance()` 方法获取了一个 WallpaperManager 实例。然后,我们可以使用 `setBitmap()` 或者 `setStream()` 方法来设置壁纸。其中,`setBitmap()` 方法可以设置一个本地的 Bitmap 对象作为壁纸,而 `setStream()` 方法可以设置一个输入流,用于加载网络图片或者本地图片。 接下来是设置动态壁纸的代码示例: ```java // 构造一个 Intent 对象,用于启动动态壁纸的服务 Intent intent = new Intent(); intent.setAction(WallpaperService.SERVICE_INTERFACE); intent.setClassName("com.example.myapplication", "com.example.myapplication.MyWallpaperService"); // 启动动态壁纸的服务 context.startService(intent); // 获取 WallpaperManager 实例,用于设置动态壁纸 WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); // 设置动态壁纸 try { // 获取动态壁纸的服务 WallpaperInfo wallpaperInfo = wallpaperManager.getWallpaperInfo(); if (wallpaperInfo != null) { // 如果动态壁纸已经被设置,则先取消掉 wallpaperManager.clear(); } // 设置动态壁纸 wallpaperManager.setWallpaperComponent(new ComponentName("com.example.myapplication", "com.example.myapplication.MyWallpaperService")); } catch (IOException e) { e.printStackTrace(); } ``` 上面的代码中,我们首先构造了一个 Intent 对象,用于启动动态壁纸的服务。这个 Intent 对象需要指定服务的包名和类名,可以根据实际情况进行修改。 然后,我们使用 `startService()` 方法启动动态壁纸的服务。接着,我们通过 `WallpaperManager.getInstance()` 方法获取了一个 WallpaperManager 实例,用于设置动态壁纸。 在设置动态壁纸之前,我们需要先获取当前是否已经设置了动态壁纸。如果已经设置了动态壁纸,则需要先取消掉,然后再设置新的动态壁纸。最后,我们使用 `setWallpaperComponent()` 方法来设置动态壁纸的组件。 需要注意的是,设置动态壁纸的过程比较复杂,需要先创建一个服务,然后在服务中实现动态壁纸的逻辑。这个过程涉及到的知识点比较多,需要进一步学习和掌握。 希望以上内容能够帮助到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值