android 返回键完全退出程序

主页面中覆盖onKeyDown方法
   
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode == KeyEvent.KEYCODE_BACK){
            ShowDialog.showExitDialog(MainActivity.this);
        }
        return true;
    }






ShowDialog.java

public class ShowDialog{
   
   
    public static void showExitDialog(Activity context){
        //实例化一个关于退出的警示对话框
        final AlertDialog exitDialog = new AlertDialog.Builder(context).create();
        exitDialog.setTitle("提示");
        exitDialog.setMessage("是否退出程序");
       
        //点击“是”的时候触发的事件
        exitDialog.setButton("是", new AlertDialog.OnClickListener(){

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                exitDialog.dismiss();
                System.out.println("the wopai program exit");
                System.exit(0);
               
            }
           
        });
       
        //点击“否”的时候触发的事件
        exitDialog.setButton2("否", new AlertDialog.OnClickListener(){

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                exitDialog.dismiss();
            }
           
        });
        //显示对话框
        exitDialog.show();
    }
   
}

你可能感兴趣的:(android 返回键完全退出程序)