大家好我试图在我的应用上创建一个能改变语言的功能(在Android上),事实是它不能很好地工作,因此我必须重启我的应用程序才能使用该语言改变应用
我想要实现的是首先选择弹出的语言,然后通知用户应用程序将重新启动以便应用更改。
这是我用来更改语言的代码段,以及应用程序在重新启动时保存更改语言的代码:
public class LocalizationUpdaterActivity extends Activity {
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 :(得分:0)
当您在共享偏好中存储语言时,您只需在对话框中调用此代码段&#34;确定&#34;点击事件:
Intent i = new Intent(MyClass.this, MyClass.class);
startActivity(i);
答案 1 :(得分: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();
}
}
}
这两个函数可用于终止/重启应用程序。
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);
}
这种做法从来没有让我失望,我也没有发现任何问题。