Android - 以编程方式更改设备系统区域设置

问题描述 投票:8回答:2

我想从我的应用程序中更改设备区域设置(不仅仅是应用程序区域设置),就像用户可以在“设置” - >“语言和键盘” - >“语言”中进行操作一样。

有人可以解释一下怎么做吗?我一直在寻找几个小时,找不到办法。

android locale
2个回答
6
投票

这个程序确实想要你想要https://play.google.com/store/apps/details?id=org.gnvo.langpicker&hl=en

这是它的源代码https://code.google.com/p/languagepickerwidget/

这是它的主要逻辑http://bin-liu.blogspot.in/2012/05/how-to-change-system-locale-calling.html

编辑

在4.2新的Jelly Bean之后,CHANGE_CONFIGURATION的保护级别定义已经改变,因此应用程序将无法在4.2以上工作,解决方案是http://droider.eu/2013/07/22/morelocale-2-not-working-on-4-2-2-without-su-or-pm/


4
投票

是的,您可以更改任何Android版本的设备区域设置,确保您的应用程序是系统应用程序。有代码可以帮助您。如果您喜欢,请给我的答案评分。

//getting the languages that are shown same as in our device settings
     String[] systemLocaleIetfLanguageTags = getAssets().getLocales();
            Arrays.sort(systemLocaleIetfLanguageTags);
            this.locale_data = new ArrayList();
            for (String ietfLanguageTag : systemLocaleIetfLanguageTags)
            {
                if (ietfLanguageTag != null && ietfLanguageTag.length() == 5)
                {
                    this.locale_data.add(new Loc(ietfLanguageTag));
                }
            }
            adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, locale_data);
            // Assign adapter to ListView
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
                {
                    int itemPosition = position;
                    Loc loc = (Loc) ChangeLanguage.this.adapter.getItem(position);
                    Toast.makeText(ChangeLanguage.this, "language activated successfully !!", 0).show();
                    ChangeLanguage.change_setting(loc.getLocale());
                }
            });
        }
    //to change the locale
        public static void change_setting(Locale loc)
        {
            try
            {
                Class<?> activityManagerNative = Class.forName("android.app.ActivityManagerNative");
                Object am = activityManagerNative.getMethod("getDefault", new Class[0]).invoke(activityManagerNative, new Object[0]);
                Object config = am.getClass().getMethod("getConfiguration", new Class[0]).invoke(am, new Object[0]);
                config.getClass().getDeclaredField("locale").set(config, loc);
                config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
                am.getClass().getMethod("updateConfiguration", new Class[] { Configuration.class }).invoke(am, new Object[] { config });
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.