当前位置:操作系统 > 安卓/Android >>

android中Activity实现再按一次退出

[java]
Exit exit = new Exit(); 
     
  @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
      if (keyCode == KeyEvent.KEYCODE_BACK) { 
              pressAgainExit(); 
              return true; 
              } 
 
        return super.onKeyDown(keyCode, event); 
    } 
   
  private void pressAgainExit() { 
          if (exit.isExit()) { 
              finish(); 
          } else { 
              Toast.makeText(getApplicationContext(), "再按一次退出程序", 
                      1000).show(); 
              exit.doExitInOneSecond(); 
          } 
      } 


辅助类Exit.java

[java]
package com.chen.b; 
 
import android.os.Handler; 
import android.os.HandlerThread; 
 
public class Exit { 
 
    private boolean isExit = false; 
        private Runnable task = new Runnable() { 
            @Override 
            public void run() { 
                isExit = false; 
            } 
        }; 
      
        public void doExitInOneSecond() { 
            isExit = true; 
            HandlerThread thread = new HandlerThread("doTask"); 
            thread.start(); 
            new Handler(thread.getLooper()).postDelayed(task, 1000); 
        } 
          
        public boolean isExit() { 
            return isExit; 
        }  www.zzzyk.com
      
        public void setExit(boolean isExit) { 
            this.isExit = isExit; 
        } 
    } 

 作者:chenshijun0101

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,