How do I change the wallpaper on the lock screen?

2

Basically I'm making a wallpapers application, when changing the wallpaper or background only changes the one on the home screen, but the blocking screen does not, I've searched in different forums but I can not find a correct solution.

Code where I get the Bitmap from an XML on a server:

URL url = new URL(url_down);

            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.connect();
            img = BitmapFactory.decodeStream(connection.getInputStream());

            if (colocarWallpaper) {

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

                    wallpaperManager.setBitmap(img);


                } catch (Exception error) {
                    Log.e("prueba", "colocar wall error: " + error);

                }
                return null;

            }....

Thanks in advance for the collaboration.

// SOLUTION

if (colocarWallpaper) {

                WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                int flag_lock = WallpaperManager.FLAG_LOCK;

                try {

                    wallpaperManager.setBitmap(img);
                    wallpaperManager.setBitmap(img, null, true, flag_lock);//**

                } catch (Exception error) {
                    Log.e("prueba", "colocar wall error: " + error);

                }

** Using the WallpaperManager and with this line I can make the background change of the lock screen by passing the img (Bitmap)

    
asked by Leonardo Henao 07.11.2017 в 20:50
source

1 answer

3

Perfect. This would be the method that makes magic. See here .

int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, int which)

Now I will explain how to do the correct implementation.

First, add the necessary permissions:

<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />

Now the method that will set the wallpaper according to the parameter:

/**
 * Cambia el fondo de pantalla del home y/o lock screen desde un mapa de bits(Bitmap).
 *
 * @param activity    Actividad del contexto
 * @param bitmap      Mapa de bits para ser aplicado como fondo de pantalla
 * @param statesApply Lista booleana que contiene estados para aplicar un fondo de pantalla. Este
 *                    array debe se ser de solo dos elementos, es decir:
 *                    boolean statesApply[] = new boolean[] {whichSystem, whichLock};
 *                    El primer elemento de su indice indica al whichSystem y el segundo al  whichLock,
 *                    este orden no puede ser alterado por ningun motivo.
 *                    Si {@param statesApply} es null se aplicará para ambos, es decir:
 *                    whichSystem(HomeScreen) = true y whichLock(LockScreem) = true
 *                    NOTA: Este parametro tendrá efecto apartir de la API 24 y posterior.
 * @param fixed       Determina si al momento de aplicar, éste será de tamano fijado por la resolucion del dispositivo o no:
 *                    true --> se aplica con la misma resolucion del dispositivo
 *                    false --> se aplica dependiendo de la resolucion del wallpaper si es muy alta será scrollable.
 * @return true si se aplicó correctamente el fondo de pantalla o false si hubo algún error.
 */
public static boolean setWallaper(Activity activity, Bitmap bitmap, final @Nullable boolean[] statesApply, boolean fixed) {
    boolean success = false;
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(activity);

    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    final int height = metrics.heightPixels;
    final int width = metrics.widthPixels;

    if (bitmap != null) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                boolean whichSystem = true;
                boolean whichLock = true;
                if (statesApply != null) {
                    whichSystem = statesApply[0];
                    whichLock = statesApply[1];
                }
                int which = 0;
                if (whichSystem) which |= WallpaperManager.FLAG_SYSTEM;
                if (whichLock) which |= WallpaperManager.FLAG_LOCK;

                //wallpaperManager.clear();
                if (fixed) {
                    wallpaperManager.setWallpaperOffsetSteps(1, 1);
                    wallpaperManager.suggestDesiredDimensions(width, height);
                }
                wallpaperManager.setBitmap(bitmap, null, true, which);
            } else {
                //wallpaperManager.clear();
                if (fixed) {
                    wallpaperManager.setWallpaperOffsetSteps(1, 1);
                    wallpaperManager.suggestDesiredDimensions(width, height);
                }
                wallpaperManager.setBitmap(bitmap);
            }
            success = true;
        } catch (OutOfMemoryError | IOException | NullPointerException e) {
            if (BuildConfig.DEBUG)
                e.printStackTrace();
        }
    }
    return success;
}

Finally the way to call the previous method to set the wallpaper:

boolean whichSystem = true, whichLock = true;
boolean statesApply[] = new boolean[]{whichSystem, whichLock};
boolean fixed = false;

Utils.setWallaper(MainActivity.this, bitmap, statesApply, fixed);

Final result:

    
answered by 08.11.2017 в 05:24