在Android 8.0 系统处理多语言切换,当杀掉应用后再次进来时,发现设置的多语言切换无效
设置的简略代码如下
/**
* 设置语言
*/
public void setConfiguration() {
Locale targetLocale = getLanguageLocale();//获取待设置的语言配置
Configuration configuration = mContext.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(targetLocale);
} else {
configuration.locale = targetLocale;
}
Resources resources = mContext.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
resources.updateConfiguration(configuration, dm);//语言更换生效的代码!
}
对于8.0以下的系统, 上文代码中的 mContext 采用 ApplicationContext 可以正确的切换应用的语言类型
但在8.0 系统中,若 mContext 采用 ApplicationContext 则无法切换应用的语言类型。
处理方法:
在进行应用的语言类型切换时,将 mContext 设置为当前的 Activity 即可
参考资料
记APP实现多语言(国际化)过程,兼容Android 8.0以上
以下是多语言工具整理
/**
* 多语言类型 - 只处理了 英文、简体中文、繁体中文(台湾) 三种类型
* Created by wzq on 2018/7/25.
*/
public enum MultiLanguageType {
CN("cn"),EN("en"),TW("tw");
public String typeKey;
MultiLanguageType(String typeKey){
this.typeKey = typeKey;
}
public static MultiLanguageType match(String code){
if (code == null){
return null;
}
for(MultiLanguageType type: values()){
if(type.typeKey.equals(code)){
return type;
}
}
return null;
}
}
以下是封装的多语言设置的工具类:
/**
* 多语言工具类
* Created by wzq on 2018/8/1.
*/
public class MultiLanguageUtil {
/**
* 获取系统语言类型
*/
public static MultiLanguageType getSystemLanguageType(){
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else {
locale = Locale.getDefault();
}
if (locale.getLanguage().equals("zh")){
if (locale.getCountry().equals("CN")) {
return MultiLanguageType.CN;//简体中文
}else {
return MultiLanguageType.TW;//繁体中文
}
}else if (locale.getLanguage().equals("en")){
return MultiLanguageType.EN;
}
return MultiLanguageType.CN;
}
/**
* 更新本地语言环境
* @param context 应用上下文 (在Android 8.0 以上不能是 ApplicationContext)
*/
public static void autoUpdateLanguageEnviroment(Context context){
//语言环境适配顺序 :用户自定义配置 - app配置 - 系统配置
UserDalex user = UserDalex.get().findById(CrmAppContext.getInstance().getLastOriginalAccount());
MultiLanguageType type = null;
if (user != null){
type = MultiLanguageType.match(user.getLanguagetype());
}
if (type != null){
//优先取用户自定义配置
updateLanguageEnviroment(context,type);
}else if (CrmAppContext.getInstance().getLastLanguageType() != null){
//无用户自定义,则取app配置(上次用户语言环境记录)
updateLanguageEnviroment(context,CrmAppContext.getInstance().getLastLanguageType());
}else {
//取系统语言
updateLanguageEnviroment(context,getSystemLanguageType());
}
}
/**
*
* 更新语言环境
* @param context 应用上下文 (在Android 8.0 以上不能是 ApplicationContext)
* @param languageType 语言类型
*/
public static void updateLanguageEnviroment(Context context, MultiLanguageType languageType){
if (languageType == null || context == null){
return;
}
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
Locale locale;
switch (languageType){
case CN:
locale = Locale.SIMPLIFIED_CHINESE;
break;
case EN:
locale = Locale.ENGLISH;
break;
case TW:
locale = Locale.TRADITIONAL_CHINESE;
break;
default:
locale = Locale.SIMPLIFIED_CHINESE;
languageType = MultiLanguageType.CN;
break;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
config.setLocales(new LocaleList(locale));
}else {
config.locale = locale;
}
resources.updateConfiguration(config, dm);
//同步更新 - app语言环境配置记录更新
CrmAppContext.getInstance().writePreferences(CrmAppContext.Preference.LastLanguageType,languageType.typeKey);
}
}