cocos2d-x编译安卓版本时实现“再按一次退出程序”的效果

转载自: http://blog.csdn.net/somestill/article/details/18046441



 我们常见的安卓项目都有再按一次退出程序的提示,或者是要有一个确认框,这样可以避免由于误按导致的程序的退出,所以,当我们通过cocos2d-x制作项目时,也常用到这样的功能,如果通过c++来实现的话,会相对麻烦些,况且不同地方都要设置,相对麻烦,而通过原生的java就可以很好的解决这个问题。

        1、我用的cocos2d-x的版本为2.1.5,版本不同,可能具体的解决方法不同,但思路相同。(转载者注: 3.2版本亲测可用)

        2、首先找到下图中的这个文件:src目录下的Cocos2dxGLSurfaceView.java文件

 

找到其中的onKeyDown函数如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2.     public boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent) {  
  3.         switch (pKeyCode) {  
  4.             case KeyEvent.KEYCODE_BACK:  
  5.             case KeyEvent.KEYCODE_MENU:  
  6.                 this.queueEvent(new Runnable() {  
  7.                     @Override  
  8.                     public void run() {  
  9.                         Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);  
  10.                     }  
  11.                 });  
  12.                 return true;  
  13.             default:  
  14.                 return super.onKeyDown(pKeyCode, pKeyEvent);  
  15.         }  
  16.     }  
修改为如下代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2.     public boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent) {  
  3.         switch (pKeyCode) {  
  4.             case KeyEvent.KEYCODE_BACK:  
  5.                 return false;  
  6.             case KeyEvent.KEYCODE_MENU:  
  7.                 this.queueEvent(new Runnable() {  
  8.                     @Override  
  9.                     public void run() {  
  10.                         Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);  
  11.                     }  
  12.                 });  
  13.                 return true;  
  14.             default:  
  15.                 return super.onKeyDown(pKeyCode, pKeyEvent);  
  16.         }  
  17.     }  
也就是在监听到KEYCODE_BACK时,返回false,而之前返回的是true;
       3、找到我们自己设置的安卓的包的标示文件,比如我的是com.planefight.org


打开其中的PlaneFight.java文件,重写onKeyDown方法。

系统自动生成的这个文件的原代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**************************************************************************** 
  2. Copyright (c) 2010-2011 cocos2d-x.org 
  3.  
  4. http://www.cocos2d-x.org 
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy 
  7. of this software and associated documentation files (the "Software"), to deal 
  8. in the Software without restriction, including without limitation the rights 
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
  10. copies of the Software, and to permit persons to whom the Software is 
  11. furnished to do so, subject to the following conditions: 
  12.  
  13. The above copyright notice and this permission notice shall be included in 
  14. all copies or substantial portions of the Software. 
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
  22. THE SOFTWARE. 
  23. ****************************************************************************/  
  24. package com.planefight.org;  
  25.   
  26. import org.cocos2dx.lib.Cocos2dxActivity;  
  27. import org.cocos2dx.lib.Cocos2dxGLSurfaceView;  
  28.   
  29. import android.os.Bundle;  
  30. import android.view.KeyEvent;  
  31. import android.widget.Toast;  
  32.   
  33. public class PlaneFight extends Cocos2dxActivity{  
  34.       
  35.     protected void onCreate(Bundle savedInstanceState){  
  36.         super.onCreate(savedInstanceState);   
  37.     }  
  38.   
  39.     public Cocos2dxGLSurfaceView onCreateView() {  
  40.         Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);  
  41.         // PlaneFight should create stencil buffer  
  42.         glSurfaceView.setEGLConfigChooser(5650168);  
  43.           
  44.         return glSurfaceView;  
  45.     }  
  46.       
  47.   
  48.     static {  
  49.         System.loadLibrary("cocos2dcpp");  
  50.     }       
  51. }  
修改后的代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**************************************************************************** 
  2. Copyright (c) 2010-2011 cocos2d-x.org 
  3.  
  4. http://www.cocos2d-x.org 
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy 
  7. of this software and associated documentation files (the "Software"), to deal 
  8. in the Software without restriction, including without limitation the rights 
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
  10. copies of the Software, and to permit persons to whom the Software is 
  11. furnished to do so, subject to the following conditions: 
  12.  
  13. The above copyright notice and this permission notice shall be included in 
  14. all copies or substantial portions of the Software. 
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
  22. THE SOFTWARE. 
  23. ****************************************************************************/  
  24. package com.planefight.org;  
  25.   
  26. import org.cocos2dx.lib.Cocos2dxActivity;  
  27. import org.cocos2dx.lib.Cocos2dxGLSurfaceView;  
  28.   
  29. import android.os.Bundle;  
  30. import android.view.KeyEvent;  
  31. import android.widget.Toast;  
  32.   
  33. public class PlaneFight extends Cocos2dxActivity{  
  34.     private long exitTime = 0;  
  35.     protected void onCreate(Bundle savedInstanceState){  
  36.         super.onCreate(savedInstanceState);   
  37.     }  
  38.   
  39.     public Cocos2dxGLSurfaceView onCreateView() {  
  40.         Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);  
  41.         // PlaneFight should create stencil buffer  
  42.         glSurfaceView.setEGLConfigChooser(5650168);  
  43.           
  44.         return glSurfaceView;  
  45.     }  
  46.       
  47.       
  48.     @Override  
  49.     public boolean onKeyDown(int keyCode, KeyEvent event)   
  50.     {  
  51.          if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)  
  52.          {      
  53.                 if((System.currentTimeMillis()-exitTime) > 2000)  //System.currentTimeMillis()无论何时调用,肯定大于2000  
  54.                  {  
  55.                   Toast.makeText(getApplicationContext(), "再按一次退出程序",Toast.LENGTH_SHORT).show();                                  
  56.                   exitTime = System.currentTimeMillis();  
  57.                  }  
  58.                  else  
  59.                  {  
  60.                      finish();  
  61.                      System.exit(0);  
  62.                  }  
  63.                            
  64.                  return true;  
  65.          }  
  66.          return super.onKeyDown(keyCode, event);  
  67.     }  
  68.       
  69.   
  70.     static {  
  71.         System.loadLibrary("cocos2dcpp");  
  72.     }       
  73. }  
也就是添加了重写的onKeyDown函数,并在之前声明了变量exitTime。

这样,就实现了在安卓应用任何页面按返回键均有提示“再按一次退出程序”;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值