如何从退出按钮退出android应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11643224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:20:15  来源:igfitidea点击:

how to exit android application from exit button?

android

提问by SopheakVirak

Possible Duplicate:
android - exit application code

可能重复:
android - 退出应用程序代码

I am finding the way to exit android application to home screen(android phone).

我正在寻找将 android 应用程序退出到主屏幕(android 手机)的方法。

My code is not completely exit application but it turn to the root activity of application.

我的代码不是完全退出应用程序,而是转向应用程序的根活动。

Here is my code

这是我的代码

//Expression
    Button exit = (Button) findViewById(R.id.exit);
    exit.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            finish();
            System.exit(0);
        }
    });

Can you tell me how to exit application and go to home screen of android phone?

你能告诉我如何退出应用程序并进入安卓手机的主屏幕吗?

Regards,

问候,

回答by Ram kiran

public void AppExit()
{

    this.finish();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

    /*int pid = android.os.Process.myPid();=====> use this if you want to kill your activity. But its not a good one to do.
    android.os.Process.killProcess(pid);*/

}

call this method where you click exitbutton

在单击exit按钮的地方调用此方法