11月 262020
本文基于Android8.1系统,应用需要为系统应用
实现方法参考此链接:https://blog.csdn.net/weijuantang/article/details/41379027
通过反射实现设置语言功能的函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
private void updateLanguage(Locale locale) { Log.d("ANDROID_LAB", locale.toString()); try { Object objIAm, objActMagNative; Class ClzAm = ActivityManager.class; ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); Class clzIAM = Class.forName("android.app.IActivityManager"); Class clzAmNative = Class.forName("android.app.ActivityManagerNative"); Method mtdActMagNative$getDefault = clzAmNative.getDeclaredMethod("getDefault"); // IActivityManager iActMag = ActivityManagerNative.getDefault(); Method getIamMethod = ClzAm.getMethod("getService"); objIAm = getIamMethod.invoke(am); ////objIAm = mtdActMagNative$getDefault.invoke(clzAmNative); // Configuration config = iActMag.getConfiguration(); Method mtdIActMag$getConfiguration = clzIAM.getDeclaredMethod("getConfiguration"); Configuration config = (Configuration) mtdIActMag$getConfiguration.invoke(objIAm); config.locale = locale; config.setLocales(new LocaleList(locale)); // iActMag.updateConfiguration(config); // 此处需要声明权限:android.permission.CHANGE_CONFIGURATION // 会重新调用 onCreate(); Method updatePersistentConfigurationMethod = clzIAM.getMethod("updatePersistentConfiguration", Configuration.class); updatePersistentConfigurationMethod.invoke(objIAm, config); Class[] clzParams = { Configuration.class }; Method mtdIActMag$updateConfiguration = clzIAM.getDeclaredMethod( "updateConfiguration", clzParams); mtdIActMag$updateConfiguration.invoke(objIAm, config); } catch (Exception e) { e.printStackTrace(); } } |
调用方法:
1 2 |
updateLanguage(new Locale("zh", "CN")); updateLanguage(new Locale("en", "US")); |
Sorry, the comment form is closed at this time.