Android 切换语言的坑

在一款 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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容