Java 如何设置android锁屏图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2653954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set android lock screen image
提问by robintw
I'm just getting started with android programming, and want to see if there is a way to programmatically set the lock screen image. I've found various ways of setting the wallpaper in the API, but I can't seem to find the equivalent ways of setting the lock screen image.
我刚刚开始使用android编程,想看看有没有办法以编程方式设置锁屏图像。我在 API 中找到了多种设置壁纸的方法,但似乎找不到设置锁屏图像的等效方法。
I've seen various posts saying that customising the lock screen by adding widgets or bits of applications is not possible, but surely there must be a way to set the image programmatically?
我已经看到各种帖子说通过添加小部件或应用程序位来自定义锁定屏幕是不可能的,但肯定必须有一种以编程方式设置图像的方法吗?
Cheers,
干杯,
Robin
罗宾
采纳答案by CommonsWare
There is no "lock screen image" in Android. There most certainly is no "lock screen image" concept that is the same between stock Android, HTC Sense, MOTOBLUR, etc. This simply is not part of the Android SDK.
Android 中没有“锁定屏幕图像”。毫无疑问,Android、HTC Sense、MOTOBLUR 等都没有相同的“锁屏图像”概念。这根本不是 Android SDK 的一部分。
The project that Mr. Rijk points to is a security violation that pretends to be a lock screen replacement.
Rijk 先生指出的项目是一个冒充锁屏替代品的安全违规行为。
回答by ppx
There is a way to do it on Samsung devices. In the intent you can put an extra.
有一种方法可以在三星设备上做到这一点。在意图中,您可以添加一个额外的。
intent.putExtra("SET_LOCKSCREEN_WALLPAPER", true);
startActivity(intent);
I've only tested this on some Samsung phones and there's no guarantee that this won't break some time in the future. Use with caution.
我只在一些三星手机上测试过这个,不能保证这不会在未来一段时间内中断。谨慎使用。
回答by mikepenz
As of API Level 24 they have added new methods (and updated the documentation) and flags to the WallpaperManager
which allow you to set a Wallpaper
not only to the home screen but also to the Lockscreen
从 API 级别 24 开始,他们添加了新方法(并更新了文档)和标志WallpaperManager
,允许您Wallpaper
不仅设置主屏幕,还设置Lockscreen
To set a Wallpaper
to the Lockscreen
use the new flag WallpaperManager.FLAG_LOCK, and one of the methods which take int which
将 a 设置Wallpaper
为Lockscreen
使用新标志WallpaperManager.FLAG_LOCK,以及采用的方法之一int which
WallpaperManager.getInstance(this).setStream(inputStream, null, true, WallpaperManager.FLAG_LOCK);
You can also use one of the following methods
您也可以使用以下方法之一
int setStream (InputStream bitmapData, Rect visibleCropHint, boolean allowBackup, int which)
int setResource (int resid, int which)
int setBitmap (Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, int which)
A nice addition is that you can now also check if you are allowed to set the wallpaper via isSetWallpaperAllowed
, and get the current set wallpaper via getWallpaperFile
一个不错的补充是,您现在还可以检查是否允许通过 设置墙纸isSetWallpaperAllowed
,并通过获取当前设置的墙纸getWallpaperFile
Check out the updated documentationfor the WallpaperManager
.
退房的更新文档的WallpaperManager
。
回答by Licat Julius
There is another way to do this. at first ,you need save the pic which you wanna set in lockedscreen in a folder(suppose it's called "appName").and then ,use following code to open gallery, after gallery has opened.lead user to open "appName" folder ,and choose the pic in gallery of system. in the gallery,user can set a pic as wallpaper or lockscreen paper.
还有另一种方法可以做到这一点。首先,您需要将要在锁定屏幕中设置的图片保存在一个文件夹中(假设它称为“appName”)。然后,使用以下代码打开图库,打开图库后,引导用户打开“appName”文件夹,并在系统图库中选择图片。在图库中,用户可以将图片设置为壁纸或锁屏纸。
// this code to open gallery. startActivity(new Intent(Intent.ACTION_SET_WALLPAPER));
// 此代码用于打开图库。开始活动(新意图(意图。ACTION_SET_WALLPAPER));
回答by Prashant Kumar
You can use these three methods of WalpaperManager class but it will only work for nought version devices or above it:-
您可以使用 WalpaperManager 类的这三个方法,但它仅适用于零版本设备或更高版本:-
public int setBitmap (Bitmap fullImage,
Rect visibleCropHint,
boolean allowBackup,
int which)
public int setResource (int resid,
int which)
public int setStream (InputStream inputStreamData,
Rect visibleCropHint,
boolean allowBackup,
int which)
Parameter of these three methods:-
这三种方法的参数:-
Bitmap/resid/inputStreamData :-this parameter accept data
Bitmap/resid/inputStreamData :-这个参数接受数据
visibleCropHint:-this parameter accept Rect object which is mainly used for Cropping functionality, for more information refer to Android developer reference website, you can also pass null if u don't want cropping functionality
visibleCropHint:-该参数接受主要用于裁剪功能的 Rect 对象,更多信息请参考Android 开发者参考网站,如果您不想要裁剪功能,您也可以传递 null
allowBackup:-boolean: true if the OS is permitted to back up this wallpaper image for restore to a future device; false otherwise.
allowBackup:-boolean: 如果允许操作系统备份此壁纸图像以恢复到未来的设备,则为 true;否则为假。
which:-It is one of the most important parameter which helps you to configure wallpaper for lock screen and home wallpaper. for lock screen use WalpaperManager.FLAG_LOCK and for home wallpaper use FLAG_SYSTEM
其中:-它是最重要的参数之一,可帮助您为锁屏和主墙纸配置墙纸。锁屏使用 WalpaperManager.FLAG_LOCK 和家庭壁纸使用 FLAG_SYSTEM
I am giving one example to make you understand how to use it:-
我举一个例子让你了解如何使用它:-
WalaperManager wm = WalaperManager.getInstance();
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
wm.setBitmap(bitmap,null,true,WalpaperManager.FLAG_LOCK);//For Lock screen
Toast.makeText(context.context, "done", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context.context, "Lock screen walpaper not supported",
Toast.LENGTH_SHORT).show();
}
} catch (e: Exception) {
Toast.makeText(context.context, e.message, Toast.LENGTH_SHORT).show();
}
for more information visit Android developer wallpaper manager reference
有关更多信息,请访问Android 开发者壁纸管理器参考