Android连续点击两次Back键退出程序

Android连续点击两次Back键退出程序       
        分类:            Android 288人阅读 评论(0) 收藏 举报

     思路1:记录上次点击的时间,与本次点击的时间比较,当两次时间间隔小于一定值时,退出,否则提示“再按一次退出程序”,同时更新上次点击时间

   

  1. private long firstTime = 0
  2.    @Override 
  3. public boolean onKeyUp(int keyCode, KeyEvent event) { 
  4.        // TODO Auto-generated method stub 
  5.        switch(keyCode) 
  6.        { 
  7.        case KeyEvent.KEYCODE_BACK: 
  8.             long secondTime = System.currentTimeMillis();  
  9.              if (secondTime - firstTime > 2000) {                                         //如果两次按键时间间隔大于2秒,则不退出 
  10.                  Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();  
  11.                  firstTime = secondTime;//更新firstTime 
  12.                  return true;  
  13.              } else {                                                    //两次按键小于2秒时,退出应用 
  14.             System.exit(0); 
  15.              }  
  16.            break
  17.        } 
  18.      return super.onKeyUp(keyCode, event); 
  19.    } 
 private long firstTime = 0;
    @Override
 public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
             long secondTime = System.currentTimeMillis(); 
              if (secondTime - firstTime > 2000) {                                         //如果两次按键时间间隔大于2秒,则不退出
                  Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show(); 
                  firstTime = secondTime;//更新firstTime
                  return true; 
              } else {                                                    //两次按键小于2秒时,退出应用
             System.exit(0);
              } 
            break;
        }
      return super.onKeyUp(keyCode, event);
    }



     思路2:开线程延时处理

  1. private int mBackKeyPressedTimes = 0
  2.  
  3.         @Override 
  4.         public void onBackPressed() { 
  5.                 if (mBackKeyPressedTimes == 0) { 
  6.                         Toast.makeText(this, "再按一次退出程序 ", Toast.LENGTH_SHORT).show(); 
  7.                         mBackKeyPressedTimes = 1
  8.                         new Thread() { 
  9.                                 @Override 
  10.                                 public void run() { 
  11.                                         try
  12.                                                 Thread.sleep(2000); 
  13.                                         } catch (InterruptedException e) { 
  14.                                                 e.printStackTrace(); 
  15.                                         } finally
  16.                                                 mBackKeyPressedTimes = 0
  17.                                         } 
  18.                                 } 
  19.                         }.start(); 
  20.                         return
  21.                       else
  22.                                this.activity.finish(); 
  23.                             } 
  24.                 } 
  25.                 super.onBackPressed(); 
  26.         } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值