Android wallpaper setting and confirmation

Android wallpaper setting and confirmation

Android wallpapers include lock screen wallpapers and desktop wallpapers, and wallpapers are divided into static and dynamic.
This article only introduces static wallpaper setting and confirmation.

First of all, Android phone wallpaper and desktop Launcher are separate and are two different applications.
Wallpaper also has related Manager and Service in the system Framework;
the default background of Android Launcher is transparent, covering the wallpaper. The
simple understanding is Launcher is an apk that displays many application icons.

After the Android wallpaper is set, it will save the path:

/data/system/users/0/wallpaper

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

1. Wallpaper settings

(1) Code wallpaper settings

Permissions need to be declared in AndroidManifest.xml:

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

Set by WallpaperManager

This method can directly set the picture as the wallpaper, and is used for Android systems of all platforms.

Set the Bitmap object

try {
    WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(Context.WALLPAPER_SERVICE);
    if (wpm != null) {
        Bitmap mBitmap = BitmapFactory.decodeFile(path); //path为绝对路径
        //第一个参数是Bitmap对象,第二个参数是截取图片的大小矩形,第三个参数是是否备份
        wpm.setBitmap(mBitmap, new Rect(0, 0, right, bottom), true); 
        Log.i("liwenzhi", "wallpaper not null");
    }
} catch (IOException e) {
    Log.e(TAG, "Failed to set wallpaper: " + e);
}

Note that Android 10 and later versions do not even have permission to read sdcard files, because a sandbox mechanism has been added.

But there are ways to solve it, 1 is to add system signature android.uid.system, 2 is to add WRITE_MEDIA_STORAGE permission

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

    //Android10及以后的版本,需要额外添加的权限
    <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE"
        tools:ignore="ProtectedPermissions" />


Another way is to set the Source object

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


try {
    WallpaperManager wpm = WallpaperManager.getInstance(context);//同getActivity().getSystemService(Context.WALLPAPER_SERVICE);
    wpm.setResource(getResources().getIdentifier(name, "drawable", context.getPackageName()));
} catch (IOException e) {
    Log.e("TAG","error = " + e.getMessage());
}


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

(2) adb wallpaper settings

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

//root
adb root

//拉到电脑的D盘temp目录下,电脑中看
adb push D:/temp/wallpaper.png /data/system/users/0/wallpaper 

//拉到sdcard根目录,手机应用中看
adb push /sdcard/wallpaper.png /data/system/users/0/wallpaper

//需要重启才能生效
adb root

2. Wallpaper verification

Requires permissions (i.e. system apps, root permissions)!

(1) Verification in the system application code

Open the wallpaper file

//显示壁纸图片,需要系统签名
    public void showWallpaper(View view) {
        try {
            Bitmap bitmap = BitmapFactory.decodeFile("/data/system/users/0/wallpaper");
            iv_wallpaper.setImageBitmap(bitmap);
        } catch (Exception e) {
            Log.e(TAG, "showWallpaper error = " + e.getMessage());
            tv_info.append("showWallpaper error = " + e.getMessage());
        }
    }
    
    

    //把壁纸pull到sdcard目录,需要系统签名
    public void pullWallpaperToSdcard(View view) {
        Log.i(TAG, "pullWallpaperToSdcard start");
        File fromFile = new File("/data/system/users/0/wallpaper");
        File toFile = new File("/sdcard/" + getTimeString() + "_wallpaper.png");
        copyFile(fromFile, toFile);
        Log.i(TAG, "pullWallpaperToSdcard end");
    }
    
    //获取当前时间的完整显示字符串
    private String getTimeString() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        return format.format(new Date(System.currentTimeMillis()));
    }

    //复制文件操作
    private void copyFile(File fromFile, File toFile) {
        try {
            if (!toFile.getParentFile().exists()) {
                toFile.getParentFile().mkdirs();
            }

            java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile);
            java.io.FileOutputStream fosto = new FileOutputStream(toFile);
            byte bt[] = new byte[1024];
            int c;
            while ((c = fosfrom.read(bt)) > 0) {
                fosto.write(bt, 0, c); //将内容写到新文件当中
            }
            fosfrom.close();
            fosto.close();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            tv_info.append("copyFile error = " + e.getMessage());
        }
    }


(2) adb copy file verification


//root权限
adb root

//拉到电脑的D盘temp目录下,电脑中看
adb pull /data/system/users/0/wallpaper D:/temp/wallpaper.png

//拉到sdcard根目录,手机应用中看
adb pull /data/system/users/0/wallpaper D:/temp/wallpaper.png

(3) APK verification, please see the attachment

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

Simply set the wallpaper to verify the apk resource of the wallpaper:
https://download.csdn.net/download/wenzhi20102321/82630130

3. Others

(1) The wallpaper setting is invalid

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

(2) Relationship between wallpaper and desktop Launcher

It doesn't matter. But the Launcher can override the wallpaper.

Wallpapers are related to system services and 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 of wallpaper setting

Interested to see: https://blog.csdn.net/lj527409/article/details/79825015

(4) System default wallpaper setting failed?


默认壁纸路径:frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.png

It should be noted that there are multiple drawable folders in the same directory of res, and some of them also have default_wallpaper.png pictures.

If multiple default_wallpaper.png pictures in the drawable are replaced and have no response,
then you need to see where the system source code is wrong, add some prints to see the specific situation analysis,
there is code logic for reading default_wallpaper.png pictures, for example Keyword: R.drawable.default_wallpaper.

Encouragement: Life is too short to live.

Guess you like

Origin blog.csdn.net/wenzhi20102321/article/details/123123609