如果要在代码中切换 Android 设备的语言并重启应用程序,可以使用以下代码:
// 获取系统配置对象
Configuration config = getResources().getConfiguration();
// 设置新的语言
config.locale = new Locale("en");
// 更新配置
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
// 重启应用程序
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
注意,需要自己实现MainActivity.class,更改"en"为需要切换的语言