日常 bug

Android 切换语言的坑

2017-04-10  本文已影响165人  hunter886

在一款 HTC 的测试机中,应用中切换语言之后发现在 Service 中,出现语言没有切换过来的情况。

切换语言的代码如下:

public static void switchLanguage(Context context, Locale locale, boolean saveLocale) {
    if (saveLocale) {
        saveLocale(locale);
    }
    Resources resources = context.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    config.locale = locale;
    resources.updateConfiguration(config, dm);
}

解决办法:

在 Service 的 onBind 在切换一下语言就可以了

@Override
public IBinder onBind(Intent intent) {
    if (!LanguageUtil.isLocaleChanged(this.getApplicationContext())) {
        LanguageUtil.resetDefaultLanguage(this.getApplicationContext());
    }
    return binder;
}
上一篇下一篇

猜你喜欢

热点阅读