gpt4 book ai didi

android - 什么时候使用 System.exit(0) 和 System.exit(2)?

转载 作者:行者123 更新时间:2023-12-05 00:11:17 26 4
gpt4 key购买 nike

我想在崩溃后重新启动应用程序。我正在使用下面的代码来执行该任务。

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

是的,它重新启动了应用程序,但在其他教程中我发现了用于重新启动应用程序的相同代码,但System.exit(2)代码在下面

  Intent mStartActivity = new Intent(HomeActivity.this, SplashScreen.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(HomeActivity.this, mPendingIntentId, mStartActivity,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) HomeActivity.this.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, mPendingIntent);
System.exit(2);

是的,在这两种情况下,应用程序都在重新启动,但我想知道 System.exit(0) 和 System.exit(2) 之间的区别。什么时候特别使用它们?

最佳答案

简短回答:从不

你永远不应该使用 System.exit(0)System.exit(1) 的原因,也不应该在 Android 中使用 exit 的任何一个值是因为它破坏了 Activity 的生命周期。 Android 自行处理,试图干扰它是一个非常糟糕的主意。

如果您真的想终止您的应用,请使用 Activity.finish() .

你应该看看Android Activity Lifecycle真正理解它是如何工作的。

关于android - 什么时候使用 System.exit(0) 和 System.exit(2)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50247636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com