简体   繁体   English

弹出窗口,将重新启动App Android

[英]Pop-up that will restart the App Android

Hi Everyone i'm trying to create a function on my app that will change the language ( on Android ) the thing is that it does not really work well, thus i have to restart my App in order for the language change to be applied 大家好,我正尝试在我的应用程序上创建一个功能(该功能会更改语言)(在Android上),事实是它确实无法正常工作,因此我必须重新启动我的应用程序才能应用语言更改

What i am trying to achieve is to first select the language a pop up and then to inform the user that the app will be restarted in order for the change to be applied . 我要实现的目的是首先选择一种弹出窗口,然后通知用户该应用程序将重新启动以便进行更改。

here is the piece of code i'm using to change the language and for the app to save the change language when it restart: 这是我用来更改语言并在重新启动时保存更改语言的应用程序的代码:

public class LocalizationUpdaterActivity extends Activity { 公共类LocalizationUpdaterActivity扩展了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

        }
    });
}

} }

As you are storing language in sharedpreference you can simply call this snippet on your dialog's "ok" click event: 当您将语言存储在sharedpreference中时,您可以在对话框的“ ok”单击事件中简单地调用以下代码段:

Intent i = new Intent(MyClass.this, MyClass.class);
startActivity(i);

I had the same issue as you're currently faced with. 我遇到了与您当前面临的相同问题。
To accomplish this, i implemented a BroadcastReceiver in my BaseActivity (which all activities extend from), which when receiving a specific command, would finish the activity. 为此,我在BaseActivity中实现了BroadcastReceiver(所有活动都从中扩展),当接收到特定命令时,该广播接收器将完成该活动。
This would finish all activities when the command is sent, thus killing the application. 发送命令后,这将完成所有活动,从而终止应用程序。

To restart it immediately, you simply create an intent for the wanted activity and start the activity in the same function as you're killing the application. 要立即重新启动它,只需为所需的活动创建一个意图,然后以与终止应用程序相同的功能启动该活动。

A simple example is shown below: 下面是一个简单的示例:

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

And these 2 functions can be used to kill/restart the application. 这两个功能可用于终止/重新启动应用程序。

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

This way of doing it has never failed me, and i haven't found any issues with it. 这种方式从未使我失败,而且我也没有发现任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM