如何设置安卓锁屏图片

发布于 2024-08-29 11:57:45 字数 177 浏览 8 评论 0原文

我刚刚开始使用 android 编程,想看看是否有一种方法可以以编程方式设置锁定屏幕图像。我在API中找到了各种设置壁纸的方法,但我似乎找不到设置锁屏图像的等效方法。

我看过各种帖子说通过添加小部件或应用程序位来自定义锁定屏幕是不可能的,但肯定有一种方法可以以编程方式设置图像?

干杯,

罗宾

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

熟人话多 2024-09-05 11:57:46

还有另一种方法可以做到这一点。首先,您需要将要设置锁屏的图片保存在一个文件夹中(假设它名为“appName”)。然后,使用以下代码打开图库,图库打开后,引导用户打开“appName”文件夹,并选择系统图库中的图片。在图库中,用户可以将图片设置为壁纸或锁屏纸。

// 此代码用于打开画廊。
startActivity(new Intent(Intent.ACTION_SET_WALLPAPER));

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.

// this code to open gallery.
startActivity(new Intent(Intent.ACTION_SET_WALLPAPER));

瞎闹 2024-09-05 11:57:46
 Bitmap icon = BitmapFactory.decodeResource(getViewContext().getResources(), R.drawable.wall);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(getViewContext());
        try {
            wallpaperManager.setBitmap(icon, null, true, WallpaperManager.FLAG_LOCK);
        } catch (IOException e) {
            e.printStackTrace();
        }
    
 Bitmap icon = BitmapFactory.decodeResource(getViewContext().getResources(), R.drawable.wall);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(getViewContext());
        try {
            wallpaperManager.setBitmap(icon, null, true, WallpaperManager.FLAG_LOCK);
        } catch (IOException e) {
            e.printStackTrace();
        }
    
从此见与不见 2024-09-05 11:57:46

api30+ 的用法

public void onWallpaperChanged(Bitmap bitmap, boolean onHomeScreen, boolean onLockScreen) {
        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());

        try {
            if(onHomeScreen) {
                myWallpaperManager.setBitmap(bitmap);// For Home screen
            }

            if(onLockScreen) {
                myWallpaperManager.setBitmap(bitmap,null,true, WallpaperManager.FLAG_LOCK);//For Lock screen
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

usage for api30+

public void onWallpaperChanged(Bitmap bitmap, boolean onHomeScreen, boolean onLockScreen) {
        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());

        try {
            if(onHomeScreen) {
                myWallpaperManager.setBitmap(bitmap);// For Home screen
            }

            if(onLockScreen) {
                myWallpaperManager.setBitmap(bitmap,null,true, WallpaperManager.FLAG_LOCK);//For Lock screen
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
浅沫记忆 2024-09-05 11:57:46

API 级别 24 起,您可以为屏幕、锁定屏幕或两者设置壁纸:

WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    // home screen
    wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_SYSTEM);

    // lock screen
    wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_LOCK);

    // home screen & lock screen
    wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM);
} else {
    wallpaperManager.setBitmap(mBitmap);
}

来源

Since API level 24, you can set wallpaper to your home screen, lock screen, or both:

WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    // home screen
    wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_SYSTEM);

    // lock screen
    wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_LOCK);

    // home screen & lock screen
    wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM);
} else {
    wallpaperManager.setBitmap(mBitmap);
}

source

浮光之海 2024-09-05 11:57:45

从 API 级别 24 开始,他们添加了新方法(并更新了文档 )和 WallpaperManager 的标志,它允许您不仅在主屏幕上设置 Wallpaper ,而且还可以在 Lockscreen

设置 Lockscreen 的 >Wallpaper 使用新标志 WallpaperManager.FLAG_LOCK,以及采用 int which 的方法之一

WallpaperManager.getInstance(this).setStream(inputStream, null, true, WallpaperManager.FLAG_LOCK);

您还可以使用以下方法之一

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)

一个不错的补充是,您现在还可以通过 isSetWallpaperAllowed 检查是否允许设置壁纸,并通过 getWallpaperFile

检查 获取当前设置的壁纸更新 WallpaperManager文档

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

To set a Wallpaper to the Lockscreen use the new flag WallpaperManager.FLAG_LOCK, and one of the methods which take 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

Check out the updated documentation for the WallpaperManager.

二货你真萌 2024-09-05 11:57:45

Android 中没有“锁屏图像”。在原生 Android、HTC Sense、MOTOBLUR 等之间肯定不存在相同的“锁屏图像”概念。这根本不是 Android SDK 的一部分。

Rijk 先生指出的该项目是一种安全违规行为,冒充锁屏替代品。

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.

The project that Mr. Rijk points to is a security violation that pretends to be a lock screen replacement.

笑看君怀她人 2024-09-05 11:57:45

有一种方法可以在三星设备上做到这一点。在意图中你可以添加额外的内容。

intent.putExtra("SET_LOCKSCREEN_WALLPAPER", true);
startActivity(intent);

我只在一些三星手机上测试过这一点,并不能保证这在未来一段时间内不会出现问题。谨慎使用。

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.

这个俗人 2024-09-05 11:57:45

您可以使用 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)

这三个方法的参数:-

Bitmap/resid/inputStreamData :- 该参数接受数据

visibleCropHint:- 该参数接受 Rect 对象,其中主要用于裁剪功能,更多信息请参考 Android 开发者参考网站 ,如果您不想要裁剪功能,您也可以传递 null。allowBackup

:-boolean: true 如果操作系统允许备份此壁纸图像以便恢复到未来的设备;否则为假。

其中:-它是帮助您配置锁屏壁纸和主壁纸的最重要参数之一。对于锁屏使用 WalpaperManager.FLAG_LOCK 对于家庭壁纸使用 FLAG_SYSTEM

我举一个例子来让您了解如何使用它:-

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();
    }

有关更多信息,请访问 Android 开发者壁纸管理器参考

You can use these three methods of WalpaperManager class but it will only work for nought version devices or above it:-

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

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

allowBackup:-boolean: true if the OS is permitted to back up this wallpaper image for restore to a future device; false otherwise.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文