2013-11-04 74 views
2

我可以改變使用此代碼我的應用程序的語言環境:如何更改另一個應用程序的區域設置?

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()); 
} 

,但如何與其他應用程序做到這一點?

+0

這不是系統範圍的設置嗎? – dacwe

回答

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 "???"; 
    } 
+0

爲我節省了大量的時間。謝謝。 – Ray

相關問題