最近在做一款壁纸app,刚接触native.js,因为对安卓也不熟悉,直到google了下才找到了设置 锁屏壁纸的方法,桌面壁纸方法官网社区也是有答案的。
As of the latest Android API 24 it is possible to update the Lockscreen wallpaper by using the WallpaperManager and providing the FLAG_LOCK flag.
wallpaperManager.setBitmap(bitmap); //设置壁纸
wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK); //设置锁屏
通过native.js修改系统壁纸和锁屏壁纸,整合的源码如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//设置壁纸和锁屏 function setwallpaper($wallpaper_url){ var WallpaperManager = plus.android.importClass("android.app.WallpaperManager"); //获取应用主activity实例对象 var Main = plus.android.runtimeMainActivity(); var wallpaperManager = WallpaperManager.getInstance(Main); plus.android.importClass(wallpaperManager); var BitmapFactory = plus.android.importClass("android.graphics.BitmapFactory"); var url=$wallpaper_url; // 换成要设置的壁纸图片路径 //将本地URL路径转换成平台绝对路径,如url为“_doc/a.png” var path=plus.io.convertLocalFileSystemURL(url); //解析图片文件并创建对应的Bitmap对象 var bitmap = BitmapFactory.decodeFile(path); try{ wallpaperManager.setBitmap(bitmap); //设置壁纸 wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK); //设置锁屏 bitmap.recycle(); // 设置完毕桌面要进行 原生层的BITMAP回收 减少内存压力 }catch(e){ //TODO handle the exception } } |
:Bcoder资源网 » 使用native.js修改系统壁纸和锁屏壁纸(Android)