Our common android project prompts you to exit the program again, or you need a confirmation box to avoid the exit of the program due to incorrect clicking, when we make a project through cocos2d-x, also commonly used to this function, if through c ++ to achieve, it will be relatively troublesome, and different places have to set, relatively troublesome, the native java can solve this problem well.
1. I used the cocos2d-x version 2.1.5, the version is different, the specific solution may be different, but the idea is the same.
2. First find the Cocos2dxGLSurfaceView. java file in the src directory.
Find the onKeyDown function as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> @ Overridepublic boolean onKeyDown (final int pKeyCode, final KeyEvent pKeyEvent) {switch (pKeyCode) {case KeyEvent. KEYCODE_BACK: case KeyEvent. KEYCODE_MENU: this. queueEvent (new Runnable () {@ Overridepublic void run () {Cocos2dxGLSurfaceView. this. mCocos2dxRenderer. handleKeyDown (pKeyCode) ;}}); return true; default: return super. onKeyDown (pKeyCode, pKeyEvent );}}Modify it to the following code:
@Overridepublic boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent) {switch (pKeyCode) {case KeyEvent.KEYCODE_BACK:return false;case KeyEvent.KEYCODE_MENU:this.queueEvent(new Runnable() {@Overridepublic void run() {Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);}});return true;default:return super.onKeyDown(pKeyCode, pKeyEvent);}}That is, when listening to KEYCODE_BACK, false is returned, and true is returned before;
3. Find the android package ID file we have set. For example, my package is com.planefight.org.
Open the PlaneFight. java file and rewrite the onKeyDown method.
The original code of the file automatically generated by the system:
/****************************************************************************Copyright (c) 2010-2011 cocos2d-x.orghttp://www.cocos2d-x.orgPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.****************************************************************************/package com.planefight.org;import org.cocos2dx.lib.Cocos2dxActivity;import org.cocos2dx.lib.Cocos2dxGLSurfaceView;import android.os.Bundle;import android.view.KeyEvent;import android.widget.Toast;public class PlaneFight extends Cocos2dxActivity{ protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);} public Cocos2dxGLSurfaceView onCreateView() { Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this); // PlaneFight should create stencil buffer glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8); return glSurfaceView; } static { System.loadLibrary("cocos2dcpp"); } }Modified code:
/*************************************** * *********************************** Copyright (c) 2010-2011 cocos2d-x.org http://www.cocos2d-x.orgPermission Is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be encoded inall copies or substantial portions of the Software. the software is provided "as is", without warranty of any kind, express orimplied, including but not limited to the warranties of merchantability, fitness for a particle purpose and noninfringement. in no event shall theauthors or copyright holders be liable for any claim, damages or otherliability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings inthe software. **************************************** * ********************************/package com.planefight.org; import org. cocos2dx. lib. cocos2dxActivity; import org. cocos2dx. lib. cocos2dxGLSurfaceView; import android. OS. bundle; import android. view. keyEvent; import android. widget. toast; public class PlaneFight extends Cocos2dxActivity {private long exitTime = 0; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);} public Cocos2dxGLSurfaceView onCreateView () {Cocos2dxGLSurfaceView glSurfaceView = new topology (this); // PlaneFight shocould create stencel buffer surglfaceview. setEGLConfigChooser (5, 6, 5, 0, 16, 8); return glSurfaceView;} @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK & event. getAction () = KeyEvent. ACTION_DOWN) {if (System. currentTimeMillis ()-exitTime)> 2000) // System. currentTimeMillis () must be greater than 2000 {Toast at any time. makeText (getApplicationContext (), "exit the program again", Toast. LENGTH_SHORT ). show (); exitTime = System. currentTimeMillis ();} else {finish (); System. exit (0);} return true;} return super. onKeyDown (keyCode, event);} static {System. loadLibrary ("cocos2dcpp ");}}That is, the override onKeyDown function is added and the variable exitTime is declared before.
In this way, when you press the return key on any page of the android application, the system prompts "exit the program again ";
This article is original. For more information, see the source.