我找到了一些以编程方式退出Android应用程序的代码。通过在onDestroy()中使用以下任何代码,它会完全退出应用程序吗?
System.runFinalizersOnExit(真正的)
(或)
android.os.Process.killProcess (android.os.Process.myPid ());
我不想在点击退出按钮后在后台运行我的应用程序。
请告知我是否可以使用这些代码中的任何一个来退出我的应用程序?如果可以,我可以使用哪个代码?在Android中退出应用程序是一个好方法吗?
从API 16开始,你可以使用finishAffinity方法,它似乎非常接近于通过它的名称和Javadoc描述关闭所有相关的活动:
this.finishAffinity();
Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and into its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.
Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.
从API 21开始,您可以使用非常类似的命令
finishAndRemoveTask();
完成此任务中的所有活动,并将其从最近任务列表中移除。
我不知道这是否会引起不满,但我就是这么做的……
步骤1 -我通常有一个类,其中包含我想全局访问的方法和变量。在本例中,我将其称为“App”类。在类中为应用程序的每个活动创建一个静态Activity变量。然后创建一个名为“close”的静态方法,如果这些Activity变量不为空,它将在每个Activity变量上运行finish()方法。如果你有一个主/父活动,最后关闭它:
public class App
{
////////////////////////////////////////////////////////////////
// INSTANTIATED ACTIVITY VARIABLES
////////////////////////////////////////////////////////////////
public static Activity activity1;
public static Activity activity2;
public static Activity activity3;
////////////////////////////////////////////////////////////////
// CLOSE APP METHOD
////////////////////////////////////////////////////////////////
public static void close()
{
if (App.activity3 != null) {App.activity3.finish();}
if (App.activity2 != null) {App.activity2.finish();}
if (App.activity1 != null) {App.activity1.finish();}
}
}
步骤2 -在每个活动中,重写onStart()和onDestroy()方法。在onStart()中,将App类中的静态变量设置为"this"。在onDestroy()中,将其设置为null。例如,在“Activity1”类中:
@Override
public void onStart()
{
// RUN SUPER | REGISTER ACTIVITY AS INSTANTIATED IN APP CLASS
super.onStart();
App.activity1 = this;
}
@Override
public void onDestroy()
{
// RUN SUPER | REGISTER ACTIVITY AS NULL IN APP CLASS
super.onDestroy();
App.activity1 = null;
}
步骤3 -当你想关闭你的应用程序时,只需从任何地方调用app .close()。所有实例化的活动将关闭!因为你只是关闭活动,而不是杀死应用程序本身(如你的例子),Android可以自由地从那里接管,并做任何必要的清理。
再说一次,我不知道这是否会因为任何原因而引起不满。如果是这样的话,我很乐意阅读关于为什么会这样以及如何改进的评论!
从API 16开始,你可以使用finishAffinity方法,它似乎非常接近于通过它的名称和Javadoc描述关闭所有相关的活动:
this.finishAffinity();
Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and into its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.
Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.
从API 21开始,您可以使用非常类似的命令
finishAndRemoveTask();
完成此任务中的所有活动,并将其从最近任务列表中移除。