Android设置语言

简介: Android设置语言

Android设置语言


应用语言的切换

单纯的切换自身应用的语言。

Resources resources = getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
config.setLocale(locale);
resources.updateConfiguration(config, dm);

系统语言的切换

切换整个系统的语言。

在6.0的系统中,切换系统语言的方法位于LocalePicker.java文件中(7.0后有变化):

#android/frameworks/base/core/java/com/android/internal/app/LocalePicker.java
public static void updateLocale(Locale locale) {
    try {
        IActivityManager am = ActivityManagerNative.getDefault();
        Configuration config = am.getConfiguration();
        config.setLocale(locale);
        config.userSetLocale = true;
        am.updateConfiguration(config);
        // Trigger the dirty bit for the Settings Provider.
        BackupManager.dataChanged("com.android.providers.settings");
    } catch (RemoteException e) {
        // Intentionally left blank
    }
}

 

我们可以通过下面两种方法来修改系统语言:

1. 修改persist.sys.locale的值

adb shell命令:通过 getprop 和 setprop persist.sys.locale的值来实现语言的切换,但是这种方法需要重启后才能生效。例如:setprop persist.sys.locale zh-CN

2. 程序中通过反射来修改

try {
    Class iActivityManager = Class.forName("android.app.IActivityManager");
    Class activityManagerNative = Class.forName("android.app.ActivityManagerNative");
    Method getDefault = activityManagerNative.getDeclaredMethod("getDefault");
    Object objIActMag = getDefault.invoke(activityManagerNative);
    Method getConfiguration = iActivityManager.getDeclaredMethod("getConfiguration");
    Configuration config = (Configuration) getConfiguration.invoke(objIActMag);
    config.locale = locale;
    Class clzConfig = Class.forName("android.content.res.Configuration");
    java.lang.reflect.Field userSetLocale = clzConfig.getField("userSetLocale");
    userSetLocale.set(config, true);
    Class[] clzParams = {Configuration.class};
    Method updateConfiguration = iActivityManager.getDeclaredMethod("updateConfiguration", clzParams);
    updateConfiguration.invoke(objIActMag, config);
    BackupManager.dataChanged("com.android.providers.settings");
} catch (Exception e) {
    e.printStackTrace();
}

Author:xcz1899 Link: https://www.jianshu.com/p/53b4df276b01 Source: Jane's Book Copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.


目录
相关文章
|
1月前
|
数据库 Android开发
Android 通过升级SettingsProvider数据强制覆盖用户的设置项
Android 通过升级SettingsProvider数据强制覆盖用户的设置项 【5月更文挑战第7天】
42 5
|
17天前
|
传感器 Android开发 UED
Android统一设置页面竖屏
【6月更文挑战第4天】
|
19天前
|
Android开发 UED
|
5天前
|
XML Java Android开发
Android RecyclerView用代码动态设置item的selector
Android RecyclerView用代码动态设置item的selector
10 0
|
5天前
|
开发工具 Android开发 git
Android自定义View——可以设置最大宽高的FrameLayout
Android自定义View——可以设置最大宽高的FrameLayout
18 0
|
5天前
|
JSON Android开发 数据格式
Android动态添加view设置view大小(宽高)
Android动态添加view设置view大小(宽高)
6 0
|
1月前
|
移动开发 Java Android开发
Android应用开发:Kotlin语言的优势与实践
【5月更文挑战第7天】 在移动开发的世界中,Android平台的Kotlin语言以其高效、简洁的语法和强大的功能吸引了众多开发者。本文将深入探讨Kotlin语言的核心优势,并通过实际案例展示如何在Android应用开发中有效地运用这一现代编程语言。我们将从语言特性出发,逐步分析其在提升开发效率、改善代码质量以及增强应用性能方面的具体表现,为读者提供一个全面而细致的Kotlin应用开发指南。
|
1月前
|
XML 搜索推荐 Java
Android TextView的字体设置
【5月更文挑战第13天】
|
1月前
|
Java Shell Android开发
Android11 有线网和wifi优先级设置
Android11 有线网和wifi优先级设置
91 0
|
Android开发 iOS开发 C++
Flutter中设置Android的应用名称和图标(android,ios,web)
在前面的几期给大家介绍了flutter的安装以及一些简单的配置,还运行了helloword 那么接下来就带领大家了解如何设置应用名称以及图标
255 0