首页 > 程序开发 > 移动开发 > Android >

修正android 处理连续按键两次

2012-01-09

1.变量初始化:private boolean isExit = false;private boolean hasTask = false;private boolean isExit = false;private boolean hasTask = false;2.按键捕捉(比如放在onTouchDown函数中进行...

1.变量初始化:


private boolean isExit = false;
private boolean hasTask = false;
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);
}
}
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;
}
}
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;
}
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;
}
}

摘自 xiaoxiaobian3310903的专栏

相关文章
最新文章
热点推荐