2015-06-03 55 views
0

嗨每個人我試圖在我的應用程序上創建一個功能,將改變語言(Android上)的事情是,它不是真的很好,因此我必須重新啓動我的應用程序爲了適用語言更改彈出窗口,將重新啓動應用程序Android

我想要實現的是首先選擇語言彈出,然後通知用戶應用程序將重新啓動,以便應用更改。

這裏是一段代碼,我使用更改語言和應用以保存更改語言,當它重新啓動:

公共類LocalizationUpdaterActivity延伸活動{

private String[] languages = { "English", "Francais", "Espanol", "Ivrit" }; 
/** 
* Called when the activity is first created. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_langues); 

    SharedPreferences sp = this.getApplicationContext().getSharedPreferences("loginSaved", Context.MODE_PRIVATE); 
    final SharedPreferences.Editor editor = sp.edit(); 


    Spinner spinner = (Spinner) findViewById(R.id.spinner1); 
    spinner.setPrompt("select language"); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, languages); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter); 

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

     public void onItemSelected(AdapterView arg0, View arg1, 
            int arg2, long arg3) { 
      Configuration config = new Configuration(); 
      switch (arg2) { 
       case 0: 
        config.locale = Locale.ENGLISH; 
        editor.putString("Langues", "en_US"); 
        break; 
       case 1: 
        config.locale = Locale.FRENCH; 
        editor.putString("Langues", "fr_FR"); 
        break; 
       case 2: 
        config.locale = new Locale("es_ES"); 
        editor.putString("Langues", "es_ES"); 
        break; 
       case 3: 
        config.locale = new Locale("he", "IL"); 
        editor.putString("Langues", "he_IL"); 
        break; 
       default: 
        config.locale = Locale.ENGLISH; 
        editor.putString("Langues", "en_US"); 
        break; 
      } 
      getResources().updateConfiguration(config, null); 
     } 

     public void onNothingSelected(AdapterView arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 
} 

}

回答

0

我遇到了與您目前相同的問題。
爲了達到這個目的,我在我的BaseActivity中實現了一個BroadcastReceiver(所有活動都從中延伸),當接收到特定命令時,它將完成活動。
這將完成發送命令時的所有活動,從而終止應用程序。

要立即重新啓動它,只需簡單地爲想要的活動創建一個意圖,然後在殺死該應用程序的同一功能中啓動該活動。

一個簡單的例子如下所示:

public abstract class BaseActivity extends FragmentActivity { 

    private KillReceiver killReceiver; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     killReceiver = new KillReceiver(); 
     registerReceiver(killReceiver, IntentFilter.create("kill", "content://all")); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     unregisterReceiver(killReceiver); 
    } 

    private final class KillReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      finish(); 
     } 
    } 
} 

而且這些2個函數可用於殺滅/重新啓動該應用程序。

public void killApplication(Activity activity) { 
    //Broadcast the command to kill all activities 
    Intent intent = new Intent("kill"); 
    intent.setType("content://all"); 
    activity.sendBroadcast(intent); 
} 

public void restartApplication(Activity activity) { 
    killApplication(activity); 

    //Start the launch activity 
    Intent i = activity.getBaseContext().getPackageManager().getLaunchIntentForPackage(activity.getBaseContext().getPackageName()); 
    activity.startActivity(i); 
} 

這樣做的方式從來沒有讓我失望,我還沒有發現任何問題。

0

由於您在sharedpreference中存儲語言,因此您可以簡單地在對話框的「確定」單擊事件中調用此片段:

Intent i = new Intent(MyClass.this, MyClass.class); 
startActivity(i); 
相關問題