【Android】【转】实现android中Activity实现再按一次退出
Exit exit = new Exit(); @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { pressAgainExit(); return true; } return super.onKeyDown(keyCode, event); } private void pressAgainExit() { if (exit.isExit()) { finish(); } else { Toast.makeText(getApplicationContext(), "再按一次退出程序", ).show(); exit.doExitInOneSecond(); } }
辅助类Exit.java
package com.chen.b; import android.os.Handler; import android.os.HandlerThread; public class Exit { private boolean isExit = false; private Runnable task = new Runnable() { @Override public void run() { isExit = false; } }; public void doExitInOneSecond() { isExit = true; HandlerThread thread = new HandlerThread("doTask"); thread.start(); new Handler(thread.getLooper()).postDelayed(task, 1000); } public boolean isExit() { return isExit; } public void setExit(boolean isExit) { this.isExit = isExit; } }
源自:http://www.cnblogs.com/android100/archive/2012/06/22/Activity-exit.html
作者:风倾清凌
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.