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

在做安卓应用是我们经常要判断用户对返回键的操作,一般为了防止误操作都是在用户连续按下两次返回键的时候提示用户是否退出应用程序。

第一种实现的基本原理就是,当按下BACK键时,会被onKeyDown捕获,判断是BACK键,则执行exit方法。
在exit方法中,会首先判断isExit的值,如果为false的话,则置为true,同时会弹出提示,并在2000毫秒(2秒)后发出一个消息,在Handler中将此值还原成false。如果在发送消息间隔的2秒内,再次按了BACK键,则再次执行exit方法,此时isExit的值已为true,则会执行退出的方法。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.gaolei.exitdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.Toast;

public class MainActivity extends Activity {

    // 定义一个变量,来标识是否退出
    private static boolean isExit = false;

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);//经测试有没有finish,都一样;

        }
    }

}

第二种实现方式,通过记录按键时间计算时间差实现:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.gaolei.exitdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class MainActivity extends Activity {

    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();//经测试有没有finish,都一样;
            System.exit(0);
        }
    }

}

 

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; 
        }

 

 

 

 

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

    

 

[java]   view plain copy
  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.    }  


 



     思路2:开线程延时处理

 

[java]   view plain copy
  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.         }  

 

 

你可能感兴趣的:(实现点击两次返回键退出)