Avoid entering sleep / background mode


#1

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!


#2

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


#3

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?


#4

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”);
wl.acquire();

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


#5

This is better, no wake lock needed:

    public Cocos2dxGLSurfaceView onCreateView() {
        // Init handler

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

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

        // ...add to FrameLayout
        framelayout.addView(gLSurfaceView);

        gLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());

        // Set framelayout as the content view
        setContentView(framelayout);

        // set the keep screen on flag      
        gLSurfaceView.setKeepScreenOn(true);

        return gLSurfaceView;
    }

#6

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.


#7

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


#8

What is the path of the file edited?


#9

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

YOUR-PROJECT-DIR/cocos2d/cocos/platform/android/java/src/org/cocos2dx/lib

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)