android KEYCODE_BACK监听处理

@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK ){
			showDailog("亲!真的要退出吗?");
	}
//		return super.onKeyDown(keyCode, event);
		return false;//当有onclickListener时候,上面的写法showDailog会直接退出程序,不知到为什么
	}
	private void showDailog(String msg) {
		AlertDialog.Builder builder = new Builder(this);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle("确认退出");
		builder.setMessage(msg);
//		builder.setCancelable(false);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
//				Intent startMain = new Intent(Intent.ACTION_MAIN);
//				startMain.addCategory(Intent.CATEGORY_HOME);
//				startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				startActivity(startMain);//跳转到home下
				finish();
			}
		});
		builder.setNegativeButton("取消", null);
		builder.create().show();
	}


 
 

你可能感兴趣的:(android)