Switch system language

Generally, the need to switch the system language in your own APP is relatively rare. After all, it is in the system settings, but if you encounter such a requirement in the project, you can switch according to the following code.


public void setLan(Locale locale) {
		//

		Class amnClass = null;
		try {
			amnClass = Class.forName("android.app.ActivityManagerNative");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		Object amn = null;
		Configuration config = null;

		// amn = ActivityManagerNative.getDefault();
		Method methodGetDefault = null;
		try {
			methodGetDefault = amnClass.getMethod("getDefault");
		
		methodGetDefault.setAccessible(true);
		
			amn = methodGetDefault.invoke(amnClass);
		Method methodGetConfiguration = null;
		
			methodGetConfiguration = amnClass.getMethod("getConfiguration");
		
		methodGetConfiguration.setAccessible(true);
		
			config = (Configuration) methodGetConfiguration.invoke(amn);
		}catch (Exception e1) {
			
			e1.printStackTrace();
		}
		// config.userSetLocale = true;
		Class configClass = config.getClass();
		Field f = null;
		
		try {
			f = configClass.getField("userSetLocale");
			f.setBoolean(config, true);
		}  catch (Exception e) {
		
			e.printStackTrace ();
			
		}
		
		Log.d("wzh", "mCurrentLanguage" + mCurrentLanguage);
		// set the locale to the new value
		config.locale = locale;

		// amn.updateConfiguration(config);
		Method methodUpdateConfiguration = null;
		try {
			methodUpdateConfiguration = amnClass.getMethod(
					"updateConfiguration", Configuration.class);
		
		methodUpdateConfiguration.setAccessible(true);
		
			methodUpdateConfiguration.invoke(amn, config);
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
	}

The above code is just a method, you need to switch to whatever language you need to pass any parameters, for example: to switch to Chinese, call setLan(Locale.SIMPLIFIED_CHINESE), and to switch to English, call setLan(Locale.ENGLISH)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325938084&siteId=291194637