[SOLVED] Prevent sleep mode in v3.6?

Hi,

I just released an app for kids for Android, in which you have to use the accelerometer to move the character.

The problem is that the app enters on sleep mode in the middle of the game.

I was looking on the forum and I found the solution: In Cocos2dxActivity.java I set mGLSurfaceView.setKeepScreenOn(value); to TRUE.

But unfortunately it doesn’t work for me. I’m using cocos2dx.v-3.6

How can I do to solve it? I really need it, because if not, my game is unplayable… :frowning:

I would call getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); in the onCreate function of your activity.

1 Like

Thank you so much @mars3142 ! Do you mean in my Cocos2dxActivity.java?

Check out your AndroidManifest.xml. It’s the one with intent-filter with the action of android.intent.action.MAIN (if you only have one activity). In a fresh 3.16 project this will be called AppActivity.java and the Cocos2dxActivity.java is the base class of it.

Thanks again,

I only have one activity in my AndroidManifest.xml

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

This is my AppActivity.java:

package org.cocos2dx.cpp; import org.cocos2dx.lib.Cocos2dxActivity;

public class AppActivity extends Cocos2dxActivity {
}

I do not have any onCreate function inside my AppActivity.java, so where I should place getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); ?

Create them :wink:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!isTaskRoot()) { // this was added in 3.16
       return;
    }

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
1 Like

I added it, but I get an error while compiling.

I’m using v.3.6 (a bit old, but I use this version because I still work with CocoStudio for the animations).

Do you know how can I do for this version of cocos2dx?

You can probably upgrade to v3.10 or v3.11 safely and still use Studio.

What’s the compiling error? Just remove the if() {} section. The remaining part should work.

I tried it when 3.10 and 3.11 was released, but I got strange behaviors with my animations, so I keep using 3.6.

Do you know if is still planned to add the feature of import csbs to cocos creator?

Yes, I tried it but still have errors:

-compile:
[javac] Compiling 3 source files to /Users/Panor/Desktop/THalloween/proj.android/bin/classes
[javac] /Users/Panor/Desktop/THalloween/proj.android/src/org/cocos2dx/cpp/AppActivity.java:35: error: class, interface, or enum expected
[javac] public void onCreate(Bundle savedInstanceState) {
[javac] ^
[javac] /Users/Panor/Desktop/THalloween/proj.android/src/org/cocos2dx/cpp/AppActivity.java:39: error: class, interface, or enum expected
[javac] getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
[javac] ^
[javac] /Users/Panor/Desktop/THalloween/proj.android/src/org/cocos2dx/cpp/AppActivity.java:40: error: class, interface, or enum expected
[javac] }
[javac] ^
[javac] 3 errors

Is the onCreate function within the AppActivity class? Please show the complete file. It has to be within

 public class AppActivity extends Cocos2dxActivity {
 }

PS: And you have to import import import android.os.Bundle and android.view.WindowManager as well.

1 Like

I believe that it is and that might already be available. I am not entirely sure. I know it was a feature that everyone wanted and I know there was internal discussion about it. Let me reach out to the engineering team on this and ask.

This is my AppActivity class:

package org.cocos2dx.cpp;

import org.cocos2dx.lib.Cocos2dxActivity;
import android.os.Bundle;
import android.view.WindowManager;

public class AppActivity extends Cocos2dxActivity {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

}

The } above the @override has to move to the end of the file, so the onCreate is scoped with the class.

Its working! Thank you so much!!!

I tried it before, but I think I did not save the file before compile it lol.

I was told that importing from Creator has been available for some time already.

1 Like

Thanks for the info, I wished that since long time ago!! Definitely I will try it!!