android程序 点击两次返回键程序退出

转自:http://www.cnblogs.com/sunjialiang/archive/2011/08/31/2161047.html

===========================================================================================

private static Boolean isExit = false;
private static Boolean hasTask = false;
Timer tExit = new Timer();
TimerTask task = new TimerTask() {

@Override
public void run() {
isExit = false;
hasTask = true;
}
};


public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK){
// System.out.println("user back down");
if(isExit == false ) {
isExit = true;
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
if(!hasTask) {
tExit.schedule(task, 2000);
}} else {
}
finish();
System.exit(0);
}
}
return false;
}

posted @ 2013-02-22 22:38  __木头鱼__  阅读(143)  评论(0编辑  收藏  举报