zoukankan      html  css  js  c++  java
  • Android双击返回键退出Activity的方法

    第一种:利用线程延时实现:
          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. }  
  • 相关阅读:
    什么是继承?
    程序员兄弟们,我们的基本素质怎么样?
    C#基础概念二十五问
    windows mobile 5.0 PocketPC模拟器上网的设置
    数据库设计中的14个技巧
    Microsoft SQL Server 2005 存储过程翻页
    在.Net如何制作自定义的快捷方式(转)
    ActiveSync 没有DMA端口问题的解决方法
    原型模式(Prototype Pattern)
    获取鼠标和键盘长时间不动的时间
  • 原文地址:https://www.cnblogs.com/cmblogs/p/4390891.html
Copyright © 2011-2022 走看看