【问题标题】:Changing Android Apk Locale via Robotium通过 Robotium 更改 Android Apk 语言环境
【发布时间】:2013-08-07 18:47:13
【问题描述】:

我正在研究一个使用 Robotium 框架自动验证 AUT 的活动检测测试用例。我想自动化几个语言测试。我试图通过 Robotium 更改语言,方法是从 AUT 中提取资源并强制本地语言环境配置为不同的语言,但无济于事:

    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = res.getConfiguration();      
    config.locale = locale;
    res.updateConfiguration(config, res.getDisplayMetrics());

我还听说过去可以使用活动管理器使用 ADB 更改语言,但我一直无法找到适用于 V4.2.2 的有效解决方案。除了在应用程序本身中嵌入代码或对设备进行 root 操作之外,有没有办法通过 Robotium 或其他方式远程更改语言环境?

提前致谢

【问题讨论】:

标签: localization apk robotium adb


【解决方案1】:
protected void changeLocale(Locale locale)
        throws ClassNotFoundException, SecurityException,
        NoSuchMethodException, IllegalArgumentException,
        IllegalAccessException, InvocationTargetException,
        NoSuchFieldException {       
    @SuppressWarnings("rawtypes")
    Class amnClass = Class.forName("android.app.ActivityManagerNative");
    Object amn = null;
    Configuration config = null;

    // amn = ActivityManagerNative.getDefault();
    Method methodGetDefault = amnClass.getMethod("getDefault");
    methodGetDefault.setAccessible(true);
    amn = methodGetDefault.invoke(amnClass);

    // config = amn.getConfiguration();
    Method methodGetConfiguration = amnClass.getMethod("getConfiguration");
    methodGetConfiguration.setAccessible(true);
    config = (Configuration) methodGetConfiguration.invoke(amn);

    // config.userSetLocale = true;
    @SuppressWarnings("rawtypes")
    Class configClass = config.getClass();
    Field f = configClass.getField("userSetLocale");
    f.setBoolean(config, true);

    // set the locale to the new value
    config.locale = locale;

    // amn.updateConfiguration(config);
    Method methodUpdateConfiguration = amnClass.getMethod(
            "updateConfiguration", Configuration.class);
    methodUpdateConfiguration.setAccessible(true);
    methodUpdateConfiguration.invoke(amn, config);
}

您需要在您的应用程序中获得许可:

android.permission.CHANGE_CONFIGURATION

对于 API 级别 >= 17,您必须通过 adb 授予它:

adb shell pm grant application_package android.permission.CHANGE_CONFIGURATION

【讨论】:

  • 非常迟来的感谢您的这篇文章。经过 9 个月的中断,我终于能够尝试这个解决方案,不幸的是,即使在应用程序中获得许可并通过 adb 授予它,我仍然收到 SecurityException。我试图在一个单独的广播接收器中配置语言环境,并且有点工作。语言发生了变化,但应用程序失去了焦点。任何提示,或者我是否需要按照link中的建议使用固件密钥签名
【解决方案2】:

前段时间我也遇到过类似的问题,想到了以下解决方案:

private void changeActivityLocale(final Activity a, String locale ){
    Resources res = a.getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = new Locale(locale);
    res.updateConfiguration(conf, dm);
    a.getResources().updateConfiguration(conf, dm);

    getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            a.recreate();
        }
    });
}

在开始一些特定于语言环境的测试之前,我在我的测试用例中调用了这个方法。 希望对你有帮助。

最好的问候,

彼得

【讨论】:

  • 它在一个简单的测试中对我有用!我喜欢这样,因为我不必向 android 清单添加/修改权限。使用新语言环境重新创建活动的好技巧!
猜你喜欢
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多