首页 > 其他分享 >Android设置语言

Android设置语言

时间:2023-05-06 16:55:53浏览次数:33  
标签:语言 locale updateConfiguration 设置 Android Configuration config Class 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.  

标签:语言,locale,updateConfiguration,设置,Android,Configuration,config,Class,android
From: https://www.cnblogs.com/wanglongjiang/p/17377916.html

相关文章

  • 通过IIS设置来解决System.BadImageFormatException错误
    工作时换了新电脑,然后运行发布后MVC程序就报错:    直接运行Code是OK。错误的原因肯定是64位系统调用了32bit的dll。尝试修改project的Targe为x86,还是无法解决问题。最后查看资料,将应用程序池修改为启用32bit就可以了。 ......
  • R语言中cor函数应用与数据框
     R语言中cor函数应用与数据框,生成以列为基本单位的两两变量之间的相关的矩阵。 01、测试:a<-c(1,3,5,7,9)b<-c(3,8,7,12,23)c<-c(12,23,54,63,89)d<-c(23,45,68,87,234)dat<-data.frame(a,b,c,d)##测试数据dat 002、dattes......
  • 面向过程编程语言特点
    面向过程的编程语言也称为结构化程序设计语言,是高级语言的一种。在面向过程程序设计中,问题被看作一系列需要完成的任务,函数则用于完成这些任务,解决问题的焦点集中于函数。面向过程的编程语言采用自顶向下、逐步求解的程序设计方法,使用三种基本控制结构构造程序,即任何程序都可由顺......
  • 一统天下 flutter - 存储: shared_preferences - 用于操作 android 的 SharedPreferen
    源码https://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-存储:shared_preferences-用于操作android的SharedPreferences,ios的NSUserDefaults,web的LocalStorage示例如下:lib\storage\shared_preferences.dart/**shared_preferences......
  • 编程语言的通用架构——不同的编程语言之间是否存在共性或者说共通之处?能否学会一种编
    本文重点解决如下问题:不同的编程语言之间是否存在共性或者说共通之处?能否学会一种编程语言之后,即可触类旁通的学会其它编程语言?即本文重点描述了不同编程语言之间的通用架构。需要指出的是,为便于读者理解,使用了编程语言的通用架构这一说法,实际上要归纳出种类繁多的编程语言的通用......
  • AndroidStudio插件GsonFormat快速实现JavaBean
    安装方法一:1.AndroidstudioFile->Settings..->Plugins–>Browserepositores..搜索GsonFormat2.安装插件,重启androidstudio方法二:1.下载GsonFormat.jar;2.AndroidstudioFile->Settings..->Plugins–>installpluginfromdisk..导入下载GsonFormat.jar3.重启android......
  • Android开发:使用Glide动态加载圆形图片和圆角图片
    最新消息,鼎鼎大名的Yelp应用也转投Glide的阵营了,而且Glide在跟Listview的配合起来非常的顺畅,Glide除了配置简单,还可以本地缓存图片,也可以实现Listview图片的提前预加载,使得listview的更加的顺滑,具体可以查看Yelp的那篇博文。但是如果碰到要把加载下来的图片转成圆角或者圆形的图......
  • Android开发中的一个小功能 清空搜索框的文字
    需求:项目中的有关搜索的地方,加上清空文字的功能,目的是为了增加用户体验,使用户删除文本更加快捷解决过程:开始的时候感觉这个东西不太好实现,主要就是布局的问题,可能是开始顾虑的太多了,再加上当时产品催的不太紧,而且这个功能也不是必须实现的。但是今天不一样了,这个是老大让加上的,说别......
  • android 获取屏幕高度和宽度 的方法
    Android 获取屏幕高度和宽度的方法  我们需要获取Android手机或Pad的屏幕的物理尺寸,以便于界面的设计或是其他功能的实现。下面就介绍讲一讲如何获取屏幕的物理尺寸   下面的代码即可获取屏幕的尺寸。    在一个Activity的onCreate方法中,写入如下代码:[java] vi......
  • Android 布局设计新思路
    [YouTube](AndroidSpeechRecognition-Part1-YouTube---Android语音识别-第1部分-YouTube)......