双击两次返回键退出应用方法一:
//退出应用:
private long firstTime = 0;
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
long secondTime = System.currentTimeMillis();
if (secondTime - firstTime > 2000) { //如果两次按键时间间隔大于2秒,则不退出
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
firstTime = secondTime;//更新firstTime
return true;
} else { //两次按键小于2秒时,退出应用
System.exit(0);
}
break;
}
return super.onKeyUp(keyCode, event);
}
双击两次返回键退出应用方法二:
![171000_cjCz_2407682.png](https://i-blog.csdnimg.cn/blog_migrate/a3e2b36db2b0a1a59a87b72a904db57f.png)