Skip to content

Instantly share code, notes, and snippets.

@RichardNi
Last active August 29, 2015 14:14
Show Gist options
  • Save RichardNi/65e79acd326db1afc984 to your computer and use it in GitHub Desktop.
Save RichardNi/65e79acd326db1afc984 to your computer and use it in GitHub Desktop.
Android模拟双击按钮退出应用,转自:http://www.cnblogs.com/net168/p/4234869.html
private long mPressedTime = 0;
@Override
public void onBackPressed() {
//获取第一次按键时间
  long mNowTime = System.currentTimeMillis();
  
  //比较两次按键时间差
  if((mNowTime - mPressedTime) > 2000){
    Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
    mPressedTime = mNowTime;
  } else {//退出程序
    this.finish();
    System.exit(0);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment