android 点击两次才退出app

访问量: 270

参考:  http://stackoverflow.com/questions/8430805/clicking-the-back-button-twice-to-exit-an-activity

把下面代码放到你的 activity中.   handler 记得是 android.os  这个package 的.

boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;                       
        }
    }, 2000);
} 


订阅/RSS Feed

Subscribe

分类/category