android应用程序以编程方式关闭
我正在从事一个Android项目,要求在任何活动中出现异常。然后,应用程序应该在执行catch块后自动退出。我知道Android应用程序架构不建议应用程序自动关闭。使用此代码编写应用程序android应用程序以编程方式关闭,android,exit,Android,Exit,我正在从事一个Android项目,要求在任何活动中出现异常。然后,应用程序应该在执行catch块后自动退出。我知道Android应用程序架构不建议应用程序自动关闭。使用此代码编写应用程序 Intent homeIntent = new Intent(Intent.ACTION_MAIN); homeIntent.addCategory( Intent.CATEGORY_HOME ); homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); sta
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
并查看以下链接以了解更多详细信息
希望它有帮助,谢谢:( < p>)以编程方式(优雅地)终止Android应用程序,考虑实现完成链机制。具体来说, (1) 实现所有活动子类的onResume(),如下所示。 (2) 当您想终止应用程序时,请调用下面的方法。 因为Android可能会重用根活动实例而不是“新建”它,所以根活动的onResume()必须清除“停止”标志
protected final void onResume()
{
super.onResume();
if (App.getInstance().isStopping())
{
// Close this Activity, meaning that this application is closed
// because this Activity is the root.
finish();
// Reset the termination state because Android may reuse this
// Activity instance instead of creating a new one.
App.getInstance().setStopping(false);
}
else
{
......
}
}
实现“退出”(甚至“重启”)机制的示例应用程序:样本基本根活动:
如果您有多个活动,可以使用finishAffinity关闭所有活动
尝试在catch块中的每个活动中调用
finish()
,或者在您想要关闭应用程序的任何位置…您需要完全关闭应用程序,这意味着后台本身,或者在发生异常时希望处于活动状态。
protected void onResume()
{
super.onResume();
// Check if the application is in the process of
// stopping. How to implement 'App' in the code
// below is up to you.
if (App.getInstance().isStopping())
{
finish();
return;
}
......
protected void exit()
{
// Turn into 'stopping' state.
App.getInstance().setStopping(true);
// Finish this Activity. This removes this Activity
// instance from the Activity stack and the next
// Activity instance will be displayed. And if the
// next Activity's onResume() is implemented like the
// above example, the next Activity will finish(), too.
// This finish-chain mechanism continues until it
// reaches the root Activity.
finish();
}
protected final void onResume()
{
super.onResume();
if (App.getInstance().isStopping())
{
// Close this Activity, meaning that this application is closed
// because this Activity is the root.
finish();
// Reset the termination state because Android may reuse this
// Activity instance instead of creating a new one.
App.getInstance().setStopping(false);
}
else
{
......
}
}
finishAffinity();