一,两种方式捕捉
1,重写 onBackPressed方法
@Override
public void onBackPressed() {
super.onBackPressed();
}
2,重写onKeyDown方法
@Override
public boolean onKeyDown(intkeyCode,KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
}
}
二,再按一次退出程序
private longexitTime=0;
@Override
public booleanonKeyDown(intkeyCode,KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
if((System.currentTimeMillis()-exitTime)>2000){
Toast.makeText(this,"你真的要退出程序吗?,Toast.LENGTH_SHORT).show();
exitTime=System.currentTimeMillis();
}else{
finish();
}
return true;
}
return super.onKeyDown(keyCode,event);
}