Here is how I did it (with the ability to toggle it on or off, for example if only one scene need this feature) :
public static void preventIdleTimerSleep(boolean prevent)
{
//Those operations must be executed on the main thread, otherwise there is an exception
getMainActivity().runOnUiThread(prevent ? new Runnable()
{ @Override
public void run()
{
Log.i(TAG, “Preventing app from sleep”);
getMainActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
: new Runnable()
{ @Override
public void run()
{
Log.i(TAG, “Stop preventing app from sleep”);
getMainActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
}
You need to create a JNI wrapper to call it from C++. It is fairly easy to do.
You’ll need the WAKE_LOCK permission in your manifest.
It’s not really advanced (a bit complicated at first, but it’s required as soon as you need an Android feature not already in cocos2d-x). There are some JNI wrappers in cocos2d-x project you can take example on.
But yes, changing directly Cocos2dxActivity is the easy solution.