对接安卓渠道被反馈 安卓手机物理返回键点击不起作用
处理如下
@Override
public boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent) {
switch (pKeyCode) {
case KeyEvent.KEYCODE_BACK:
Cocos2dxVideoHelper.mVideoHandler.sendEmptyMessage(Cocos2dxVideoHelper.KeyEventBack);
return false; // 添加代码,去除拦截,防止android物理返回键失效
case KeyEvent.KEYCODE_MENU:
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
case KeyEvent.KEYCODE_DPAD_CENTER:
this.queueEvent(new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);
}
});
return true;
default:
return super.onKeyDown(pKeyCode, pKeyEvent);
}
}
添加代码 return false; 就可以解除拦截了
解除挡截之后建议用activity的onKeyDown方法(onBackPressed 还是监听不到)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
if(hasExitBox){//直接调用sdk的退出框
OutFace.getInstance(this).outQuit(this);
}else{
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("status",0);
} catch (JSONException e) {
e.printStackTrace();
}
sActivity.evalStringWithJsonData("xxx.showExitAlert", jsonObject);
}
return true;
}
return super.onKeyDown(keyCode, event);
}