Android to achieve double-tap the back key to exit, press the return key does not exit

Transfer: http://blog.sina.com.cn/s/blog_4fd2a65a0101gg2o.html

Original Address: http://my.eoe.cn/leigo/archive/2146.html   original author: gaolei_xj

 

Android application is doing we often have to judge a user operation on the return key, the general in order to prevent misuse are asked whether to exit the application when the user continuously pressed the return key twice.

A first basic principle is implemented, when the BACK button is pressed, the onKeyDown are captured, BACK key is determined, the exit method is performed.
In the exit process, will first determination value isExit is, if false, then set to true, will also prompted and sent a message after 2000 milliseconds (two seconds), in the Handler is reduced to false this value. If the transmitted message within 2 seconds of the interval, the BACK button is pressed again, the exit method is performed again, this time value isExit been true, a method of performing withdrawal.

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

}

Second implementation, the key is calculated by recording the time difference between the time to achieve:

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();
            System.exit(0);
        }
    }

}

The above program is running there will be a small bug, when the long-press the return key, not even long press, simply press a bit longer than clicks, your program will operate mistaken twice keydown, then call exit () to exit the program.

Solution is: OnBackPressed () function instead of the OnKeyDown () function. OnBackPressed () function is a separate function of the return key listener. code show as below

  // 第一次按下返回键的时间
    private long firstPressedTime;

   /**
     * 利用时间差方法,实现再按一次退出程序
     * System.currentTimeMillis() 是当前系统的时间
     */
    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - firstPressedTime < 2000) {
            super.onBackPressed();
        } else {
            Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            firstPressedTime = System.currentTimeMillis();
        }
    }

OnKeyDown () is a unified physical buttons on the monitor, and after the trigger still need to determine which physical button, return key or the menu key, etc., this method is compatible with Android 1.0 to Android 2.1 is the conventional method. The OnBackPressed () function returns alone to monitor key and exit the program, the code is simple and elegant, but one thing to note that this method only applies to version 2.0 or later of sdk.

 

Reference: https://blog.csdn.net/ww897532167/article/details/53893340

Guess you like

Origin blog.csdn.net/Smile_Qian/article/details/81609209