以编程方式重新启动 android 应用程序

Gan*_*esh 4 android relaunch android-intent intentservice

我正在开发适用于 6 个国家/地区的 I18N 应用程序,有一个要求,例如,当用户从设置中更改语言时,应用程序应自行重启并以更改后的语言显示应用程序。为此,我将语言值存储在共享首选项中并与当前语言值进行比较。如果值不同,我需要重新启动应用程序。为此,我使用以下代码:

if (currentLanguage == null) {
    Util.saveStringInSP(_activity, "LANGUAGE", languageValue);
}
else if (currentLanguage.equalsIgnoreCase(languageValue)) {
    String currentLanguage = Util.getStringFromSP(_activity, "LANGUAGE");
}
else {
    try {
        android.os.Process.killProcess(android.os.Process.myPid());
    }
    catch(Exception e) {   }
    Intent i = new Intent();
    PackageManager manager = getPackageManager();
    i = manager.getLaunchIntentForPackage("com.pfizer.stablemate");
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)

请向我提供一段代码,以便以编程方式重新启动 Android 应用程序。

Koo*_*oki 8

尝试 :

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(        
getBaseContext().getPackageName() );
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)