[share use full code]Exit a app when back button twice touch.

I work with my companion Who has expert skill android.

He send a code for android back-button.(back arrow button)
I want to use a code but my boss said “I am not want to use this”.

but that code is so cool.
just comment is korean. so I add a english comments. hm.

In /src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java

use a code! surely “public class Cocos2dxGLSurfaceView extends GLSurfaceView”

public class Cocos2dxGLSurfaceView extends GLSurfaceView {

/*
* This function is called before Cocos2dxRenderer.nativeInit(), so the width and height is correct.
*/
protected void onSizeChanged(int w, int h, int oldw, int oldh){
this.mRenderer.setScreenWidthAndHeight(w, h);
}

// back 버튼을 위한 변수
// back button for valuable
private static final int MSG_TIMER_EXPIRED = 1;
private static final int BACKKEY_TIMEOUT = 2000; // 이것을 바꿔주면 연속 터치 시간 조절 가능
private boolean mIsBackKeyPressed = false;
private long mCurrentTimeInMillis = 0;

`Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
final int kc = keyCode;
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(mIsBackKeyPressed == false){
mIsBackKeyPressed = true;

            mCurrentTimeInMillis = Calendar.getInstance().getTimeInMillis();

            //Toast.makeText(getContext(), "\'뒤로\' 버튼을 한번 더 누르시면 종료됩니다.", Toast.LENGTH_SHORT).show();
                   //Toast.makeText(getContext(), "if you touch back button once more, It is exit or end.", Toast.LENGTH_SHORT).show();
            startTimer();
        }else{
            mIsBackKeyPressed = false;

            if(Calendar.getInstance().getTimeInMillis() <= (mCurrentTimeInMillis + (BACKKEY_TIMEOUT))){
                System.exit(0);
            }
        }
        return true;
    }

// if (keyCode == KeyEvent.KEYCODE_MENU) {
// queueEvent(new Runnable() {
// `Override
// public void run() {
// mRenderer.handleKeyDown(kc);
// }
// });
// return true;
// }
return super.onKeyDown(keyCode, event);
}

// 뒤로 버튼을 위한 핸들러
// handler for back button
private Handler mTimerHandler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case MSG_TIMER_EXPIRED:
{
mIsBackKeyPressed = false;
}break;
}
}
};

// 뒤로 버튼을 위한 타이머
// timer for back button
private void startTimer()
{
mTimerHandler.sendEmptyMessageDelayed(MSG_TIMER_EXPIRED, BACKKEY_TIMEOUT);
}
}

well there’s an easier way, cclayer already has a keyback method so you can override it
i.e.

void HelloWorld::keyBackClicked()
{
    CCDirector::sharedDirector()->end();
}

if you want double click you can make a time method that updates time and make something like

void HelloWorld::keyBackClicked()
{
   if(firstClick)
   {
      clickTime = this->getTime();
      firstClick = false;
   }
   else
   {
       if((this->getTime() - clickTime) < CLICK_DELAY)
       {
          CCDirector::sharedDirector()->end();
       }
       else
          clickTime = this->getTime();
   }
}
void HelloWorld::setTime()
{
   time++;
}
int HelloWord::getTime()
{
   return time;
}
//in helloworld init 
time = 0;
this->schedule( schedule_selector(HelloWorld::setTime),1.0);