亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開發(fā)壁紙的驗證設置和確認功能實現(xiàn)demo

 更新時間:2022年04月27日 17:31:54   作者:崢嶸life  
android?wallpaper包括鎖屏壁紙和桌面壁紙,壁紙又區(qū)分靜態(tài)和動態(tài)兩種。本文詳細介紹靜態(tài)壁紙設置和確認,有需要的朋友可以借鑒參考下,希望能夠有所幫助

前言

首先說一下,Android手機壁紙Wallpaper和桌面Launcher是分開的,是兩個不同的應用

Wallpaper在系統(tǒng)Framework也有相關Manager和Service;

Android Launcher默認背景是透明的,覆蓋在壁紙之上

簡單的理解就是Launcher就是一個顯示了很多應用圖標的apk。

Android壁紙設置后會存路徑:

/data/system/users/0/wallpaper

這個wallpaper是一個文件,可以pull到電腦中添加后綴.png查看圖片。

一、壁紙設置

(1)代碼壁紙設置

AndroidManifest.xml中需要申明權限:

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

通過WallpaperManager設置

該方法可以直接將圖片置為壁紙,對于所有平臺的Android系統(tǒng)都使用。

設置Bitmap對象

try {
    WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(Context.WALLPAPER_SERVICE);
    if (wpm != null) {
        Bitmap mBitmap = BitmapFactory.decodeFile(path); //path為絕對路徑
        //第一個參數(shù)是Bitmap對象,第二個參數(shù)是截取圖片的大小矩形,第三個參數(shù)是是否備份
        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);
}

Android 10及以后的版本要注意,普通應用連sdcard文件讀取的權限都沒有,因為加了沙箱機制了。

不過也是有方法解決的

1是添加系統(tǒng)簽名android.uid.system

2是添加WRITE_MEDIA_STORAGE權限

    <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" />

還有一種方法是設置Source對象

指的是應用內(nèi)部的圖片文件,比如raw目錄或者drawable目錄,的文件
并且只支持PNG or JPEG格式的圖片。

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

該方式不需要讀取權限,只要SET_WALLPAPER權限即可。

(2)adb壁紙設置

需要root權限!而且需要重啟一次,才能看到效果,因為沒有調(diào)用到系統(tǒng)刷新。

//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

二、壁紙驗證

需要權限(即系統(tǒng)應用、root權限)!

(1)系統(tǒng)應用代碼中驗證

打開壁紙文件

//顯示壁紙圖片,需要系統(tǒng)簽名
    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目錄,需要系統(tǒng)簽名
    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); //將內(nèi)容寫到新文件當中
            }
            fosfrom.close();
            fosto.close();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
            tv_info.append("copyFile error = " + e.getMessage());
        }
    }

(2)adb 復制文件驗證

//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驗證,請看附件

這個wallpaper.apk只能替換壁紙,若要顯示和pull文件需要簽名文件,替換項目中的簽名文件即可。

簡單的設置壁紙驗證壁紙的apk資源點擊下載

三、其他

(1)壁紙設置無效

和Launcher相關,Launcher界面是覆蓋在壁紙之上的。

(2)壁紙和桌面Launcher的關系

沒啥關系。但是Launcher可以覆蓋壁紙。

壁紙是與系統(tǒng)服務,以及SystemUi相關的。

你把/data/system/users/0/wallpaper文件刪除,會發(fā)現(xiàn),壁紙背景是黑色的。

(3)壁紙設置的系統(tǒng)代碼流程

有興趣的看:http://chabaoo.cn/article/44216.htm

(4)系統(tǒng)默認壁紙設置失?。?/h3>

默認壁紙路徑:

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

要注意res同一個目錄下有多個drawable文件夾,有的里面也有default_wallpaper.png圖片。

如果是替換了drawable里面多個default_wallpaper.png圖片沒用反應

那么要看看系統(tǒng)源碼哪里出錯了,多加一些打印看看進行具體情況分析

里面是有讀取default_wallpaper.png圖片的代碼邏輯,比如關鍵字:

R.drawable.default_wallpaper

以上就是Android開發(fā)壁紙的驗證設置和確認功能實現(xiàn)demo的詳細內(nèi)容,更多關于Android開發(fā)壁紙驗證設置確認的資料請關注腳本之家其它相關文章!

相關文章

最新評論