diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java index aa215b065f..93c001d769 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java @@ -403,10 +403,15 @@ public class CocosHelper { }); } public static void setKeepScreenOn(boolean keepScreenOn) { - if (keepScreenOn) { - sActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - } else { - sActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - } + sActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + if (keepScreenOn) { + sActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } else { + sActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + } + }); } } diff --git a/native/cocos/platform/java/jni/JniImp.cpp b/native/cocos/platform/java/jni/JniImp.cpp index 422879449d..02ba120fe9 100644 --- a/native/cocos/platform/java/jni/JniImp.cpp +++ b/native/cocos/platform/java/jni/JniImp.cpp @@ -115,6 +115,10 @@ void setVibrateJNI(float duration) { JniHelper::callStaticVoidMethod(JCLS_HELPER, "vibrate", duration); } +void setKeepScreenOnJNI(bool isEnabled) { + return JniHelper::callStaticVoidMethod(JCLS_HELPER, "setKeepScreenOn", isEnabled); +} + void finishActivity() { JniHelper::callStaticVoidMethod(JCLS_HELPER, "finishActivity"); } diff --git a/native/cocos/platform/java/jni/JniImp.h b/native/cocos/platform/java/jni/JniImp.h index 82939a3556..8eb285b8c9 100644 --- a/native/cocos/platform/java/jni/JniImp.h +++ b/native/cocos/platform/java/jni/JniImp.h @@ -37,6 +37,7 @@ extern void copyTextToClipboardJNI(const ccstd::string &text); extern ccstd::string getDeviceModelJNI(); extern int getDPIJNI(); extern void setVibrateJNI(float duration); +extern void setKeepScreenOnJNI(bool isEnabled); extern int getNetworkTypeJNI(); extern float *getSafeAreaEdgeJNI(); extern int getDeviceRotationJNI(); diff --git a/native/cocos/platform/java/modules/CommonScreen.cpp b/native/cocos/platform/java/modules/CommonScreen.cpp index ee6de65a7e..21f97e182c 100644 --- a/native/cocos/platform/java/modules/CommonScreen.cpp +++ b/native/cocos/platform/java/modules/CommonScreen.cpp @@ -60,7 +60,8 @@ float CommonScreen::getDevicePixelRatio() const { void CommonScreen::setKeepScreenOn(bool keepScreenOn) { // JniHelper::callStaticVoidMethod(JCLS_HELPER, "setKeepScreenOn", value); // ANativeActivity_setWindowFlags(JniHelper::getAndroidApp()->activity, AWINDOW_FLAG_KEEP_SCREEN_ON, 0); - CC_UNUSED_PARAM(keepScreenOn); + //CC_UNUSED_PARAM(keepScreenOn); + return setKeepScreenOnJNI(keepScreenOn); } IScreen::Orientation CommonScreen::getDeviceOrientation() const {