android 返回键 退出

	//捕获按下键盘上返回按钮
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(this)
                    // .setIcon(R.drawable.services)
                    .setTitle("EXIT")
                    .setMessage("Do you wanna quit?")
                    .setNegativeButton("cancel",
                            new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									
								}
//                                @Override
//                                public void onClick(DialogInterface dialog,
//                                        int which) {
//                                }
                            })
                    .setPositiveButton("confirm",
                            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());
    }


你可能感兴趣的:(android,dialog)