美文网首页日常 bug
Android 切换语言的坑

Android 切换语言的坑

作者: hunter886 | 来源:发表于2017-04-10 18:33 被阅读165次

    在一款 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;
    }
    

    相关文章

      网友评论

        本文标题:Android 切换语言的坑

        本文链接:https://www.haomeiwen.com/subject/miceattx.html