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);
}
}