修正android 处理连续按键两次

1.变量初始化:

private  boolean isExit = false;  
private  boolean hasTask = false;
2.按键捕捉(比如放在onTouchDown函数中进行按键监听

PressHomeKeyCount localHandler = new PressHomeKeyCount();
		
	if(isExit == false ) {  
		isExit = true;
		if(!hasTask) {
			Message localMessage = new Message();
			localMessage.what = 0;
			localHandler.sendEmptyMessageDelayed(localMessage.what, 500L);
		}
		else{
			if(mRecentApplication.getVisibility() == View.VISIBLE){
				mRecentApplication.setVisibility(View.INVISIBLE);
				isExit = false;
				hasTask = false;
			}
		}
	}
	else{
		if(mRecentApplication.getVisibility() == View.INVISIBLE){
			mRecentApplication.setVisibility(View.VISIBLE);
		}
	}


3.Handle对象

public class PressHomeKeyCount extends Handler {  
        public void handleMessage(final Message msg) {  
        	isExit = false;    
            	hasTask = true;   
        }  
} 

上边的做法会出现死循环,我又对它进行了改进,以下是改进后的代码,经过测试没什么问题,其中变量初始化一样

2.按键捕捉

if(isExit == false ) {    
        isExit = true;  
        if(!hasTask) {  
            Message localMessage = new Message();  
            localMessage.what = 0;  
            localHandler.sendEmptyMessageDelayed(localMessage.what, 500L);  
        }  
        else{  
        	isExit = false;  
            hasTask = false;  
            localHandler.removeMessages(0);

            if(mRecentApplication.getVisibility() == View.VISIBLE){
            	mRecentApplication.setVisibility(View.INVISIBLE);
            }
        } 
    } 
    else{ 
	if(mRecentApplication.getVisibility() == View.INVISIBLE){
		mRecentApplication.setVisibility(View.VISIBLE);
	}
	localHandler.removeMessages(0); 
	isExit = false; 
	hasTask = true; 
    }


 3.Handler对象 
 

public class PressHomeKeyCount extends Handler {  
        public void handleMessage(final Message msg) {  
        	isExit = false;    
            	hasTask = false;   
        }  
} 






你可能感兴趣的:(android,测试,Class)