SoFunction
Updated on 2025-03-11

Android development wallpaper verification settings and confirmation functions to implement demo

Preface

First of all, Android mobile wallpaper Wallpaper and desktop Launcher are separate, and are two different applications

Wallpaper also has related managers and services in the system Framework;

Android Launcher default background is transparent, overlaid on wallpaper

A simple understanding is that Launcher is an apk that displays many application icons.

The path will be saved after setting the Android wallpaper:

/data/system/users/0/wallpaper

This wallpaper is a file that can be pulled to the computer to add the suffix.png to view pictures.

1. Wallpaper settings

(1) Code wallpaper settings

Authorization required:

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

Settings via WallpaperManager

This method can directly place the picture as a wallpaper and use it for Android systems on all platforms.

Setting up Bitmap Objects

try {
    WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(Context.WALLPAPER_SERVICE);
    if (wpm != null) {
        Bitmap mBitmap = (path); //path is an absolute path        //The first parameter is the Bitmap object, the second parameter is the size rectangle of the image, and the third parameter is whether to back up        (mBitmap, new Rect(0, 0, right, bottom), true); 
        ("liwenzhi", "wallpaper not null");
    }
} catch (IOException e) {
    (TAG, "Failed to set wallpaper: " + e);
}

For Android 10 and later versions, you should pay attention to that ordinary applications do not even have permission to read the sdcard file because the sandbox mechanism is added.

But there is a way to solve it.

1 is to add system signature

2 is to add WRITE_MEDIA_STORAGE permission

    &lt;uses-permission android:name = ".SET_WALLPAPER"/&gt;
    &lt;uses-permission android:name=".READ_EXTERNAL_STORAGE"/&gt;
    &lt;uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/&gt;
    //Android 10 and later versions require additional permissions    &lt;uses-permission android:name=".WRITE_MEDIA_STORAGE"
        tools:ignore="ProtectedPermissions" /&gt;

Another way is to set the Source object

Refers to the image files inside the application, such as the raw directory or drawable directory.
And only supports PNG or JPEG format images.

try {
    WallpaperManager wpm = (context);//Same as getActivity().getSystemService(Context.WALLPAPER_SERVICE);    (getResources().getIdentifier(name, "drawable", ()));
} catch (IOException e) {
    ("TAG","error = " + ());
}

This method does not require read permission, only SET_WALLPAPER permission is enough.

(2) Adb wallpaper settings

Requires root permissions! And it needs to be restarted once before the effect is seen, because the system refresh is not called.

//root
adb root
//Pull it into the computer's D drive temp directory and view it on the computeradb push D:/temp/ /data/system/users/0/wallpaper 
//Pull to the root directory of sdcard and view it in the mobile applicationadb push /sdcard/ /data/system/users/0/wallpaper
//Restart required to take effectadb root

2. Wallpaper verification

Permissions are required (i.e. system application, root permissions)!

(1) Verification in the system application code

Open wallpaper file

//Show wallpaper pictures, system signature is required    public void showWallpaper(View view) {
        try {
            Bitmap bitmap = ("/data/system/users/0/wallpaper");
            iv_wallpaper.setImageBitmap(bitmap);
        } catch (Exception e) {
            (TAG, "showWallpaper error = " + ());
            tv_info.append("showWallpaper error = " + ());
        }
    }
    // Pull the wallpaper to the sdcard directory, the system signature is required    public void pullWallpaperToSdcard(View view) {
        (TAG, "pullWallpaperToSdcard start");
        File fromFile = new File("/data/system/users/0/wallpaper");
        File toFile = new File("/sdcard/" + getTimeString() + "_wallpaper.png");
        copyFile(fromFile, toFile);
        (TAG, "pullWallpaperToSdcard end");
    }
    //Get the full display string for the current time    private String getTimeString() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        return (new Date(()));
    }
    //Copy file operation    private void copyFile(File fromFile, File toFile) {
        try {
            if (!().exists()) {
                ().mkdirs();
            }
             fosfrom = new (fromFile);
             fosto = new FileOutputStream(toFile);
            byte bt[] = new byte[1024];
            int c;
            while ((c = (bt)) &gt; 0) {
                (bt, 0, c); //Write the content into a new file            }
            ();
            ();
        } catch (Exception e) {
            (TAG, ());
            tv_info.append("copyFile error = " + ());
        }
    }

(2) Adb copy file verification

//root permissionsadb root
//Pull it into the computer's D drive temp directory and view it on the computeradb pull /data/system/users/0/wallpaper D:/temp/
//Pull to the root directory of sdcard and view it in the mobile applicationadb pull /data/system/users/0/wallpaper D:/temp/

(3) Apk verification, please see the attachment

This can only replace wallpaper. If you want to display and pull files, you need to replace the signature file in the project.

Simple setting of wallpaper verification wallpaper apk resources click to download

3. Others

(1) The wallpaper setting is invalid

Related to Launcher, the Launcher interface is covered on the wallpaper.

(2) The relationship between wallpaper and desktop Launcher

It doesn't matter. But Launcher can overwrite wallpaper.

Wallpapers are related to system services, as well as SystemUi.

If you delete the /data/system/users/0/wallpaper file, you will find that the background of the wallpaper is black.

(3) System code flow for wallpaper settings

If you are interested, please read:https:///article/

(4) The system default wallpaper setting failed?

Default wallpaper path:

frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.png

Note that there are multiple drawable folders in the same directory as res, and some also have default_wallpaper.png images.

If you replace multiple default_wallpaper.png images in drawable, you will not respond

Then you need to see where the system source code is wrong, add more prints to see how to analyze the specific situation

There is the code logic for reading default_wallpaper.png images, such as keywords:

.default_wallpaper

The above is the detailed content of the verification settings and confirmation function of Android development wallpaper. For more information about the verification settings of Android development wallpaper, please pay attention to my other related articles!