如何更改其他应用的语言环境?

问题描述 投票:2回答:1

我可以使用以下代码更改应用的语言环境:

public static void setLocale(Locale locale) 
{
    Locale.setDefault(locale);
    Configuration appConfig = new Configuration();
    appConfig.locale = locale;
    App.context().getResources().updateConfiguration
            (appConfig,App.context().getResources().getDisplayMetrics());
} 

但是如何与其他任何应用程序一起使用?

android locale
1个回答
1
投票

完成!!

我正在与其他用户分享我的代码!它以英文语言环境显示当前活动应用程序的名称:

public static void setLocale(Locale locale, String packageName){
    try{
        Context myAppContext = App.context();
        Context otherAppContext = myAppContext.createPackageContext(packageName, myAppContext.CONTEXT_IGNORE_SECURITY);         
        Locale.setDefault(locale);
        Configuration appConfig = new Configuration();
        appConfig.locale = locale;
        otherAppContext.getResources().updateConfiguration(appConfig, App.context().getResources().getDisplayMetrics());
    }catch(Throwable t){
       History.Error(t);
    }
} 

public static String getActive(){
    try{        
        PackageManager pm = App.context().getPackageManager();
        ActivityManager am = (ActivityManager) App.context().getSystemService(App.context().ACTIVITY_SERVICE);
        RunningTaskInfo taskInfo = am.getRunningTasks(1).get(0);    // The first in the list of RunningTasks is always the foreground task.
        String packageName = taskInfo.topActivity.getPackageName();         
        setLocale(new Locale("en-US"), packageName);        
        PackageInfo appInfo = pm.getPackageInfo(packageName, 0);
        String label = appInfo.applicationInfo.loadLabel(pm).toString();
        App.Toast(label);       
        return  label;
    }catch (Throwable t){
        History.Error(t);
    }
    return "???";
}
© www.soinside.com 2019 - 2024. All rights reserved.