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

以编程方式重新启动android应用程序,android,android-intent,intentservice,relaunch,Android,Android Intent,Intentservice,Relaunch,我正在开发适用于6个国家/地区的I18N应用程序,其中有一个要求,例如,当用户从设置中更改语言时,应用程序应重新启动并以更改的语言显示应用程序。为此,我将语言值存储在共享首选项中,并与当前语言值进行比较。如果值不相同,我需要重新启动应用程序。为此,我使用以下代码: if (currentLanguage == null) { Util.saveStringInSP(_activity, "LANGUAGE", languageValue); } else if (currentLangu

我正在开发适用于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);
}
请提供一段代码,以编程方式重新启动Android应用程序。

尝试:

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(        
getBaseContext().getPackageName() );
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
试试这个:

   Intent mStartActivity = new Intent(context, Your_first_activity.class);
   int mPendingIntentId = 123456; 
   PendingIntent mPendingIntent = PendingIntent.getActivity(context, 
   mPendingIntentId,mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
   AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
   mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
   System.exit(0);

您不需要为此而停止/重新启动。您只需要在清单的configChanges字段中不包含区域设置,android将为您重新启动活动。我不想重新启动活动,我需要从一开始就重新启动整个应用程序。然后,您可以在configChanges字段中包含区域设置,并在活动的onConfigChange中接收区域设置更改