Android雙擊返回鍵退出Activity的方法

來源:互聯網
上載者:User

標籤:

第一種:利用線程延時實現:

 

      private int mBackKeyPressedTimes = 0;

 

        @Override        public void onBackPressed() {                if (mBackKeyPressedTimes == 0) {                        Toast.makeText(this, "再按一次退出程式 ", Toast.LENGTH_SHORT).show();                        mBackKeyPressedTimes = 1;                        new Thread() {                                @Override                                public void run() {                                        try {                                                Thread.sleep(2000);                                        } catch (InterruptedException e) {                                                e.printStackTrace();                                        } finally {                                                mBackKeyPressedTimes = 0;                                        }                                }                        }.start();                        return;                      else{                               this.activity.finish();                            }                }                super.onBackPressed();        }



第二種:利用計算時間差實現 (個人覺得這種方式較為簡單,而且不容易發生異常,代碼較為安全)

        private long exitTime = 0;

        public void ExitApp()
        {
                if ((System.currentTimeMillis() - exitTime) > 2000)
                {
                        Toast.makeText(this.activity, "再按一次退出程式", Toast.LENGTH_SHORT).show();
                        exitTime = System.currentTimeMillis();
                } else
                {
                        this.activity.finish();
                }

        }

 

第三種方法

 

  1. /** 
  2.  * 菜單、返回鍵響應 
  3.  */  
  4. @Override  
  5. public boolean onKeyDown(int keyCode, KeyEvent event) {  
  6.     // TODO Auto-generated method stub  
  7.     if(keyCode == KeyEvent.KEYCODE_BACK)  
  8.        {    
  9.            exitBy2Click();      //調用雙擊退出函數  
  10.        }  
  11.     return false;  
  12. }  
  13. /** 
  14.  * 雙擊退出函數 
  15.  */  
  16. private static Boolean isExit = false;  
  17.   
  18. private void exitBy2Click() {  
  19.     Timer tExit = null;  
  20.     if (isExit == false) {  
  21.         isExit = true; // 準備退出  
  22.         Toast.makeText(this, "再按一次退出程式", Toast.LENGTH_SHORT).show();  
  23.         tExit = new Timer();  
  24.         tExit.schedule(new TimerTask() {  
  25.             @Override  
  26.             public void run() {  
  27.                 isExit = false; // 取消退出  
  28.             }  
  29.         }, 2000); // 如果2秒鐘內沒有按下返回鍵,則啟動定時器取消掉剛才執行的任務  
  30.   
  31.     } else {  
  32.         finish();  
  33.         System.exit(0);  
  34.     }  
  35. }  

 

Android雙擊返回鍵退出Activity的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.