返回键关闭程序

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

// 按下键盘上返回按钮

if (keyCode == KeyEvent.KEYCODE_BACK) {

new AlertDialog.Builder(this)
.setTitle("关闭程序")

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

}

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