如何通过代码自动更改android系统的语言环境

时间:2015-11-09 03:46:43

标签: android automated-tests locale robotium

我有应用程序的apk文件(没有源代码,是robotium UI自动测试)。我需要通过代码自动更改系统区域设置。谁能给我一个解决这个问题的机制或方法?

有人向我推荐了这段代码:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.LanguageSettings");            
startActivity(intent);

它帮助我打开设置 - >语言&输入,但我需要通过代码自动选择一种语言(因为它需要机器人自动化测试)。

请给我一个具体的解决方案。

由于

1 个答案:

答案 0 :(得分:1)

如果您想要更改应用语言,请执行以下操作:

Resources res = getResources();
                // Change locale settings in the app.
                DisplayMetrics dm = res.getDisplayMetrics();
                android.content.res.Configuration conf = res.getConfiguration();
                conf.locale = new Locale(/* your language here */);
                res.updateConfiguration(conf, dm);
相关问题