天天看點

在開發過程中使用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());  
    }