I need to change the language of the Operating system from my application (Installed as System app) and I am using reflection to get access to these classes
public void setSystemLanguage (String langCode)
{
Locale loc;
loc = Locale.forLanguageTag(langCode);
try {
Class<?> activityManagerNative = Class.forName("android.app.ActivityManagerNative");
Object am = activityManagerNative.getMethod("getDefault", new Class[0]).invoke(activityManagerNative, new Object[0]);
Object config = am.getClass().getMethod("getConfiguration", new Class[0]).invoke(am, new Object[0]);
config.getClass().getDeclaredField("locale").set(config, loc);
config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
am.getClass().getMethod("updateConfiguration", new Class[]{Configuration.class}).invoke(am, new Object[]{config});
} catch (Exception e) {
Log.e(LOG_TAG, Log.getStackTraceString(e));
}
}
My Issue is that ActivityManagerNative
,Locale.locale
and ActivityManagerNative.updateConfiguration()
are deprecated.
I tried the following code
Object objIActMag, objActMagNative;
Class clzIActMag = Class.forName("android.app.IActivityManager");
Class clzActMagNative = Class.forName("android.app.ActivityManager");
Method getDefault = clzActMagNative.getDeclaredMethod("getService");
objIActMag = getDefault.invoke(clzActMagNative);
Method getConfiguration = clzIActMag.getDeclaredMethod("getConfiguration");
Configuration config = (Configuration) getConfiguration.invoke(objIActMag);
LocaleList localeList = new LocaleList(locale);
config.setLocale(localeList.get(0));
config.getClass().getDeclaredField("locale").set(config, locale);
config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
Class[] clzParams = { Configuration.class };
Method updateConfiguration = clzIActMag.getDeclaredMethod(
"updateConfiguration", clzParams);
updateConfiguration.invoke(clzIActMag,config);
BackupManager.dataChanged("com.android.providers.settings");
My problem is that I cannot find a replacement for the deprecated method updateConfiguration()
.I found a replacement for this method
Configuration overrideConfiguration = getBaseContext().getResources().getConfiguration();
overrideConfiguration.setLocales(LocaleList);
Context context = createConfigurationContext(overrideConfiguration);
Resources resources = context.getResources();
But I am unsure of how to use the context from here as I need to change the SYSTEM language. Please note that this is not for changing the Application language.
The code to change the Locale in System from LocalePicker.java is
public static void updateLocales(LocaleList locales) {
try {
final IActivityManager am = ActivityManager.getService();
final Configuration config = am.getConfiguration();
config.setLocales(locales);
config.userSetLocale = true;
am.updatePersistentConfiguration(config);
BackupManager.dataChanged("com.android.providers.settings");
} catch (RemoteException e) {
}
}
Please find a replacement for the deprecated method updateConfiguration()
so that I can Apply The changes to the SYSTEM