private void updateLanguage(Locale locale) { try { Object objIActMag, objActMagNative; Class clzIActMag = Class.forName("android.app.IActivityManager"); Class clzActMagNative = Class.forName("android.app.ActivityManagerNative"); Method mtdActMagNative$getDefault = clzActMagNative.getDeclaredMethod("getDefault"); // IActivityManager iActMag = ActivityManagerNative.getDefault(); objIActMag = mtdActMagNative$getDefault.invoke(clzActMagNative); // Configuration config = iActMag.getConfiguration(); Method mtdIActMag$getConfiguration = clzIActMag.getDeclaredMethod("getConfiguration"); Configuration config = (Configuration) mtdIActMag$getConfiguration.invoke(objIActMag); config.locale = locale; // iActMag.updateConfiguration(config); // 此处需要声明权限:android.permission.CHANGE_CONFIGURATION // 会重新调用 onCreate(); Class[] clzParams = { Configuration.class }; Method mtdIActMag$updateConfiguration = clzIActMag.getDeclaredMethod( "updateConfiguration", clzParams); mtdIActMag$updateConfiguration.invoke(objIActMag, config); } catch (Exception e) { e.printStackTrace(); } }
参数从Local中获取,例如:Local.ENGLISH;
使用之前应该root系统,adb shell
->su
然后# pm grant com.example.clickdemo android.permission.CHANGE_CONFIGURATION手动赋予权限,否则会报反射异常