How to set android lock screen image
Asked Answered
A

8

36

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

Abreast answered 16/4, 2010 at 15:2 Comment(0)
A
16

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.

Aronson answered 16/4, 2010 at 15:36 Comment(6)
I downvoted this answer because there is an app that specifically does this now. Checkout, muse seemuse.com/home.htmlClea
@Clea I would downvote your comment if I could. Muse is a lock screen replacement app. It doesn't set the lock screen background. It sets its own background as it becomes the lockscreen.Mccracken
Does this answer still hold true? How does an app like Google Play Music get control of the lock screen when music is playing?Narah
@McAdam331: RemoteControlClient on Android 4.x, an a media Notification on 5.0+. Play Music does not "get control of the lock screen"; Play Music provides information to be displayed on the lock screen.Aronson
@Aronson sorry for using the wrong terminology, I understand what you mean. Thanks for clearing that up!Narah
Android Nougat has changed the way lock screens work so that you can now pass a flag when getting or setting the wallpaper specifying lock screen wallpaper or system wallpaper. Check out my answer here: https://mcmap.net/q/427733/-is-there-any-lock-screen-wallpaper-manager-class-in-android.Mckie
Q
43

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.

Quadripartite answered 2/8, 2016 at 18:9 Comment(0)
A
16

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.

Aronson answered 16/4, 2010 at 15:36 Comment(6)
I downvoted this answer because there is an app that specifically does this now. Checkout, muse seemuse.com/home.htmlClea
@Clea I would downvote your comment if I could. Muse is a lock screen replacement app. It doesn't set the lock screen background. It sets its own background as it becomes the lockscreen.Mccracken
Does this answer still hold true? How does an app like Google Play Music get control of the lock screen when music is playing?Narah
@McAdam331: RemoteControlClient on Android 4.x, an a media Notification on 5.0+. Play Music does not "get control of the lock screen"; Play Music provides information to be displayed on the lock screen.Aronson
@Aronson sorry for using the wrong terminology, I understand what you mean. Thanks for clearing that up!Narah
Android Nougat has changed the way lock screens work so that you can now pass a flag when getting or setting the wallpaper specifying lock screen wallpaper or system wallpaper. Check out my answer here: https://mcmap.net/q/427733/-is-there-any-lock-screen-wallpaper-manager-class-in-android.Mckie
A
3

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.

Alec answered 11/2, 2015 at 12:17 Comment(2)
ohk what means is intent?Poff
please give proper example and full exapmlePoff
M
1

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

Meemeece answered 1/8, 2019 at 4:6 Comment(0)
G
0

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

Grillwork answered 24/3, 2017 at 9:2 Comment(0)
N
0
 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();
        }
    
Nightcap answered 3/9, 2020 at 8:32 Comment(0)
B
0

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();
        }
    }
Billiot answered 7/12, 2020 at 12:33 Comment(0)
C
0

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

Cryptomeria answered 12/1, 2022 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.