2017-05-19 103 views
-2

我正在使用谷歌地圖在我的活動。 當我在我的應用程序設置 中更改語言例如中文時,地圖的語言沒有改變,直到我重新打開我的應用程序。谷歌地圖緩存問題

Locale locale = new Locale(languageToLoad); 
Locale.setDefault(locale); 
Configuration config = new Configuration(); 
config.locale = locale; 
getBaseContext().getResources().updateConfiguration(config, 
     getBaseContext().getResources().getDisplayMetrics()); 

setContentView(R.layout.activity_maps); 

我搜索了很多關於這個問題,我發現它是谷歌地圖緩存問題,但沒有找到任何解決方案。

+0

您是否嘗試過在onConfigurationChanged活動的方法來處理這個問題? – MatPag

+0

我試過這個但不工作.....即使它不工作,以防萬一我重新打開活動......它只適用於應用程序重新打開。 – Manish

回答

1

我能想到約2選項來解決這個問題:

  1. 在onConfigurationChanged方法調用的setContentView

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
        super.onConfigurationChanged(newConfig); 
        if (language_changed){//pseudo_code 
         Locale locale = new Locale(languageToLoad); 
         Locale.setDefault(locale); 
         Configuration config = new Configuration(); 
         config.locale = locale; 
         getBaseContext().getResources().updateConfiguration(config, 
           getBaseContext().getResources().getDisplayMetrics()); 
    
         setContentView(R.layout.activity_maps); 
         //now you need to do again all the findViewById call 
        } 
    } 
    

  • 重新啓動onConfigurationChanged方法中的活動

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
        super.onConfigurationChanged(newConfig); 
        if (language_changed){//pseudo_code 
         startActivity(CurrentActivity.this, CurrentActivity.class); 
         finish(); 
        } 
    } 
    
  • 我沒有測試這一點,但也許這是一個起點,來嘗試解決您的問題。

    不要忘記將android:configChanges="locale"添加到AndroidManifest.xml中的活動標記中,讓系統知道您想自己處理區域設置更改。如果需要,請閱讀here

    +0

    感謝您的回答,但它不工作...我已經嘗試過它..甚至當我手動重新啓動活動它不工作...只適用於應用程序重新打開 – Manish

    +0

    你是否在活動標記中添加了configChanges屬性?閱讀我的回答的最後2行 – MatPag

    +0

    是嘗試過但沒有工作... – Manish

    0

    您可以使用帶有LOCALE_CHANGED Intent的BroadcastReceiver來檢測設置更改,然後更新您的視圖(地圖視圖或總佈局)。 https://developer.android.com/reference/android/content/Intent.html#ACTION_LOCALE_CHANGED

    public class LocaleReceiver extends BroadcastReceiver { 
        public LocaleReceiver() {} 
    
        @Override 
        public void onReceive(Context context, Intent intent) { 
    
        if(Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())){ 
         // Call a method to trigger view refreshing here 
         } 
        } 
    }