Launcher3自訂壁紙旋轉後展開無法恢複,launcher3展開

來源:互聯網
上載者:User

Launcher3自訂壁紙旋轉後展開無法恢複,launcher3展開

著作權,轉載請註明出處:

http://www.cnblogs.com/sickworm/p/3966857.html 

 

MTK8382/8121平台。

 

描述:將自訂圖片設定成壁紙後,橫屏顯示時,旋轉為豎屏,圖片由於解析度過小,會展開;再旋轉為橫屏,展開不恢複。

這兩天正在解這個問題,研究了很久,走了不少彎路,最後發現是Launcher讀取SharePreferences時的一個bug。

 

bug是這樣產生的:

Launcher3設定完自訂壁紙(系統內建壁紙不會記錄)的時候,會在com.android.launcher3.WallpaperCropActivity.xml中記錄被設定壁紙的解析度,並提交解析度給WallpaperManager(通過suggestWallpaperDimension())。具體函數是:WallpaperCropActivity.java中的updateWallpaperDimensions(),它被WallpaperCropActivity.java的setWallpaper()調用;

Launcher3每次旋轉後會重新執行onCreate(),同時會提交當前壁紙的解析度給WallpaperManager,提交解析度的函數在Workspace.java中的setWallpaperDimension()中。問題在這裡:setWallpaperDimension()無法擷取之前updateWallpaperDimensions()修改的SharedPreferences,導致它提交的是預設的壁紙解析度1920x1080,從而導致低解析度的壁紙展開。

解決此問題的方法是:修改Workspace.java中的setWallpaperDimension()中的getSharedPreferences()的flag,把MODE_PRIVATE改為MODE_MULTI_PROCESS。修改後成功訪問。

 

我的問題是:

根據Android Developer的解釋:

MODE_PRIVATE:

File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).

即MODEL_PRIVATE只能被同一個application或者同一個userID的application調用。按這個說法,這兩個Activity應該是可以共同訪問的。(Workspace.java使用的是Launcher.java的context,兩個Activity pid不一樣,uid一樣)

同時我還看了MODE_MULTI_PROCESS的解釋:

MODE_MULTI_PROCESS:

SharedPreference loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process. This behavior is sometimes desired in cases where the application has multiple processes, all writing to the same SharedPreferences file. Generally there are better forms of communication between processes, though.

This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targetting such releases. For applications targetting SDK versions greater than Android 2.3, this flag must be explicitly set if desired.

意思是這個flag用於給擁有多個進程的application共同訪問同一個SharedPreferences使用的。按照這個說法,似乎又確實應該使用MODEL_MULTI_PROCESS。

 

於是我想找到getSharedPreference實現代碼,看看它怎麼處理這幾個flag。可惡的是,從Activity父類一級一級往上找,都找不到實現的方法,直到找到這篇文章:

http://blog.csdn.net/qinjuning/article/details/7310620

才知道ContextImpl實現了Context的具體方法,進而找到了答案:

ContextImpl類中有getSharedPreferences的實現。裡面說明了在MODE_MULTI_PROCESS標誌中,getSharedPreferences會進行reload。換言之MODE_PRIVATE不會重新讀取SharedPreferences。

這裡終於搞懂他的意思:在之前的bug,並不是SharedPreferences擷取失敗,而是因為沒有reload所以沒有擷取到新寫入的解析度資訊。因為之前沒有注意到這個問題,所以走了彎路。

不過還有問題:每次Launcher旋轉的時候都會重新啟動Activity調用onCreate,為什麼我getSharePreferences還是舊的呢?繼續觀察android.app.ContextImpl的getSharedPreferences:

@Overridepublic SharedPreferences getSharedPreferences(String name, int mode) {Log.w("Launcher", "contextImpl: " + this, new RuntimeException("getSp").fillInStackTrace());SharedPreferencesImpl sp;synchronized (ContextImpl.class) {if (sSharedPrefs == null) {Log.e("Launcher", "all init");sSharedPrefs = new ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>>();}final String packageName = getPackageName();ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName);if (packagePrefs == null) {Log.e("Launcher", "package init");packagePrefs = new ArrayMap<String, SharedPreferencesImpl>();sSharedPrefs.put(packageName, packagePrefs);}// At least one application in the world actually passes in a null// name. This happened to work because when we generated the file name// we would stringify it to "null.xml". Nice.if (mPackageInfo.getApplicationInfo().targetSdkVersion <Build.VERSION_CODES.KITKAT) {if (name == null) {name = "null";}}sp = packagePrefs.get(name);if (sp == null) {File prefsFile = getSharedPrefsFile(name);sp = new SharedPreferencesImpl(prefsFile, mode);packagePrefs.put(name, sp);Log.e("Launcher", "new sp");return sp;}Log.e("Launcher", "old sp");}if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {// If somebody else (some other process) changed the prefs// file behind our back, we reload it. This has been the// historical (if undocumented) behavior.sp.startReloadIfChangedUnexpectedly();Log.e("Launcher", "reload");}return sp;}

裡面Log.e("Launcher", ...);是我自己加的調試資訊。首先會判斷sSharedPrefs是否有內容,然後從中擷取對應package的prefsFile。如果sSharedPrefs找不到,才從xml檔案中重新讀取。最後加了一個判斷,如果設定了MODE_MULTI_PROCESS變數,或者Android 2.2以下的系統,會預設從xml檔案中重新reload,以保持最新的SharedPreferences資料。

注意這裡的sSharedPrefs變數,它只在getSharedPreferences中有賦值,也就是說SharedPreferences的資料一直跟隨著ContextImpl執行個體走,只從getSharedPreferences()方法中擷取資料。也就是說,當旋轉螢幕的時候,我們調用getSharedPreferences()擷取的資料都是從這個sSharedPrefs變數中取出來的。實際從下面Log資訊也可以看到,旋轉並不會有"new sp"的Log列印,只有對Launcher3在設定中force stop和clear data的時候才會出現"new sp"。

旋轉的Log提示:

09-11 08:47:19.599: E/Launcher(4628): launcher:com.android.launcher3.Launcher@42392ac809-11 08:47:19.599: E/Launcher(4628): mBase:android.app.ContextImpl@424c189809-11 08:47:19.608: E/Launcher(4628): old sp09-11 08:47:19.727: E/Launcher(4628): old sp09-11 08:47:19.731: E/Launcher(4628): reload09-11 08:47:19.932: E/Launcher(4628): old sp

force stop的提示:

09-11 08:48:29.456: E/Launcher(5271): launcher:com.android.launcher3.Launcher@4238f22009-11 08:48:29.456: E/Launcher(5271): mBase:android.app.ContextImpl@42391ab809-11 08:48:29.489: E/Launcher(5271): all init09-11 08:48:29.489: E/Launcher(5271): package init09-11 08:48:29.493: E/Launcher(5271): new sp09-11 08:48:29.679: E/Launcher(5271): new sp09-11 08:48:29.766: E/Launcher(5271): old sp09-11 08:48:29.767: E/Launcher(5271): old sp09-11 08:48:29.790: E/Launcher(5271): old sp

裡面三個連著的old sp,只有第二個是讀取解析度的,其他兩個分辨是LauncherAppState的SharedPreferences。因為此時Launcher3代碼已經被我修改為MODE_MULTI_PROCESS,所以旋轉會打出"reload"資訊。

 

也就是說,旋轉的時候sSharedPrefs的值是一直儲存著的。可是通過Log列印資訊,我們發現,每一次的getSharedPreferences()的contextImpl都是不一樣的!

這裡要注意的是,執行方法的context是Activity的父類ContextThemeWrapper的mBase私人成員執行的,擷取mBase可以在getSharedPreferences()中列印this出來,也可以在Activity中進行反射,這裡用的是反射方法:

try {Log.e("Launcher", "launcher:" + this);java.lang.reflect.Field f = Activity.class.getSuperclass().getDeclaredField("mBase");f.setAccessible(true);Context s = (Context) f.get(this);Log.e("Launcher", "mBase:" + s);} catch(Exception e) {e.printStackTrace();}

contextImpl不一樣了,但是sSharedPrefs資料還儲存著,且sSharedPrefs不能通過其他方法賦值,只能猜測在旋轉的時候通過原型模式把原來的context傳進去了。Launcher3的旋轉處理,我只找到ACTION_CONFIGURATION_CHANGE這個廣播,但是我把裡面的代碼注釋了,依然可以正常工作。估計是更底層的代碼控制的。

 

具體代碼還沒找到,因為還要搬磚。有空再研究吧!


800*480 安卓23怎設定壁紙正好合適?(不被展開之類的)

首先你要下載專業的解析度匹配的壁紙。建議你用豌豆莢裡面的壁紙資源,因為都是和你手機相匹配的,包括動態還是靜態。。。這個一般都沒有問題。
但是如果你想使用多景案頭的壁紙,那就比較麻煩了。我的多景案頭現在所有的原有壁紙都不能實現適合螢幕,需要自己調整或者。
 
三星I9300高仿機,我把系統內建的launcher3的啟動器卸了,而後介面不動,我重啟後一直開機關機在開機介面

是不是主叫方通過嘗試刪除已安裝的撥號器。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.