1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 近段时间的学习碎片整理(18)

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

时间:2023-04-02 13:53:40

相关推荐

近段时间的学习碎片整理(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/*")ponent = componentNamestartActivity(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/*");ponent = componentNamestartActivity(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/*")ponent = componentNamestartActivity(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/*")ponent = componentNamestartActivity(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上声明:

<providerandroid:name="androidx.core.content.FileProvider"android:authorities="*******.fileProvider" //可自己定义,一般是应用包名android:exported="false"android:grantUriPermissions="true"tools:replace="android:authorities"><meta-dataandroid: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="/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)}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。