Android用户设置的语言环境总是在onCreate之后重置?

时间:2010-09-14 18:14:31

标签: android configuration locale

我想在我的应用中设置可配置的语言设置。

所以,在onCreate of my activity中,我调用了Resources.updateConfiguration 使用新的语言环境。

然而,在onCreate之后(在某个时候,我找不到它的时候), locale被设置回默认语言环境。

在下面的代码示例中,主要布局中显示的字符串(如 由setContentView膨胀)显示 “在”语言版本,但当我按下菜单按钮,在上面 onCreateMenu被调用,字符串是 取自“en”(默认)区域设置。

日志显示:

 18337               oncreate  D  { scale=1.0 imsi=525/1 loc=intouch=3 keys=1/1/2 nav=3/1 orien=1 layout=34 uiMode=17 seq=143}
 30430        ActivityManager  I  Displayed activity yuku.coba.locale/.CobaLocaleActivity: 266 ms (total 266 ms)
 18337        KeyCharacterMap  W  No keyboard for id 65540
 18337        KeyCharacterMap  W  Using default keymap: /system/usr/keychars/qwerty.kcm.bin
 18337                 onmenu  D  { scale=1.0 imsi=525/1 loc=en_GB touch=3 keys=1/1/2 nav=3/1 orien=1 layout=34 uiMode=17 seq=143}

在“oncreate”和“onmenu”之间,语言环境神奇地改变了。

请帮忙,我一直在修补这个,没有运气。

代码段:

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       Configuration c = new Configuration();
       c.locale = new Locale("in");
       getResources().updateConfiguration(c, null);

       setContentView(R.layout.main);
       Log.d("oncreate", getResources().getConfiguration().toString());
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       Log.d("onmenu", getResources().getConfiguration().toString());
       new MenuInflater(this).inflate(R.menu.utama, menu);

       return true;
   }

4 个答案:

答案 0 :(得分:1)

每100ms重置配置:)

答案 1 :(得分:0)

我建议不要强制语言环境与设备设置不同。你会遇到问题。您可以将您的呼叫转移到onResume。它会更好,但你仍然要打击设备配置。再次。不要这样做。

答案 2 :(得分:0)

而不是 getResources()。updateConfiguration(c,null);

试 getBaseContext()。getResources()。updateConfiguration(c,null);

(不是getDefaultContext抱歉)

答案 3 :(得分:0)

您可以使用我编写的方法来处理这种情况:

/**
     * Sets app language locale also taking account system resets of the Locale; Resetting of the Locale is done right after onResume of the first Activity that is run in the application.
     * So... if the AppLanugage is set e.g. in first activitys onCreate method, this language will be reset shortly after; To counter this the method starts the Timer (if a flag is set) to
     * handle pottential Locale resets that are done by the system.
     * 
     * In case the flag alsoReloadAfterDelay is set, usually the numberOfResetsOsDoes parameter and the numberOfResetsOsDoes should be used separately from eachother; Either the timer
     * shoudl run till the numberOfResetsOsDoes is matched, or run till the timer runs out of time, irrespectively of the number of resets the os does
     * @param appContext    The application context
     * @param lang  New language to set the locale
     * @param alsoReloadAfterDelay  The flag that says whether to start a timer that will handle pottential Locale resets, that are done by Android OS;
     * @param numberOfResetsOsDoes  The number of resets the OS does after onResume method of the first activity; So far I noticed that it is happening twice (emulator 2.3.3)
     * @param resetTimerDuration    The duration of the reset timer;
     * @see <a href="https://groups.google.com/forum/?hl=en#!topic/android-developers/VEAWMCdyIWg">https://groups.google.com/forum/?hl=en#!topic/android-developers/VEAWMCdyIWg</a> 
     * @return  True if the operation succeded (if the given lang param was appliable for instance); False otherwise
     */
    public static boolean setAppLanguageLocale(final Context appContext, final String lang, boolean alsoReloadAfterDelay, final int numberOfResetsOsDoes, int resetTimerDuration)
    {       
        final String previousAppLanguage = getAppLanguage(appContext);
        final String newLang = lang.toUpperCase(Locale.US);

        if(previousAppLanguage.equals(newLang))
            return true;

        setAppLanguageLocaleP(appContext, lang);

        if(alsoReloadAfterDelay)
        {               
            new CancellableCountDownTimer(resetTimerDuration, 10)
            {
                private int aResetCounter = 0;

                @Override
                public void onTick(long millisUntilFinished)
                {
                    if(aResetCounter == numberOfResetsOsDoes)
                    {
                        this.cancel();
                        return;
                    }

                    String currentAppLanguage = getAppLanguage(appContext);

                    if(!currentAppLanguage.equals(newLang))
                    {
                        aResetCounter++;

                        setAppLanguageLocale(appContext, lang);
                    }
                }

                @Override
                public void onFinish()
                {                   
                }
            }.start();
        }

        return true;
    }