码迷,mamicode.com
首页 > 移动开发 > 详细

Android 按二次后退键退出应用程序

时间:2014-06-03 02:25:32      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:android   apk   csdn   frameworks   google   

前言
         欢迎大家我分享和推荐好用的代码段~~
声明
         欢迎转载,但请保留文章原始出处:
         CSDN
http://www.csdn.net
         雨季o莫忧离:http://blog.csdn.net/luckkof

正文


<span style="font-size:10px;">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;
        }
    };
     
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        System.out.println("TabHost_Index.java onKeyDown");
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            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;
    }
</span>
<span style="font-size:10px;">private long waitTime = 2000;  
	private long touchTime = 0; 
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN && KeyEvent.KEYCODE_BACK == keyCode) {
			long currentTime = System.currentTimeMillis();
			if ((currentTime - touchTime) >= waitTime) {
				Toast.makeText(context, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				touchTime = currentTime;
			} else {
				finish();
				System.exit(0);
			}
			return true;
		}
    	
		return super.onKeyDown(keyCode, event);
	}
</span>


Android 按二次后退键退出应用程序,布布扣,bubuko.com

Android 按二次后退键退出应用程序

标签:android   apk   csdn   frameworks   google   

原文地址:http://blog.csdn.net/luckkof/article/details/27548727

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!