天天看点

在开发过程中使用Android返回键

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());  
    }