Android 在运行时更改区域设置?

Android 在运行时更改区域设置?,android,localization,Android,Localization,我们有一个Android应用程序,可以作为远程PC程序的客户端。我们想添加一个功能,这样电脑可以指示Android应用程序在运行时更改其区域设置,即启动应用程序;将其与PC机进行通信;稍后,电脑会告诉应用程序切换到西班牙语或汉语 我们已经为各自的区域设置了所有布局和字符串资源。我们的应用程序是用户看到的唯一应用程序,因此设备的其余部分是否使用英语并不重要 在这个问题上还有另外一条线索,但似乎还没有得出结论 我可以把 Locale locale = new Locale(sTheNewLocale

我们有一个Android应用程序,可以作为远程PC程序的客户端。我们想添加一个功能,这样电脑可以指示Android应用程序在运行时更改其区域设置,即启动应用程序;将其与PC机进行通信;稍后,电脑会告诉应用程序切换到西班牙语或汉语

我们已经为各自的区域设置了所有布局和字符串资源。我们的应用程序是用户看到的唯一应用程序,因此设备的其余部分是否使用英语并不重要

在这个问题上还有另外一条线索,但似乎还没有得出结论

我可以把

Locale locale = new Locale(sTheNewLocale);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
      getBaseContext().getResources().getDisplayMetrics());

。在setContentView()之前的onCreate()中,但如果我想在屏幕启动并运行后更改区域设置,这并没有真正的帮助。是否有办法在活动已经运行后重新加载内容视图?因此,是否有切实可行的方法来可靠地动态更改区域设置,或者我是否必须告诉我的老板,除了在启动应用程序之前将整个设备设置为新的区域设置外,这是无法完成的?

您可以启动新的活动实例并退出旧的活动实例。下面是一个完整(未经测试)的示例,说明如何存储所需的语言并重新启动应用程序。您只需使用您喜欢的语言调用
restartInLanguage

public class YourMainActivity extends Activity {
    private static final String APP_SHARED_PREFS = "com.example.test";
    private SharedPreferences settings;
    private Editor editor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        settings=getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
        editor=settings.edit();

        Locale locale = new Locale(settings.getString("lang", "default-lang"));
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getResources().updateConfiguration(config, getResources().getDisplayMetrics());

        // your stuff...
    }

    public void restartInLanguage(String lang) {
        editor.putString("lang", lang);
        editor.commit();
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

    // ...
}
由于您可以使用API 11,因此可以在您的活动中使用此方法:

private void restartInLocale(Locale locale)
{
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    Resources resources = getResources();
    resources.updateConfiguration(config, resources.getDisplayMetrics());
    recreate();
}

解决方案是在设置区域设置后,在onResume方法中的每个活动中使用setContentView和controlLanguage(您也可以从全局类调用此方法)方法。 例如:


我结合了@weston和@rekire(两者都是+1)并扩展了它来处理这个用例:

  • ActivityA=>ActivityB=>SettingsActivityChangeLocale
SettingsActivity
中的
changeLocale
之后,还应重新创建父活动
ActivityA
ActivityB
,以反映新的区域设置

我的解决方案:ActivityA和ActivityB继承自
LocalizedActivity
,如果区域设置已更改,它将在恢复时签入
onResume
,并在必要时触发
重新创建()

因此,从
LocalizedActivity
继承的每个活动都会自动处理特定于应用程序的区域设置更改

/**
 * An activity that can change the locale (language) of its content.
 *
 * Inspired by http://stackoverflow.com/questions/13181847/change-the-locale-at-runtime
 *
 * Created by k3b on 07.01.2016.
 */
public class LocalizedActivity extends Activity {
    /** if myLocale != Locale.Default : activity must be recreated in on resume */
    private Locale myLocale = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        fixLocale(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Locale has changed by other Activity ?
        if ((myLocale != null) && (myLocale.getLanguage() != Locale.getDefault().getLanguage())) {
            myLocale = null;
            recreate();
        }
    }

    /**
     * Set Activity-s locale to SharedPreferences-setting.
     * Must be called before onCreate
     * @param context
     */
    public static void fixLocale(Context context)
    {
        final SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        String language = prefs.getString(Global.PREF_KEY_USER_LOCALE, "");
        Locale locale = Global.systemLocale; // in case that setting=="use android-locale"
        if ((language != null) && (!language.isEmpty())) {
            locale = new Locale(language); // overwrite "use android-locale"
        }

        if (locale != null) {
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            Resources resources = context.getResources();
            resources.updateConfiguration(config, resources.getDisplayMetrics());
            // recreate();

            if (context instanceof LocalizedActivity) {
                ((LocalizedActivity) context).myLocale = locale;
            }
        }
    }
}

这是我的

中使用的和的来源,我从哪里执行此操作?程序在主活动中启动,那么这将如何更改主屏幕的区域设置?或者您的意思是从主活动启动主活动的新实例,然后让旧的主活动自行终止?(这能做到吗?)是的,的确如此。。。你把上面的代码放在语言更改处理程序中,它会重新启动你的主要活动。我仍然无法解析你的答案。你是说我可以从我的主要活动重新开始我的主要活动?如何做到这一点?请尝试添加的示例,希望对您有所帮助。方法
updateConfiguration()
已被弃用。这里有一个未弃用的解决方案:config.locale已折旧too@HasanAliKaraca这也包含在链接的答案中。
/**
 * An activity that can change the locale (language) of its content.
 *
 * Inspired by http://stackoverflow.com/questions/13181847/change-the-locale-at-runtime
 *
 * Created by k3b on 07.01.2016.
 */
public class LocalizedActivity extends Activity {
    /** if myLocale != Locale.Default : activity must be recreated in on resume */
    private Locale myLocale = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        fixLocale(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Locale has changed by other Activity ?
        if ((myLocale != null) && (myLocale.getLanguage() != Locale.getDefault().getLanguage())) {
            myLocale = null;
            recreate();
        }
    }

    /**
     * Set Activity-s locale to SharedPreferences-setting.
     * Must be called before onCreate
     * @param context
     */
    public static void fixLocale(Context context)
    {
        final SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        String language = prefs.getString(Global.PREF_KEY_USER_LOCALE, "");
        Locale locale = Global.systemLocale; // in case that setting=="use android-locale"
        if ((language != null) && (!language.isEmpty())) {
            locale = new Locale(language); // overwrite "use android-locale"
        }

        if (locale != null) {
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            Resources resources = context.getResources();
            resources.updateConfiguration(config, resources.getDisplayMetrics());
            // recreate();

            if (context instanceof LocalizedActivity) {
                ((LocalizedActivity) context).myLocale = locale;
            }
        }
    }
}