Avoid entering sleep / background mode


Hi all,

I am doing a game mainly for Android, but I do not discard porting to other platforms. Developing for min SDK version 8, testing with a Samsung Galaxy S i9003 with Android 3.2. Using cocos2d-x version 2.

My game is using accelerometer in some moments. The problem is that if the player doesn’t touch the screen while playing with the accelerometer, the system goes to sleep and enter background mode. My question is that if cocos2dx provides any mechanism to prevent android go to sleep while using accelerometer.

Another solution is to avoid the entire app entering sleep mode, but I prefer do not avoid it in entire app, I prefer only do it whilst using accelerometer.

Is there any way? Thanks in advance!


Good suggestion, i think it should be implemented in engine if the issue exists as you said.


hi i am having the same problem.
Did you find out any solution to this problem?
Could you please specify where this has been implemented?


Hi, I have same problem: I actually acquire a wake lock in onCreate of Android project:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, “My WakeLock”);

But this lock prevents the application enters sleep mode even in background. How can I activate/deactivate wake lock depending on cocos2dx- scene?? any suggestion??

Thanks in advance


This is better, no wake lock needed:

    public Cocos2dxGLSurfaceView onCreateView() {
        // Init handler

        // FrameLayout
        ViewGroup.LayoutParams framelayout_params =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        FrameLayout framelayout = new FrameLayout(this);

        // Cocos2dxGLSurfaceView
        Cocos2dxGLSurfaceView gLSurfaceView = new Cocos2dxGLSurfaceView(this);

        // ...add to FrameLayout

        gLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());

        // Set framelayout as the content view

        // set the keep screen on flag      

        return gLSurfaceView;


Thank you Oren!

I made this change and now the device goes to sleep mode properly when the game is in background :smiley:

The next is activate/deactivate keep screen depening on cocos2d-x scene.


Now I have the same problems,have you found the next step to keep screen on depending on cocos2d-x scene?


What is the path of the file edited?


The answer is quite old but the path to where that method resides is:


But i don’t think it works anymore (at least not in cocos2d-x 3.6)

Refer to this post for a solution using JNI or directly modifying the Cocos2dxActivity.java (same path as above)