我找到了一些以编程方式退出Android应用程序的代码。通过在onDestroy()中使用以下任何代码,它会完全退出应用程序吗?

System.runFinalizersOnExit(真正的) (或) android.os.Process.killProcess (android.os.Process.myPid ());

我不想在点击退出按钮后在后台运行我的应用程序。 请告知我是否可以使用这些代码中的任何一个来退出我的应用程序?如果可以,我可以使用哪个代码?在Android中退出应用程序是一个好方法吗?


当前回答

只需要使用finish()返回键按onKeypressed()

其他回答

finishAffinity ();

System.exit(0);

如果你只使用finishAffinity();没有system . exit (0);你的应用程序将退出,但分配的内存仍然会被你的手机使用,所以…如果你想要一个干净的,真正退出的应用程序,使用他们两个。

这是最简单的方法,在任何地方都适用,退出应用程序,你可以有很多活动打开仍然会退出所有没有问题。

示例对一个按钮进行单击

public void exitAppCLICK (View view) {

    finishAffinity();
    System.exit(0);

}

或者如果你想要一些漂亮的东西,例如一个警报对话框,有3个按钮是,否和取消

// alertdialog for exit the app
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

// set the title of the Alert Dialog
alertDialogBuilder.setTitle("your title");

// set dialog message
alertDialogBuilder
        .setMessage("your message")
        .setCancelable(false)
        .setPositiveButton("YES"),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int id) {
                        // what to do if YES is tapped
                        finishAffinity();
                        System.exit(0);
                    }
                })

        .setNeutralButton("CANCEL"),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int id) {
                        // code to do on CANCEL tapped
                        dialog.cancel();
                    }
                })

        .setNegativeButton("NO"),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int id) {
                        // code to do on NO tapped
                        dialog.cancel();
                    }
                });

AlertDialog alertDialog = alertDialogBuilder.create();

alertDialog.show();

简单和简单的方法退出应用程序

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);

这取决于你想多快关闭你的应用程序。

关闭应用程序的安全方法是finishAffinity();

它关闭你的应用程序后,所有进程完成处理。 这可能需要一些时间。 如果你以这种方式关闭你的应用程序,并在短时间后重新启动它,你的新应用程序可能运行在同一个进程中。旧应用程序的所有未完成的进程和单例对象。

如果你想确保你的应用程序已经完全关闭,请使用System.exit(0);

这将立即关闭你的应用程序。 但这是有可能的,你破坏了你的应用程序已经打开的文件或共享首选项的编辑没有完成。所以要小心使用。

如果您将看门狗与长时间运行的任务结合使用,您可以看到不同方法的影响。

new ANRWatchDog(2000).setANRListener(new ANRWatchDog.ANRListener() {
    public void onAppNotResponding(ANRError error) {
        MainActivity.this.finishAffinity();
        System.exit(0);
    }
}).start();
for(int i = 0; i < 10; ++i){
    --i;
}

这会在2秒后杀死你的应用,而不显示ANR对话框或类似的东西。如果你删除System.exit(0),运行这段代码并在应用程序关闭后重新启动应用程序,你会遇到一些奇怪的行为,因为无休止的循环仍在运行。

public void quit() {
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);
        System.exit(0);
    }

朋友只需添加此函数以编程方式退出应用程序 # java

public void onBackPressed() 
{

    finishAffinity();
    System.exit(0);
}