在开发android应用时,常常通过按返回键(即keyCode == KeyEvent.KEYCODE_BACK)就能关闭程序,其实大多情况下并没有关闭改应用 
我们可以这样做,当用户点击自定义的退出按钮或返回键时(需要捕获动作),我们在onDestroy()里强制退出应用,或直接杀死进程,具体操作代码如下: 
public boolean onKeyDown(int keyCode, KeyEvent event) {  
  
        // 按下键盘上返回按钮  
        if (keyCode == KeyEvent.KEYCODE_BACK) {  
  
            new AlertDialog.Builder(this)  
                    .setMessage("确定退出系统吗?")  
                    .setNegativeButton("取消",  
                            new DialogInterface.OnClickListener() {  
                                public void onClick(DialogInterface dialog,  
                                        int which) {  
                                }  
                            })  
                    .setPositiveButton("确定",  
                            new DialogInterface.OnClickListener() {  
                                public void onClick(DialogInterface dialog,  
                                        int whichButton) {  
                                    finish();  
                                }  
                            }).show();  
  
            return true;  
        } else {  
            return super.onKeyDown(keyCode, event);  
        }  
    }  
  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        // 或者下面这种方式  
        //System.exit(0);  
        //建议用这种  
        android.os.Process.killProcess(android.os.Process.myPid());  
    }










