Kowlown

SetLocale

Jun 23rd, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. private void setLocale(String localeName) {
  2.         try {
  3.             Locale locale = new Locale(localeName);
  4.             Class amnClass = amnClass = Class.forName("android.app.ActivityManagerNative");
  5.             Object amn = null;
  6.             Configuration config = null;
  7.             // amn = ActivityManagerNative.getDefault();
  8.             Method methodGetDefault = amnClass.getMethod("getDefault");
  9.             methodGetDefault.setAccessible(true);
  10.             amn = methodGetDefault.invoke(amnClass);
  11.             // config = amn.getConfiguration();
  12.             Method methodGetConfiguration = amnClass.getMethod("getConfiguration");
  13.             methodGetConfiguration.setAccessible(true);
  14.             config = (Configuration) methodGetConfiguration.invoke(amn);
  15.             // config.userSetLocale = true;
  16.             Class configClass = config.getClass();
  17.             Field f = configClass.getField("userSetLocale");
  18.             f.setBoolean(config, true);
  19.             // set the locale to the new value
  20.             config.locale = locale;
  21.             // amn.updateConfiguration(config);
  22.             Method methodUpdateConfiguration = amnClass.getMethod("updateConfiguration", Configuration.class);
  23.             methodUpdateConfiguration.setAccessible(true);
  24.             methodUpdateConfiguration.invoke(amn, config);
  25.         } catch (ClassNotFoundException e) {
  26.             e.printStackTrace();
  27.         } catch (NoSuchMethodException e) {
  28.             e.printStackTrace();
  29.         } catch (IllegalAccessException e) {
  30.             e.printStackTrace();
  31.         } catch (InvocationTargetException e) {
  32.             e.printStackTrace();
  33.         } catch (NoSuchFieldException e) {
  34.             e.printStackTrace();
  35.         }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment