Android返回键的监听汇总
一,返回键的简单监听
@Override
public void onBackPressed() {
Toast.makeText(this, "你点击了返回键", 0).show();
}
二,双击退出
// 返回键的监听
private Boolean isExit = false;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exitByDoubleClick();
}
return false;
}
private void exitByDoubleClick() {
Timer tExit = null;
if (isExit == false) {
isExit = true;
Toast.makeText(this, "再按一下,退出", Toast.LENGTH_SHORT).show();
tExit = new Timer();
tExit.schedule(new TimerTask() {
@Override
public void run() {
isExit = false;
}
}, 2000);
} else {
android.os.Process.killProcess(android.os.Process.myPid());
}
}