轉(zhuǎn)載自:http://yueguc.javaeye.com/blog/824642
現(xiàn)象:Setting -> Language&Locale設置后中文后,大部分應用程序及相關點都變?yōu)橹形?,但Launcher中所有項該是英文還是英文,Reboot后變?yōu)橹形?。原因是Launcher中沒有做相應語言變更處理,做如下修改,添加消息注冊函數(shù),重新加載AllApps項!
enginer@root # git diff
src/com/android/launcher2/Launcher.java
patch | blob| history
src/com/android/launcher2/LauncherModel.java
patch | blob| history
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 84ee599..551a43e 100644 (file)
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -75,7 +75,8 @@
android:stateNotNeeded="true"
android:theme="@style/Theme"
android:screenOrientation="nosensor"
-android:windowSoftInputMode="stateUnspecified|adjustPan">
+android:windowSoftInputMode="stateUnspecified|adjustPan"
+android:configChanges="locale">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.HOME" />
diff --git a/src/com/android/launcher2/Launcher.javab/src/com/android/launcher2/Launcher.java
index 6bd915a..e333bc5 100644 (file)
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -2303,4 +2303,14 @@ public final class Launcher extendsActivity
mAllAppsGrid.dumpState();
Log.d(TAG, "END launcher2 dump state");
}
+
+ // Forcereload all apps list when locale changes
+@Override
+ public voidonConfigurationChanged(Configuration newConfig) {
+super.onConfigurationChanged(newConfig);
+checkForLocaleChange();
+mModel.setAllAppsLoaded(false); // Set force load all appslist;
+mModel.startLoader(this, true); // Reload apps list
+ }
+
}
diff --git a/src/com/android/launcher2/LauncherModel.javab/src/com/android/launcher2/LauncherModel.java
index 17f7573..4915466 100644 (file)
--- a/src/com/android/launcher2/LauncherModel.java
+++ b/src/com/android/launcher2/LauncherModel.java
@@ -86,6 +86,11 @@ public class LauncherModel extendsBroadcastReceiver {
private Bitmap mDefaultIcon;
+ public voidsetAllAppsLoaded(boolean load) {
+mAllAppsLoaded = load;
+ }
+
public interface Callbacks {
public int getCurrentWorkspaceScreen();
public void startBinding();
在 1.6 的 calendar 里,當系統(tǒng)語言改變時, calendar widget 上的語言并沒有隨著改變。其實這個 bug 在android 的很多系統(tǒng)程序里都會出現(xiàn)。只要不是把 string 提取出來的,就都要響應“android.intent.action.CONFIGURATION_CHANGED”,為UI 做一次 update 。
這個 bug 在 HTC的 magic 上同樣會出現(xiàn)。
在 1.6 的SDK 里,“android.intent.action.CONFIGURATION_CHANGED”本來就是有bug 的。網(wǎng)上居然沒人討論過這個問題。我們都知道,可以寫一個 broadcastReceiver 來接受各種各樣的 Notification, 但是,假如你的 broadcastReceiver 的 intentfilter 里允許了接收CONFIGURATION_CHANGED,那好,你的這個 AP 里面就不能再接收其它的通告,要不然,在開機的時候會出現(xiàn)錯誤提示:你的程序沒響應!
不僅如此,假如我接收了ACTION_CONFIGURATION_CHANGED ,但是我還想知道,這到底是由語言改變引起的呢?還是屏幕旋轉(zhuǎn)了?會不是在extra 里給出信息?
我追了一下源碼,在ActivityManagerService.java,有方法updateConfigurationLocked,這里是系統(tǒng)發(fā)送通告的地方:
1 | Intent intent =new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
2 | broadcastIntentLocked( null ,null ,intent, null , null, 0, null ,null , |
3 | null , false, false , MY_PID,Process.SYSTEM_UID); |
是的,我無法知道是怎樣的設置改變。
以上的討論僅限于 1.6 ,這一切在 2.2的 SDK 里不存在了!( 1.6 于2.2 之間的 SDK 就沒考證啦)
同時,在 2.2 里新加了一個通告: ACTION_ CONFIGURATION_CHANGED , 可以通過這個監(jiān)聽到系統(tǒng)語言改變。我也追了一下 2.2 的源碼,在老地方:
01 | Intent intent =new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
02 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
03 | |Intent.FLAG_RECEIVER_REPLACE_PENDING); |
04 | broadcastIntentLocked( null ,null ,intent, null , null, 0, null ,null , |
05 | null , false, false , MY_PID,Process.SYSTEM_UID); |
06 | if((changes&ActivityInfo.CONFIG_LOCALE)!= 0 ) { |
07 | broadcastIntentLocked( null ,null , |
08 | new Intent(Intent.ACTION_LOCALE_CHANGED), |
09 | null , null, 0, null ,null , |
10 | null , false, false , MY_PID,Process.SYSTEM_UID); |
11 | } |
在 froyo 里,當系統(tǒng)語言改變時,會發(fā)出兩個系統(tǒng)通告,分別是 CONFIGURATION_CHANGED 和 ACTION_LOCALE_CHANGED。
還的明確一點變動: 2.2 的ACTION_CONFIGURATION_CHANGED只能通過 r egisterReceiver ()才會被接收, manifest 里寫進去的作廢了。
總結:
如何監(jiān)聽系統(tǒng)語言改變?如果是在activity,直接可以用onConfigurationChanged();如果是舊的SDK,只能接收CONFIGURATION_CHANGED,并且這個通告很不好用!在2.2里,ACTION_LOCALE_CHANGED isenough!
愛華網(wǎng)



