近段时间的学习碎片整理(18)

设置壁纸功能(可参考Android 代码设置壁纸,兼容华为 小米 OPPO Vivo - 简书

建议先将壁纸下载到缓存文件夹后再调用手机自带的设置壁纸组件来完成,也可以用wallpapermanager来完成,但部分厂商对设置壁纸这块有不同的修改,wallpapermanager可能会没有设置上(有些锁屏壁纸没有设置上可能是手机开启了锁屏画报之类的东西),且有些设备设置壁纸会自动放大图片等等问题。ps:oppo的设置壁纸组件包名好像发生了变化,在一些比较新的机型会报错的。有没有知道新的包名叫什么呢?

 /**
     * 调用小米系统自带的壁纸设置
     * @param file File
     * @param flag Int
     */
    private fun setXiaoMi(file: File, flag: Int) {
        try {
            val componentName = ComponentName(
                "com.android.thememanager",
                "com.android.thememanager.activity.WallpaperDetailActivity"
            )
            intent = Intent("miui.intent.action.START_WALLPAPER_DETAIL")
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.setDataAndType(getUri(file), "image/*")
            intent.component = componentName
            startActivity(intent)
        }catch (e: Exception) {
            e.printStackTrace()
            setWallpaperDefault(file.absolutePath, flag)
        }
    }

    /**
     * 调用vivo系统自带的壁纸设置
     * @param file File
     * @param flag Int
     */
    private fun setVivo(file: File, flag: Int) {
        try {
            val componentName = ComponentName("com.vivo.gallery", "com.android.gallery3d.app.Wallpaper")
            intent = Intent(Intent.ACTION_VIEW)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.setDataAndType(getUri(file), "image/*");
            intent.component = componentName
            startActivity(intent)
        }catch (e: Exception) {
            e.printStackTrace()
            setWallpaperDefault(file.absolutePath, flag)
        }
    }

    /**
     * 调用huawei系统自带的壁纸设置
     * @param file File
     * @param flag Int
     */
    private fun setHuawei(file: File, flag: Int) {
        try {
            val componentName =
                ComponentName("com.android.gallery3d", "com.android.gallery3d.app.Wallpaper")
            intent = Intent(Intent.ACTION_VIEW)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.setDataAndType(getUri(file), "image/*")
            intent.component = componentName
            startActivity(intent)
        }catch (e: Exception) {
            e.printStackTrace()
            setWallpaperDefault(file.absolutePath, flag)
        }
    }

    /**
     * 调用oppo系统自带的壁纸设置
     * @param file File
     * @param flag Int
     */
    private fun setOppo(file: File, flag: Int) {
        try {
            val componentName =
                ComponentName("com.coloros.gallery3d", "com.oppo.gallery3d.app.Wallpaper")
            intent = Intent(Intent.ACTION_VIEW)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.setDataAndType(getUri(file), "image/*")
            intent.component = componentName
            startActivity(intent)
        }catch (e: Exception) {
            e.printStackTrace()
            setWallpaperDefault(file.absolutePath, flag)
        }

    }

    /**
     * 调用其他机型自带的壁纸设置
     * @param file File
     * @param flag Int
     */
    private fun setOthers(file: File, flag: Int) {
        try {
            startActivity(WallpaperManager.getInstance(this)
                .getCropAndSetWallpaperIntent(getUri(file)))
        }catch (e: Exception) {
            e.printStackTrace()
            setWallpaperDefault(file.absolutePath, flag)
        }
    }

使用wallpapermanager如下:

val wallpaperManager = WallpaperManager.getInstance(this)

//设置锁屏壁纸
wallpaperManager.setBitmap(bitmap, Rect(0, 0, bitmap.width, bitmap.height), false, WallpaperManager.FLAG_LOCK)
//设置桌面壁纸
wallpaperManager.setBitmap(bitmap, Rect(0, 0, bitmap.width, bitmap.height), false, WallpaperManager.FLAG_SYSTEM)

安卓7.0及以上获得uri需要使用fileprovider(可参考Android FileProvider详细解析和踩坑指南_wxz的博客-CSDN博客_fileprovider)

androidmanifest上声明:

   <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="*******.fileProvider" //可自己定义,一般是应用包名
            android:exported="false"
            android:grantUriPermissions="true"
            tools:replace="android:authorities">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"
                tools:replace="android:resource" />
        </provider>

在res下建立一个xml文件夹,

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="name" path="yourpath"/>
  
</paths>
 /**
     * 获得壁纸uri地址
     * @param file File
     * @return Uri
     */
    private fun getUri(file: File): Uri {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            FileProvider.getUriForFile(this, AUTHORITY, file)
        }else {
            Uri.fromFile(file)
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值