Android简洁代码实现双击退出程序

时间:2022-07-17 20:36:15

方法一:重写onBackPressed()方法

 @Override
public void onBackPressed() {
if (mBackPressed+TIME_INTERVAL>System.currentTimeMillis()){
super.onBackPressed();
return;
}else {
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
}
mBackPressed = System.currentTimeMillis();
}

当然也可以写个其他的定时器来实现,无需判断onKeyDown()和onKeyUp()

So Esay!!!